ssh 安全的远程登录
22/tcp软件实现openssh:centos默认安装dropbear ssh协议版本v1:基于crc-32做MAC 不安全;man-in-middlev2:双方主机协议选择安全的MAC方式基于DH算法做密钥交换,基于RSA或DSA实现身份认证相关包opensshopenssh-clientsopenssh-server工具基于C/S结构Linux client:ssh scp sftp sloginwindows client:xshell putty securecrt sshsecureshellclient server :sshd客户端配置文件[root @ centos7 CA]#vim /etc/ssh/ssh_config StrictHostKeyChecking no 首次登录不显示检查提示格式:ssh[user@] host [COMMAND] ssh[-l user] host [COMMAND][root @ centos7 ~]#ssh root@192.168.47.103 ls /data[root @ centos7 ~]#ssh -l root 192.168.47.103 ls /data常见选项 -p port:远程服务器监听的端口 -b:指定连接的源IP -v:调试模式 -C:压缩方式 -X:支持x11转发 -t:强制伪tty分配
ssh -t remoteserver1 ssh -t remoteserver2 ssh remoteserver3
用户远程连接ssh服务器,会复制shh服务器/etc/ssh/ssh_host*key.pub文件中的公钥到客户机的~./ssh/know_hosts中,下次连接时,会自动匹配相应私钥,不能匹配,将拒绝连接1)客户端发起链接请求服务端返回自己的公钥,以及一个会话ID(这一步客户端得到服务端公钥)
2)客户端生成密钥对客户端用自己的公钥异或会话ID,计算出一个值Res,并用服务端的公钥加密客户端发送加密后的值到服务端,服务端用私钥解密,得到Res服务端用解密后的值Res异或会话ID,计算出客户端的公钥(这一步服务端得到客户端公钥)3)最终:双方各自持有三个秘钥,分别为自己的一对公、私钥,以及对方的公钥,之后的所有通讯都会被加密用户登录认证方式:
1)基于password2)基于key1.客户端发起ssh请求,服务器会把自己的公钥发送给用户
2.用户会根据服务器发来的公钥对服务器用户的密码进行加密3.加密后的信息回传给服务器,服务器用自己的私钥解密,用得到的密码与shadow文件中密码进行比对,如果密码正确,则用户登录成功1.首先在客户端生成一对密钥(ssh-keygen)
2.并将客户端的公钥ssh-copy-id 拷贝到服务端3. 当客户端再次发送一个连接请求,包括ip、用户名4.服务端得到客户端的请求后,会到authorized_keys中查找,如果有响应的IP和用户,就会随机生成一个字符串,例如:gesila5.服务端将使用客户端拷贝过来的公钥进行加密,然后发送给客户端6.得到服务端发来的消息后,客户端会使用私钥进行解密,然后将解密后的字符串发送给服务端7.服务端接受到客户端发来的字符串后,跟之前的字符串进行对比,如果一致,就允许免密码登录基于key验证方式
1)客户端生成密钥对
ssh-keygen [-t [rsa|dsa]] [-P ''] [-f "~/.ssh/id_rsa"] -P 对私钥加密的口令,可以不指定 -f 指定文件 默认家目录的.ssh下,隐藏文件如果在生成密钥对的时候-P 加入了密码,ssh连接的时候 需要输私钥的密重设私钥口令:ssh-keygen -p如果你不想输入密码直接登录,可以托管 1.ssh-agent bash #启用代理 2.ssh-add #把口令托管给代理2)公钥文件传输至远程服务器对应用户的家目录ssh-copy-id [-i[identity_file]] [user@]host #拷贝到 ~/.ssh/authorized_keys文件中去[root @ centos7 ~]#ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): 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:37hopStfoXk3QLINB3cwIGkxstFiID8KvU9sCow24UQ root@centos7.1 The key's randomart image is: +---[RSA 2048]----+ |.E..o.+oo.+.. | |.+ o++o o o | |.o+..o o o | |*..+ B | |+=. + S + | |...= +.= | | . . oo= + | | . o+ o . | | ++.. | +----[SHA256]-----+ [root @ centos7 ~]#ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.47.103 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.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@192.168.47.103's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.47.103'" and check to make sure that only the key(s) you wanted were added. [root @ centos7 ~]#ssh root@192.168.47.103 Last login: Mon Apr 12 02:27:47 2021 from 192.168.47.100
精彩评论