openssh服务
文章目录
1. 使用 SSH 访问远程命令行
1.1 OpenSSH 简介
OpenSSH 是 SSH (Secure SHell) 协议的免费开源实现。SSH协议族可以用来进行远程控制, 或在计算机之间传送文件。而实现此功能的传统方式,如telnet(终端仿真协议)、 rcp ftp、 rlogin、rsh都是极为不安全的,并且会使用明文传送密码。OpenSSH提供了服务端后台程序和客户端工具,用来加密远程控件和文件传输过程中的数据,并由此来代替原来的类似服务。
常见的远程登录工具有:
telnet //远程登录协议,23/TCP
认证明文
数据传输明文
ssh //Secure SHell,应用层协议,22/TCP
通信过程及认证过程是加密的,主机认证
用户认证过程加密
数据传输过程加密
dropbear //嵌入式系统专用的SSH服务器端和客户端工具
1.2 SSH 版本
OpenSSH 支持 SSH 协议的版本 1.3、1.5、和 2。自从 OpenSSH 的版本2.9以来,默认的协议是版本2,该协议默认使用 RSA 钥匙。
1.3 SSH 认证方式
SSH登录方式主要分为两种:
- 用户名密码验证方式
- 基于密钥的登录方式
1.4 openSSH 的工作模式
openSSH是基于C/S架构工作的。
服务器端 //sshd,配置文件在/etc/ssh/sshd_config
客户端 //ssh,配置文件在/etc/ssh/ssh_config
ssh-keygen //密钥生成器
ssh-copy-id //将公钥传输至远程服务器
scp //跨主机安全复制工具
1.4 Secure Shell 示例
// 以当前用户身份创建远程交互式shell
[root@wyz ~]# ssh 192.168.234.22
The authenticity of host '192.168.234.22 (192.168.234.22)' can't be established.
ECDSA key fingerprint is SHA256:vP8gUnfn0QD4z743XkBHDh3XH6sllfn3cYhT3cHXM7w.
ECDSA key fingerprint is MD5:f2:02:fb:f7:8d:02:47:6f:dd:49:fe:d4:6e:26:bd:3a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.234.22' (ECDSA) to the list of known hosts.
root@192.168.234.22's password:
Last login: Mon Apr 1 14:15:44 2019 from 192.168.234.1
[root@localhost ~]#
[root@localhost ~]# exit
logout
Connection to 192.168.234.22 closed.
[root@wyz ~]#
//以其他用户身份(remoteuser)在选定主机(remotehost)上连接到远程`shell`
[root@wyz ~]# ssh wyz@192.168.234.22
wyz@192.168.234.22's password:
[wyz@localhost ~]$ whoami
wyz
[wyz@localhost ~]$ exit
logout
Connection to 192.168.234.22 closed.
[root@wyz ~]#
//以远程用户身份(remoteuser)在远程主机(remotehost)上通过将输出返回到本地显示器的方式来执行单一命令
1.5 SSH 主机密钥
ssh通过公钥加密的方式保持通信安全。当某一ssh客户端连接到ssh服务器时,在该客户端登录之前,服务器会向其发送公钥副本。这可用于为通信渠道设置安全加密,并可验证客户端的服务器。
当用户第一次使用ssh连接到特定服务器时,ssh命令可在用户的/.ssh/known_hosts文件中存储该服务器的公钥。在此之后每当用户进行连接时,客户端都会通过对比/.ssh/known_hosts文件中的服务器条目和服务器发送的公钥,确保从服务器获得相同的公钥。如果公钥不匹配,客户端会假定网络通信已遭劫持或服务器已被入侵,并且中断连接。
这意味着,如果服务器的公钥发生更改(由于硬盘出现故障导致公钥丢失,或者出于某些正当理由替换公钥),用户则需要更新其~/.ssh/known_hosts文件并删除旧的条目才能够进行登录。
//主机ID存储在本地客户端系统上的 ~/.ssh/known_hosts 中
[root@wyz ~]# cat .ssh/known_hosts
192.168.234.22 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBE/kWcb/hu2wnVdm3Hy9yyu3zsmW3php2Zp7F3oGj2371IsDK0lX5iC
HUaH7/i5vqrbY6IOSHQuleZwWHShh/A=
[root@wyz ~]#
//主机密钥存储在SSH服务器上的 /etc/ssh/ssh_host_key* 中
[root@wyz ~]# ls /etc/ssh/*key*
/etc/ssh/ssh_host_ecdsa_key /etc/ssh/ssh_host_ed25519_key /etc/ssh/ssh_host_rsa_key
/etc/ssh/ssh_host_ecdsa_key.pub /etc/ssh/ssh_host_ed25519_key.pub /etc/ssh/ssh_host_rsa_key.pub
[root@wyz ~]#
2. 配置基于 SSH 密钥的身份验证
用户可通过使用公钥身份验证进行ssh登录身份验证。ssh允许用户使用私钥-公钥方案进行身份验证。这意味着将生成私钥和公钥这两个密钥。私钥文件用作身份验证凭据,像密码一样,必须妥善保管。公钥复制到用户希望登录的系统,用于验证私钥。公钥并不需要保密。拥有公钥的ssh服务器可以发布仅持有您私钥的系统才可解答的问题。因此,可以根据所持有的密钥进行验证。如此一来,就不必在每次访问系统时键入密码,但安全性仍能得到保证。
使用ssh-keygen命令生成密码。将会生成私钥/.ssh/id_rsa和公钥/.ssh/id_rsa.pub。
注意:
生成密钥时,系统将提供指定密码的选项,在访问私钥时必须提供该密码。如果私钥被偷,除颁发者之外的其他任何人很难使用该私钥,因为已使用密码对其进行保护。这样,在攻击者破解并使用私钥前,会有足够的时间生成新的密钥对并删除所有涉及旧密钥的内容。
- 生成ssh密钥后,密钥将默认存储在家目录下的.ssh/目录中。私钥和公钥的权限就分别为600和644。.ssh目录权限必须是700。
在可以使用基于密钥的身份验证前,需要将公钥复制到目标系统上。可以使用ssh-copy-id完成这一操作
//语法 : ssh-copy-id remoteuser@remotehos
通过ssh-copy-id将密钥复制到另一系统时,它默认复制~/.ssh/id_rsa.pub文件
ssh-keygen -t rsa //指定秘钥的算法
[root@wyz ~]# 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:4J4MEZdSvdHctxF3YF8hhQiI6sU1Am3FNkwIcBZS4uM root@wyz
The key's randomart image is:
+---[RSA 2048]----+
| +oB*.X=.+....*==|
|. = .O.Oo o..o+oo|
| o =.= oo . o.|
|. .. = .. . |
| E. o . S |
| . + . |
| + |
| |
| |
+----[SHA256]-----+
[root@wyz ~]#
[root@wyz ~]# ls .ssh/
id_rsa id_rsa.pub known_hosts
[root@wyz ~]#
// 通过`ssh-copy-id`将密钥复制到另一系统时,它默认复制`~/.ssh/id_rsa.pub`文件
[root@wyz ~]# ssh-copy-id root@192.168.234.22
/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.234.22's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.234.22'"
and check to make sure that only the key(s) you wanted were added.
// 会在目标主机的 ~/.ssh/目录下生成authorized_keys文件
[root@wyz1 ~]# ls .ssh/
authorized_keys
[root@wyz1 ~]#
//使用 ssh 命令无远程主机密码登录远程主机
[root@wyz ~]# ssh root@192.168.234.22
Last login: Mon Apr 1 15:07:32 2019 from 192.168.234.11
[root@localhost ~]#
//使用 scp 命令传送文件到远程主机
//语法:scp [选项] <上传文件的绝对路径> <remoteuser@remotehos:存放在哪个目录下>
//scp命令常用选项
-r //递归复制 常用选项,用来上传和下载目录
-p //保持权限
-P //端口
-q //静默模式
-a //全部复制
[root@wyz ~]# mkdir wyz
[root@wyz ~]# touch wyz/{a,b,c,d}
[root@wyz ~]# ls wyz/
a b c d
[root@wyz ~]# scp -r /root/wyz/ root@192.168.234.22:/root/
a 100% 0 0.0KB/s 00:00
b 100% 0 0.0KB/s 00:00
c 100% 0 0.0KB/s 00:00
d 100% 0 0.0KB/s 00:00
[root@wyz ~]#
//目标主机
[root@wyz1 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg wyz
[root@wyz1 ~]# ls wyz/
a b c d
[root@wyz1 ~]#
//从目标主机下载文件是将顺序颠倒,将要下载的文件或目录放在前面
//语法:scp [选项] <remoteuser@remotehos:目标文件的绝对路径> <存放文件的绝对路径>
3. 自定义 SSH 服务配置
虽然OpenSSH服务器通常无需修改,但会提供其他安全措施,可以在配置文件/etc/ssh/sshd_config中修改OpenSSH服务器的各个方面
PermitRootLogin {yes|no} //是否允许root用户远程登录系统
PermitRootLogin without-password //仅允许root用户基于密钥方式远程登录
PasswordAuthentication {yes|no} //是否启用密码身份验证,默认开启
4. SSH 安全注意事项
- 密码应该经常换且足够复杂
[root@wyz ~]# openssl rand 30 -base64
N+JQ4G9PgZArnr2+08rRmIgQZNN1K9IAe9iSvjM3
[root@wyz ~]#
- 使用非默认端口
- 限制登录客户端地址
- 仅监听特定的IP地址
- 禁止管理员直接登录
- 仅允许有限制用户登录
- AllowUsers
- AllowGroups
- 使用基于密钥的认证
- 禁止使用空密码
- 禁止使用SSHv1版本
- 设定空闲会话超时时长
- 利用防火墙设置ssh访问策略
- 限制ssh的访问频度和并发在线数
- 做好日志的备份,经常分析(集中于某台服务器)
示例
1.说明密钥认证的过程
1.会话密钥(session key)生成
客户端请求连接服务器,服务器将 ’服务器公钥’ 发送给客户端。
服务器生成会话ID(session id),设为 p,发送给客户端。
客户端生成会话密钥(session key),设为 q,并计算 r = p xor q。
客户端将 r 用 ‘服务器公钥’ 进行加密,结果发送给服务器。
服务器用 ‘服务器密钥’ 进行解密,获得 r。
服务器进行 r xor p 的运算,获得 q。
至此服务器和客户端都知道了会话密钥q,以后的传输都将被 q 加密。
2.认证
服务器生成随机数 x,并用 ‘客户端公钥’ 加密后生成结果 S(x),发送给客户端
客户端使用 ‘客户端密钥’ 解密 S(x) 得到 x
客户端计算 q + x 的 md5 值 n(q+x),q为上一步得到的会话密钥
服务器计算 q + x 的 md5 值 m(q+x)
客户端将 n(q+x) 发送给服务器
服务器比较 m(q+x) 和 n(q+x),两者相同则认证成功
2.手动配置密钥认证登录
[root@wyz ~]# 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:4J4MEZdSvdHctxF3YF8hhQiI6sU1Am3FNkwIcBZS4uM root@wyz
The key's randomart image is:
+---[RSA 2048]----+
| +oB*.X=.+....*==|
|. = .O.Oo o..o+oo|
| o =.= oo . o.|
|. .. = .. . |
| E. o . S |
| . + . |
| + |
| |
| |
+----[SHA256]-----+
[root@wyz ~]#
[root@wyz ~]# ls .ssh/
id_rsa id_rsa.pub known_hosts
[root@wyz ~]#
[root@wyz ~]# ssh-copy-id root@192.168.234.22
/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.234.22's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.234.22'"
and check to make sure that only the key(s) you wanted were added.
// 会在目标主机的 ~/.ssh/目录下生成authorized_keys文件
[root@wyz1 ~]# ls .ssh/
authorized_keys
[root@wyz1 ~]#
//使用 ssh 命令无远程主机密码登录远程主机
[root@wyz ~]# ssh root@192.168.234.22
Last login: Mon Apr 1 15:07:32 2019 from 192.168.234.11
[root@localhost ~]#