OpenSSH
这一术语指系统中使用的Secure Shell
软件的软件实施。用于在远程系统上安全运行shell
。如果您在可提供ssh
服务的远程Linux
系统中拥有用户帐户,则ssh
是通常用来远程登录到该系统的命令。ssh
命令也可用于在远程系统中运行命令。
常见的远程登录工具有:
telnet //远程登录协议,23/TCP
认证明文
数据传输明文
ssh //Secure SHell,应用层协议,22/TCP
通信过程及认证过程是加密的,主机认证
用户认证过程加密
数据传输过程加密
dropbear //嵌入式系统专用的SSH服务器端和客户端工具
openssh有两个版本,分别为v1和v2,其特点如下:
v1:基于CRC-32做MAC,无法防范中间人(man-in-middle)攻击
v2:双方主机协议选择安全的MAC方式。基于DH算法做密钥交换,基于RSA或DSA算法实现身份认证
openssh
有两种认证方式,分别是:
openSSH
是基于C/S架构工作的。
服务器端 //sshd,配置文件在/etc/ssh/sshd_config
客户端 //ssh,配置文件在/etc/ssh/ssh_config
ssh-keygen //密钥生成器
ssh-copy-id //将公钥传输至远程服务器
scp //跨主机安全复制工具
//以当前用户身份创建远程交互式shell,然后在结束时使用exit命令返回到之前的shell
[root@longnian ~]# ssh 192.168.159.132
The authenticity of host '192.168.159.132 (192.168.159.132)' can't be established.
ECDSA key fingerprint is 03:18:89:32:f3:e7:3e:8b:44:2a:9c:ed:0a:8f:53:6f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.159.132' (ECDSA) to the list of known hosts.
[email protected]'s password:
Last login: Sun Apr 26 19:33:29 2020 from 192.168.159.1
[root@longnian ~]# exit
登出
Connection to 192.168.159.132 closed.
//以其他用户身份(remoteuser)在选定主机(remotehost)上连接到远程`shell`
[root@longnian ~]# ssh [email protected]
[email protected]'s password:
Last failed login: Sun Apr 26 19:39:01 CST 2020 from 192.168.159.131 on ssh:notty
There were 4 failed login attempts since the last successful login.
[user1@longnian ~]$ exit
登出
Connection to 192.168.159.132 closed.
//以远程用户身份(remoteuser)在远程主机(remotehost)上通过将输出返回到本地显示器的方式来执行单一命令
[root@longnian ~]# ip a s eno16777736
2: eno16777736: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:81:ca:5b brd ff:ff:ff:ff:ff:ff
inet 192.168.159.131/24 brd 192.168.159.255 scope global dynamic eno16777736
valid_lft 1765sec preferred_lft 1765sec
inet6 fe80::20c:29ff:fe81:ca5b/64 scope link
valid_lft forever preferred_lft forever
[root@longnian ~]# ssh [email protected] '/usr/sbin/ip a s eno16777736'
[email protected]'s password:
2: eno16777736: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:4a:7e:86 brd ff:ff:ff:ff:ff:ff
inet 192.168.159.132/24 brd 192.168.159.255 scope global dynamic eno16777736
valid_lft 1754sec preferred_lft 1754sec
inet6 fe80::20c:29ff:fe4a:7e86/64 scope link
valid_lft forever preferred_lft forever
//w命令可以显示当前登录到计算机的用户列表。这对于显示哪些用户使用ssh从哪些远程位置进行了登录以及执行了何种操作等内容特别有用
[root@longnian ~]# w
19:48:16 up 16 min, 3 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 19:32 15:20 0.08s 0.08s -bash
root pts/0 192.168.159.1 19:33 0.00s 0.02s 0.01s w
user1 pts/1 192.168.159.131 19:48 4.00s 0.00s 0.00s -bash
ssh
通过公钥加密的方式保持通信安全。当某一ssh
客户端连接到ssh服务器时,在该客户端登录之前,服务器会向其发送公钥副本。这可用于为通信渠道设置安全加密,并可验证客户端的服务器。
当用户第一次使用ssh
连接到特定服务器时,ssh
命令可在用户的~/.ssh/known_hosts
文件中存储该服务器的公钥。在此之后每当用户进行连接时,客户端都会通过对比~/.ssh/known_hosts
文件中的服务器条目和服务器发送的公钥,确保从服务器获得相同的公钥。如果公钥不匹配,客户端会假定网络通信已遭劫持或服务器已被入侵,并且中断连接。
这意味着,如果服务器的公钥发生更改(由于硬盘出现故障导致公钥丢失,或者出于某些正当理由替换公钥),用户则需要更新其~/.ssh/known_hosts
文件并删除旧的条目才能够进行登录。
//主机ID存储在本地客户端系统上的 ~/.ssh/known_hosts 中
[root@localhost ~]# cat ~/.ssh/known_hosts
172.16.12.138 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBHpBYg+C0GDiBU9mHsy8S3ju31OdfTq6cr6oprIsE/MM8yZdTrRh4gum8IXiVFchUelPD5R9IuTjsy8Eqy8l+Lc=
//主机密钥存储在SSH服务器上的 /etc/ssh/ssh_host_key* 中
[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
用户可通过使用公钥身份验证进行ssh
登录身份验证。ssh
允许用户使用私钥-公钥方案进行身份验证。这意味着将生成私钥和公钥这两个密钥。私钥文件用作身份验证凭据,像密码一样,必须妥善保管。公钥复制到用户希望登录的系统,用于验证私钥。公钥并不需要保密。拥有公钥的ssh服务器可以发布仅持有您私钥的系统才可解答的问题。因此,可以根据所持有的密钥进行验证。如此一来,就不必在每次访问系统时键入密码,但安全性仍能得到保证。
使用ssh-keygen
命令生成密码。将会生成私钥~/.ssh/id_rsa
和公钥~/.ssh/id_rsa.pub
。
注意:
生成密钥时,系统将提供指定密码的选项,在访问私钥时必须提供该密码。如果私钥被偷,除颁发者之外的其他任何人很难使用该私钥,因为已使用密码对其进行保护。这样,在攻击者破解并使用私钥前,会有足够的时间生成新的密钥对并删除所有涉及旧密钥的内容。
生成ssh
密钥后,密钥将默认存储在家目录下的.ssh/
目录中。私钥和公钥的权限就分别为600
和644
。.ssh
目录权限必须是700
。
在可以使用基于密钥的身份验证前,需要将公钥复制到目标系统上。可以使用ssh-copy-id
完成这一操作
[root@longnian ~]# ssh-copy-id remoteuser@remotehost
通过ssh-copy-id
将密钥复制到另一系统时,它默认复制~/.ssh/id_rsa.pub
文件
//SSH密钥演示
//使用 ssh-keygen 创建公钥-私钥对
[root@longnian ~]# 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:
a0:e9:a1:72:6a:80:38:f5:5e:f1:73:e3:63:38:e8:a8 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| . |
| . o.. |
|o. .+ oS |
|= o... o o |
|o.o... . = . |
| = ... o + |
|o E.... o . |
+-----------------+
//使用 ssh-copy-id 将公钥复制到远程系统上的正确位置
[root@longnian ~]# ls .ssh/
id_rsa id_rsa.pub known_hosts
[root@longnian ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
/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 命令无命令登录远程主机
[root@longnian ~]# ssh [email protected]
Last login: Sun Apr 26 19:36:10 2020 from 192.168.159.131
[root@longnian ~]# ip a s eno16777736
2: eno16777736: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:4a:7e:86 brd ff:ff:ff:ff:ff:ff
inet 192.168.159.132/24 brd 192.168.159.255 scope global dynamic eno16777736
valid_lft 1191sec preferred_lft 1191sec
inet6 fe80::20c:29ff:fe4a:7e86/64 scope link
valid_lft forever preferred_lft forever
//使用 scp 命令传送文件到远程主机
[root@longnian ~]# scp test [email protected]:/tmp
The authenticity of host '192.168.159.132 (192.168.159.132)' can't be established.
ECDSA key fingerprint is 03:18:89:32:f3:e7:3e:8b:44:2a:9c:ed:0a:8f:53:6f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.159.132' (ECDSA) to the list of known hosts.
[email protected]'s password:
test 100% 24 0.0KB/s 00:00
//使用 scp 命令从远程主机上下载文件到本地
[root@longnian ~]# ls
anaconda-ks.cfg
[root@longnian ~]# scp test [email protected]:/tmp/test
[email protected]'s password:
test 100% 28 0.0KB/s 00:00
[root@longnian ~]# ls
anaconda-ks.cfg test
//scp命令常用选项
-r //递归复制
-p //保持权限
-P //端口
-q //静默模式
-a //全部复制
虽然OpenSSH
服务器通常无需修改,但会提供其他安全措施,可以在配置文件/etc/ssh/sshd_config
中修改OpenSSH
服务器的各个方面。
PermitRootLogin {yes|no} //是否允许root用户远程登录系统
PermitRootLogin without-password //仅允许root用户基于密钥方式远程登录
PasswordAuthentication {yes|no} //是否启用密码身份验证,默认开启
[root@longnian ~]# tr -dc A-Za-z0-9_ < /dev/urandom | head -c 30 |xargs
UCmwaflLEyZq2ukYbWwNqy_0OTCMO6 //生成30位的密码
[root@longnian ~]# openssl rand 20 -base64
KHTEmiAfna8pQdj2Fy4zLOmOX7g= //生成20位随机密码