一、准备环境
m01 10.0.0.61
backup 10.0.0.41
二、远程连接工具
2.1 ssh
2.2 telnet服务
安装: yum install -y telnet-server
启动telnet:systemctl restart telnet.socket
如:telnet root@10.0.0.61 23
本地shell
涉及工具:wireshark(windows+Linux)抓包/tcpdump(Linux)抓包
连接网站工具:
http 超文本传输协议
https 加密超文本传输协议
2.3 ssh与telnet服务的区别?
telnet也可以连接,但他的连接是明文,没有加密,端口为23
ssh的连接是加密的,端口默认为22
三、ssh相关软件
检查软件属于哪个软件包
rpm -qf `which ssh`-----openssh-clients-7.4p1-16.el7.x86_64
rpm -qf `which sshd`-----openssh-server-7.4p1-16.el7.x86_64
3.1 客户端命令:
1>ssh:ssh客户端 远程连接,远程执行命令
参数:-p:指定远程服务器上的端口
例子1:远程连接到backup服务器
[root@m01 ~]# ssh 10.0.0.41
root@10.0.0.41's password:
Last login: Mon May 27 19:21:27 2019 from 10.0.0.61
[root@backup ~]# ll
total 4
-rw-------. 1 root root 1565 May 26 15:37 anaconda-ks.cfg
[root@backup ~]#
例子2:远程连接到指定端口,用参数-p
[root@m01 ~]# ssh -p22 10.0.0.41
root@10.0.0.41's password:
Last login: Mon May 27 19:26:11 2019 from 10.0.0.61
[root@backup ~]#
例子3:远程连接到指定端口执行个命令,执行完命令在回来
[root@m01 ~]# ssh -p22 oldboy@10.0.0.41 whoami
oldboy@10.0.0.41's password:
oldboy
[root@m01 ~]#
[root@m01 ~]# ssh -p22 root@10.0.0.41 ip a s eth1|awk -F'[ /]+' 'NR==3{print $3}'
root@10.0.0.41's password:
172.16.1.41
[root@m01 ~]#
注:远程执行命令的时候,命令不要太长或太复杂,如果太复杂的话就远程执行脚本
2>scp:远程传输数据(加密)
rcp:remote cp未加密
参数:
-P:指定远程主机的端口号
-r:以递归方式,复制目录
[root@m01 ~]# scp -p22 /etc/hostname 10.0.0.41:/tmp
root@10.0.0.41's password:
hostname 100% 4 2.4KB/s 00:00
[root@m01 ~]#
3>sftp:上传或下载,相当于lrzsz(rz和sz)
参数:-p:
ssh-----shell
sftp----xftp
①sftp远程连一下backup(10.0.0.41)服务器
[root@m01 ~]# sftp 10.0.0.41
root@10.0.0.41's password:
Connected to 10.0.0.41.
sftp> pwd
Remote working directory: /root
②sftp将本地的/etc/hostname上传到backup服务器上的/tmp下
sftp> cd /tmp
sftp> pwd
Remote working directory: /tmp
sftp> put /etc/hostname
Uploading /etc/hostname to /tmp/hostname
/etc/hostname 100% 4 10.7KB/s 00:00
③sftp将backup服务器上的/etc/hostname下载到本地的/tmp下
sftp> get /etc/hostname /opt/
Fetching /etc/hostname to /opt/hostname
/etc/hostname 100% 7 10.4KB/s 00:00
sftp> bye 退出
[root@m01 ~]#
④检查两边的数据是否正常
\\backup服务器
[root@backup /tmp]# cat /tmp/hostname
m01
[root@backup /tmp]#
\\m01
[root@m01 ~]# cat /opt/hostname
backup
[root@m01 ~]#
四、ssh服务端的配置文件
4.1 ssh服务端配置文件:/etc/ssh/sshd_config
Port 22:指定端口号
PermitRootLogin yes:是否禁止root远程登陆
PasswordAuthentication yes:是否开启通过密码登录(认证)(以后开启密钥认证再改)
PermitEmptyPasswords no:是否准许空密码
解决ssh远程连接慢的问题:
GSSAPIAuthentication no
UseDNS no
ListenAddress:监听的地址,用户可以通过哪个IP地址远程连接(监听本地网卡的IP地址)
[root@m01 /tmp]# grep 'ListenAddress' /etc/ssh/sshd_config
ListenAddress 10.0.0.61:52213
ListenAddress 172.16.1.61:22
[root@m01 /tmp]# ss -lntup |grep ssh
tcp LISTEN 0 128 10.0.0.61:52213 *:* users:(("sshd",pid=22708,fd=4))
tcp LISTEN 0 128 172.16.1.61:22 *:* users:(("sshd",pid=22708,fd=3))
[root@backup ~]# ssh -p22 172.16.1.61
root@172.16.1.61's password:
Last login: Mon May 27 11:31:06 2019 from 10.0.0.41
[root@backup ~]# ssh -p52213 10.0.0.61
root@10.0.0.61's password:
Last login: Mon May 27 11:42:59 2019 from 172.16.1.41
[root@m01 ~]#
4.2 ssh客户端配置文件:/etc/ssh/ssh_config
五、SSH认证方式
5.1 密码认证
5.2 秘钥认证
免密码认证
信任登录
1>创建秘钥认证:
##创建秘钥对
[root@m01 ~]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
SHA256:VW1UamyTZ0YDIHQxi00U7DrtJAX/BN0k5cbuhNRRA58 root@m01
The key's randomart image is:
+---[DSA 1024]----+
| .ooO**BB=|
| .*+ooO==|
| .=o.oBE+|
| . +.++= |
| S + o. o |
| + o .o |
| = . |
| . |
| |
+----[SHA256]-----+
2>检查 秘钥
[root@m01 ~]# ll ~/.ssh/
total 12
-rw------- 1 root root 668 May 27 12:13 id_dsa
-rw-r--r-- 1 root root 598 May 27 12:13 id_dsa.pub
-rw-r--r-- 1 root root 695 May 27 11:22 known_hosts
3>客户端发送公钥
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_dsa.pub 172.16.1.41
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_dsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.16.1.41's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '172.16.1.41'"
and check to make sure that only the key(s) you wanted were added.
4>测试客户端是否可以免密连接
[root@m01 ~]# ssh 172.16.1.41 hostname
backup
六、批量管理
安装软件:yum install -y sshpass pssh
1>xshell
2>pssh(epel源):多台服务器同时远程的执行相同的目录或脚本
参数
-h:指定要管理机器的hosts列表,即文件,内容格式“[user@]host[:port]”
-P:打印出服务器返回信息
-A:手动输入密码模式
批量并行执行命令
1>创建一个文件,写入要连接的ip和端口
[root@m01 ~]# cat hosts.txt
root@172.16.1.41:22
root@172.16.1.7:22
2>然后用pssh执行一条命令,看是否执行成功
[root@m01 ~]# pssh -Ph hosts.txt hostname
172.16.1.41: backup
[1] 12:42:51 [SUCCESS] root@172.16.1.41:22
172.16.1.7: web01
[2] 12:42:51 [SUCCESS] root@172.16.1.7:22
3>prsync:批量的发送脚本
参数:
-A:手动输入密码模式
-h:指定要管理机器的hosts列表,即文件内容格式“[user@]host[:port]”
-v:显示过程
[root@m01 ~]# prsync -avh hosts.txt /etc/hostname /tmp/
[1] 18:46:59 [SUCCESS] root@172.16.1.41:22
[2] 18:46:59 [SUCCESS] root@172.16.1.7:22
[root@m01 ~]# prsync -A -azh hosts.txt /etc/hostname /tmp/
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 12:52:11 [SUCCESS] root@172.16.1.41:22
[2] 12:52:11 [SUCCESS] root@172.16.1.7:22
[root@m01 ~]#
最后测试172.16.1.41和172.16.1.7两主机的/tmp/是否有hosts.txt