远程连接服务器(VMware)
远程连接服务器是通过文字或图形接口方式来远程登录系统,在远程终端控制窗口登录linux主机以取得可操作主机接口(shell),而远程连接服务器拥有分享主机的运算能力的功能。
远程连接服务器的类型(以登录的连接界面来分类),有文字接口和图形接口。文字接口又分为:明文传输(Telnet、RSH等,目前非常少用)和(加密传输:SSH为主,已经取代明文传输)。而我们使用的xshell则是基于:SSH(Secure Shell Protocol,安全的壳程序协议),它可以通过数据包加密技术将等待传输的数据包加密后再传输到网络上。
ssh协议本身提供两个服务器功能:一个是类似telnet的远程连接使用shell的服务器;另一个就是类似ftp服务的sftp-server,提供更安全的ftp服务。
目前常见的网络数据包加密技术通常是通过“非对称密钥系统”来处理的。主要通过两把不一样的公钥与私钥来进行加密与解密的过程。
公钥(public key):提供给远程主机进行数据加密的行为,所有人都可获得你的公钥来将数据加密。(公开,用来加密)
私钥(private key):远程主机使用你的公钥加密的数据,在本地端就能够使用私钥来进行解密。而私钥只有自己拥有。(私密,用来解密)
SSH的工作流程:
①版本号协商:
ssh1还是ssh2(SSH有ssh1和ssh2两个版本)
此时客户端向服务端发送TCP请求连接(三次握手),在连接建立以后。服务器发送一个支持的版本报文。客户端收到后,解析并回复自己可以支持的版本报文 。协商成功,则进入密钥算法协商环节,否则则断开连接。
②密钥和算法协商:
ssh支持多种加密算法,在本地端和shell端支持的加密算法可能不同,所以协商一种双方都支持的加密算法
服务端和客户端分别发送算法协商报文给对方,报文中包含自己支持的公钥算法列表、加密算法列表、消息验证码算法列表、压缩算法列表等。
服务端和客户端根据对方和自己支持的算法得出最终使用的算法。
服务端和客户端利用DH交换算法、主机密钥对等参数,生成会话密钥和会话ID。
假设:c公—>客户端公钥;c密—>客户端密钥;s公—>服务端公钥;s密—>服务端密钥
在版本号协商阶段完成后:服务端将 s公 发送给客户端。服务端生成会话ID ,设为 id ,发送给客户端。客户端生成会话密钥,设为 key ,并计算 res = id 异或 key。客户端将 res 用 s公 进行加密,将结果发送给服务端。服务端用 s密 进行解密,得到 res。服务器计算 res 异或 id,得到 key。
至此服务端和客户端都知道了会话密钥和会话ID,以后的数据传输都使用会话密钥进行加密和解密。
③认证阶段:
客户端请求服务器认证,服务器对客户端进行认证
SSH提供基于口令的认证(password认证)和基于密钥认证(publickey认证)
口令认证(用户名和密码认证):
客户端将用户名和密码加密发送给服务端,服务端通过解密得到的信息和设备上保存的信息进行比对,返回认证成功/失败信息)
密钥认证:
客户端产生一对公共密钥,将公钥保存到将要登录的服务器上的那个账号的家目录的.ssh/authorized_keys文件中,认证时,客户端将公钥传给服务器。服务器收到后进行比对(与本地该账号家目录下的authorized_keys中的公钥进行对比),不相同则认证失败,相同则服务端生成一段随机字符串,并先后用客户端公钥和会话密钥对其加密,发送给客户端。客户端收到后将解密后的随机字符串用会话密钥发送给服务器。如果发回的字符串与服务器端之前生成的一样,则认证通过,否则,认证失败。
④会话请求:
认证通过后,客户端向服务器端发送会话请求
⑤交互会话:
会话请求通过后,服务器端和客户端进行信息的交互
公钥密钥信息:(配对过程)
配置文件:
//密钥的登录信任文件在~/.ssh下(这个文档内是可信任文件,登录过的用户)
[root@localhost .ssh]# find / -name known*
/root/.ssh/known_hosts
//如果第一次登录则会去询问是否加入已知密钥(之后不会询问次ip):
[root@localhost ssh]# ssh root@192.168.220.234
The authenticity of host '192.168.220.234 (192.168.220.234)' can't be established.
ECDSA key fingerprint is SHA256:GHgk+fS5qfsLIwLedFAoPM4kMJrT8gaJEJGFWPWLIRM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.220.234' (ECDSA) to the list of known hosts.
root@192.168.220.234's password:
Activate the web console with: systemctl enable --now cockpit.socket
This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register
Last login: Tue Dec 21 06:13:05 2021 from 192.168.220.1
[root@lxb ~]# exit
//允许密钥的配置产生文件是在/etc/ssh/下
[root@lxb ssh]# ll
总用量 600
-rw-r--r--. 1 root root 577388 1月 8 2020 moduli
-rw-r--r--. 1 root root 1716 1月 8 2020 ssh_config
drwxr-xr-x. 2 root root 28 12月 14 14:57 ssh_config.d
-rw-------. 1 root root 4425 1月 8 2020 sshd_config
-rw-r-----. 1 root ssh_keys 480 12月 14 15:10 ssh_host_ecdsa_key
-rw-r--r--. 1 root root 162 12月 14 15:10 ssh_host_ecdsa_key.pub
-rw-r-----. 1 root ssh_keys 387 12月 14 15:10 ssh_host_ed25519_key
-rw-r--r--. 1 root root 82 12月 14 15:10 ssh_host_ed25519_key.pub
-rw-r-----. 1 root ssh_keys 2578 12月 14 15:10 ssh_host_rsa_key
-rw-r--r--. 1 root root 554 12月 14 15:10 ssh_host_rsa_key.pub
//非对称密钥加密有三种方式,分别是rsa,ecdsa,ed25519
//而主要配置文件则是在/etc/ssh/sshd_config 下
[root@lxb ~]# cat /etc/ssh/sshd_config
#Port 22 监听端口,默认监听22端口 (可以修改)
#AddressFamily any IPV4和IPV6协议家族用哪个,any表示二者均有
#ListenAddress 0.0.0.0 指明监控的地址,0.0.0.0表示本机的所有地址 【默认可修改】
#ListenAddress :: 指明监听的IPV6的所有地址格式
#Protocol 2 使用SSH第二版本
# HostKey for protocol version 1 第一版的SSH支持的秘钥形式
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2 第二版本SSH秘钥,支持以下四种秘钥认证的存放位置
HostKey /etc/ssh/ssh_host_rsa_key rsa私钥认证 (默认)
#HostKey /etc/ssh/ssh_host_dsa_key dsa私钥认证
HostKey /etc/ssh/ssh_host_ecdsa_key ecdsa私钥认证
HostKey /etc/ssh/ssh_host_ed25519_key ed25519私钥认证
#ServerKeyBits 1024 主机秘钥长度
# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV 当使用ssh登录系统的时候,SSH会记录信息在/var/log/secure里面
#LogLevel INFO 日志的等级
# Authentication:
#LoginGraceTime 2m 登录的有效时间,超过则自动断开连接
#PermitRootLogin yes 是否允许管理员远程登录,'yes'表示允许
#StrictModes yes 是否让sshd去检查用户主目录或相关文件的权限数据
#MaxAuthTries 6 最大认证尝试次数,之后需要等待某段时间后才能再次输入密码
#MaxSessions 10 允许的最大会话数
AuthorizedKeysFile .ssh/authorized_keys
选择密钥验证时,客户端生成一对公私钥之后,会将公钥放到.ssh/authorizd_keys里面
PasswordAuthentication yes 是否允许支持基于口令的认证
ChallengeResponseAuthentication no
是否允许使用键盘输入身份验证,也就是xshell的第三个登录方式
#UseDNS yes 是否反解DNS,如果想让客户端连接服务器端快一些,这个可以改为no
Subsystem sftp /usr/libexec/openssh/sftp-server
支持 SFTP ,如果注释掉,则不支持sftp连接
AllowUsers user1 user2
登录白名单(需要自己手动添加),允许远程登录的用户
如果名单中没有的用户,则提示拒绝登录
-
设置端口登录:
//在客户端上配置允许登录的端口号
[root@lxb ~]# vim /etc/ssh/sshd_config
Port 11111
//关闭防火墙,查看是否信任,然后重启服务
[root@lxb ~]# systemctl stop firewalld
[root@lxb ~]# getenforce
Permissive
[root@lxb ~]# systemctl restart sshd
//然后选择另一个虚拟机,登录客户虚拟机
[root@localhost ssh]# ssh root@192.168.220.234
ssh: connect to host 192.168.220.234 port 22: Connection refused
//此时发现登录默认的22端口无法成功,显示拒绝服务
[root@localhost ssh]# ssh root@192.168.220.234 -p 11111
root@192.168.220.234's password:
//带上11111端口后,发现可以登录这个虚拟机
-
服务器解决root登录:
//修改配置文件,设置为no
[root@lxb ~]# vim /etc/ssh/sshd_config
PermitRootLogin no
//重启服务
[root@lxb ~]# systemctl restart sshd
//此时选择登录,发现输入密码后,无法登录
[root@localhost ssh]# ssh root@192.168.220.234 -p 11111
root@192.168.220.234's password:
Permission denied, please try again.
root@192.168.220.234's password:
Permission denied, please try again.
root@192.168.220.234's password:
//但是使用普通用户还是可以登录
[root@localhost ssh]# ssh lxb@192.168.220.234 -p 11111
lxb@192.168.220.234's password:
Activate the web console with: systemctl enable --now cockpit.socket
This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register
[lxb@lxb ~]$
-
允许服务器(特定用户)登录:
//修改配置文件,增加允许等录
[root@lxb ~]# vim /etc/ssh/sshd_config
allowusers lsp
//重启服务器
[root@lxb ~]# systemctl restart sshd
//此时发现root和lxb用户都无法登录,只有lsp可以登录
[root@localhost ssh]# ssh lxb@192.168.220.234 -p 11111
lxb@192.168.220.234's password:
Permission denied, please try again.
lxb@192.168.220.234's password:
[root@localhost ssh]# ssh root@192.168.220.234 -p 11111
root@192.168.220.234's password:
Permission denied, please try again.
root@192.168.220.234's password:
[root@localhost ssh]# ssh lsp@192.168.220.234 -p 11111
lsp@192.168.220.234's password:
Activate the web console with: systemctl enable --now cockpit.socket
This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register
[lsp@lxb ~]$
-
免密登录(root用户):
//设置一个客户端公钥(使用rsa加密形式)
[root@lxb /]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:QPoBN4rwsgLvzM5IecRjsUkWtFxNaD+IuE/ga1wskGw root@lxb
The key's randomart image is:
+---[RSA 3072]----+
|. .o..B. |
| o..+O o |
|+.+*= = |
|+E+.+o = |
|* =B . S |
|.B++. |
|.oX. |
|.B.. |
|o o |
+----[SHA256]-----+
[root@lxb /]#
//此时产生一对公私密钥
//公钥存放在/root/.ssh/id_rsa.pub
//私钥存放在/root/.ssh/id_rsa
//此时将公钥发送到服务器
[root@lxb /]# scp /root/.ssh/id_rsa.pub root@192.168.220.131:/root/.ssh
The authenticity of host '192.168.220.131 (192.168.220.131)' can't be established.
ECDSA key fingerprint is SHA256:jy0EtFt7IuRKPmUUE7QudvMPCukUlKiFgbnddo1oR34.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.220.131' (ECDSA) to the list of known hosts.
root@192.168.220.131's password:
id_rsa.pub 100% 562 619.0KB/s 00:00
//此时去服务器查看是否可以登录客户端
[root@localhost ssh]# ssh root@192.168.220.234
ssh: connect to host 192.168.220.234 port 22: Connection refused
[root@localhost ssh]#
//实现服务端免密登录
//此时在服务端将文件 id_rsa.pub 名字修改为authorized_keys
[root@localhost .ssh]# mv id_rsa.pub authorized_keys
//此时去客户端去登录服务端
[root@lxb /]# ssh root@192.168.220.131
Activate the web console with: systemctl enable --now cockpit.socket
This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register
Last login: Tue Dec 21 08:36:09 2021 from 192.168.220.234
[root@localhost ~]# exit
//发现已经实现客户端免密登录
-
免密登录(非root用户):
//在客户端产生密钥id_rsa_miyao
[root@lxb /]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa_miyao
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa_miyao.
Your public key has been saved in /root/.ssh/id_rsa_miyao.pub.
The key fingerprint is:
SHA256:Di1uCwMI3OCH1nRIC2sOVS1lVh/Bbll/b1tRtR4we24 root@lxb
The key's randomart image is:
+---[RSA 3072]----+
| oo+o++...o. o o|
|o.O.+o. .... + o|
|oB = . ..o o +.|
|*.. . + =.o|
|... o S. Eo|
| . . + . +|
| o o . .o|
| + . . |
| . |
+----[SHA256]-----+
//此时将密钥发送到服务端的lxb用户上
[root@lxb /]# scp /root/.ssh/id_rsa_miyao.pub root@192.168.220.131:/home/lxb/.ssh
id_rsa_miyao.pub 100% 562 627.1KB/s 00:00
//此时发现在192.168.220.131的lxb用户下产生了一个.ssh 文件,但是无法实现免密登录lxb
//此时需要重新在lxb文件下创建文件夹.ssh
//然后重新进行scp传输,并修改lxb的文件为authorized_keys
[root@localhost .ssh]# mv id_rsa_miyao.pub authorized_keys
//此时需要给lxb用户下的.ssh文件夹和authorized_keys设置用户名和用户组为lxb
//此时在客户端下登录lxb用户
[root@lxb .ssh]# ssh lxb@192.168.220.131 -i id_rsa_miyao
Activate the web console with: systemctl enable --now cockpit.socket
This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register
Last login: Tue Dec 21 09:15:20 2021 from 192.168.220.234
[lxb@localhost ~]$
[lxb@localhost ~]$ exit
// !!!此时需要注意,客户端登录需要使用-i 并用设置的给lxb的密钥/root/.ssh/id_rsa_miyao
//如果scp传输给服务端的用户是root,需要修改目标用户的chown用户和组
//而且用户下没有.ssh的话,直接scp可能会无法读取,需要在用户下mkdir一个.ssh文件
-
Xshell免密登录:
Xshell产生密钥