OpenSSH
是 SSH (Secure SHell)
协议的免费开源实现。SSH协
议族可以用来进行远程控制, 或在计算机之间传送文件。而实现此功能的传统方式,如telnet
(终端仿真协议)、rcp ftp、 rlogin、rsh
都是极为不安全的,并且会使用明文传送密码。OpenSSH
提供了服务端后台程序和客户端工具,用来加密远程控件和文件传输过程中的数据,并由此来代替原来的类似服务。
openSSH
的工作模式
openSSH
是基于C/S架构工作的。
服务器端 //sshd,配置文件在/etc/ssh/sshd_config
客户端 //ssh,配置文件在/etc/ssh/ssh_config
ssh-keygen //密钥生成器
ssh-copy-id //将公钥传输至远程服务器
scp //跨主机安全复制工具
1. 第一次远程主机首次登录,使用exit退出返回之前的shell:
// 不使用账号登录就是以当前主机的当前账号去连接
[root@scl ~]# ssh 192.168.100.96
[email protected]'s password:
Last login: Thu Jun 27 08:57:20 2019 from 192.168.100.1
[root@localhost ~]# exit
登出
Connection to 192.168.100.96 closed.
[root@scl ~]#
2.指定用户名登录:
[root@scl ~]# ssh [email protected]
[email protected]'s password:
Last failed login: Thu Jun 27 09:04:52 CST 2019 from 192.168.100.100 on ssh:notty
There were 5 failed login attempts since the last successful login.
[tom@localhost ~]$ exit
登出
Connection to 192.168.100.96 closed.
[root@scl ~]#
3.以远程用户身份在远程主机上执行一条命令,不登录进去
[root@scl ~]# ssh [email protected] 'ifconfig ens33'
[email protected]'s password:
ens33: flags=4163 mtu 1500
inet 192.168.100.96 netmask 255.255.255.0 broadcast 192.168.100.255
ether 00:0c:29:ad:5e:54 txqueuelen 1000 (Ethernet)
RX packets 1283 bytes 129676 (126.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 945 bytes 101586 (99.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@scl ~]#
ssh
通过公钥加密的方式保持通信安全。当某一ssh
客户端连接到ssh
服务器时,在该客户端登录之前,服务器会向其发送公钥副本。这可用于为通信渠道设置安全加密,并可验证客户端的服务器。
当用户第一次使用ssh连接到特定服务器时,ssh
命令可在用户的~/.ssh/known_hosts
文件中存储该服务器的公钥。在此之后每当用户进行连接时,客户端都会通过对比~/.ssh/known_hosts
文件中的服务器条目和服务器发送的公钥,确保从服务器获得相同的公钥。如果公钥不匹配,客户端会假定网络通信已遭劫持或服务器已被入侵,并且中断连接。
这意味着,如果服务器的公钥发生更改(由于硬盘出现故障导致公钥丢失,或者出于某些正当理由替换公钥),用户则需要更新其~/.ssh/known_hosts
文件并删除旧的条目才能够进行登录。
主机端:
~/.ssh/known_hosts //存放服务器的公钥
[root@scl ~]# cat ~/.ssh/known_hosts
192.168.100.96 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFtvQzKVagWukVUwPOghxDwsP4YC9DSHNCSlDkpfCpL0RNQ3kKmZD7vMPJDSrwWWOr8CJjy4MOCpGUyAyw8k04M=
//主机密钥存储在SSH服务器上的 /etc/ssh/ssh_host_key* 中 // .pub代表公钥
[root@localhost ~]# ls /etc/ssh/*key*
/etc/ssh/ssh_host_ecdsa_key /etc/ssh/ssh_host_ed25519_key.pub
/etc/ssh/ssh_host_ecdsa_key.pub /etc/ssh/ssh_host_rsa_key
/etc/ssh/ssh_host_ed25519_key /etc/ssh/ssh_host_rsa_key.pub
服务器端:
[root@localhost ssh]# cat /etc/ssh/ssh_host_ecdsa_key.pub
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFtvQzKVagWukVUwPOghxDwsP4YC9DSHNCSlDkpfCpL0RNQ3kKmZD7vMPJDSrwWWOr8CJjy4MOCpGUyAyw8k04M=
用ssh-keygen
命令生成密码。将会生成私钥~/.ssh/id_rsa
和公钥~/.ssh/id_rsa.pub
。
1.生成密钥对
[root@scl ~]# ssh-keygen -t rsa // -t 指定rsa算法
Bad escape character 'ygen'.
[root@scl ~]# ssh-keygen -t rsa
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:ocnMTYDpPy4ylzMR1iiOrRqqDy3aZoSclHg1bDjLHJ4 root@scl
The key's randomart image is:
+---[RSA 2048]----+
| o o. |
| + B . |
|.+.O + o |
|.oE =+.= . |
|o*.o o* S |
|o++ . o |
|+o. + . |
|+== * . |
|B+o+ + |
+----[SHA256]-----+
[root@scl ~]#
2.把公钥发送至想登录的主机上
通过
ssh-copy-id
将密钥复制到另一系统时,它默认复制~/.ssh/id_rsa.pub
文件
如果你的公钥不是~/.ssh/id_rsa.pub
则要使用-i
指定,此时使用ssh连接时也要使用-i
指定公钥
[root@scl ~]# ls .ssh/
id_rsa id_rsa.pub known_hosts
[root@scl ~]# ssh-copy-id [email protected]
/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
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
//将主机端的公钥 ~/.ssh/id_rsa.pub 复制一份到服务器端生成一个文件authorized_keys,并且这个文件的权限必须是600
[root@localhost ~]# ls .ssh/
authorized_keys known_hosts
3.验证
注意事项:
// 所有公钥的权限必须是644
// 所有私钥的权限必须是600
// authorized_keys文件的权限必须是600
// .ssh这个目录的权限必须是700
[root@scl .ssh]# ssh [email protected] //此时再次连接对方主机不用输入密码
Last login: Thu Jun 27 14:07:36 2019 from 192.168.100.1
[root@localhost ~]#
scp命令
//使用scp命令可以复制本机的公钥至对方主机:
[root@scl ~]# scp ./.ssh/id_rsa.pub [email protected]:/root/.ssh/authorized_keys
//使用scp从对方主机下载文件至本主机:
[root@scl ~]# ls
anaconda-ks.cfg
[root@scl ~]# rm -f anaconda-ks.cfg
[root@scl ~]#
[root@scl ~]# scp [email protected]:/root/anaconda-ks.cfg .
anaconda-ks.cfg 100% 1257 25.3KB/s 00:00
[root@scl ~]# ls
anaconda-ks.cfg
SSH
的配置文件在/etc/ssh/sshd_config
PermitRootLogin {yes|no} //是否允许root用户远程登录系统
PermitRootLogin without-password //仅允许root用户基于密钥方式远程登录
PasswordAuthentication {yes|no} //是否启用密码身份验证,默认开启
尽量使用以下配置: