交流群:69235620,有不同的问题或意见可以在群里讨论,也可以私下和我聊天 qq:1251611916
文章目录
-
- 1. 使用 SSH 远程命令行访问
-
- 1.1 OpenSSH 简介
- 1.2 SSH 认证方式
- 1.3 openSSH 的工作模式
- 1.4 Secure Shell 示例
- 1.5 SSH 主机密钥
- 2. 配置基于 SSH 密钥身份验证
- 3. 自定义 SSH 服务配置
- 4. SSH 安全注意事项
- 作业
1. 使用 SSH 远程命令行访问
1.1 OpenSSH 简介
OpenSSH
该术语是指系统中使用的术语Secure Shell
软件实施。用于远程系统的安全运行shell
。假如你可以提供ssh
服务的远程Linux
如果系统中有用户账户,ssh
它通常用于远程登录系统的命令。ssh
命令也可用于在远程系统中运行。
常见的远程登录工具有:
- telnet
- ssh
- dropbear
telnet ///远程登录协议,23/TCP 认证明文 数据传输明文 ssh //Secure SHell,22/TCP 加密通信和认证过程,主机认证 加密用户认证过程 数据传输过程加密 dropbear ////专用于嵌入式系统SSH服务器端和客户端工具
1.2 SSH 认证方式
openssh
认证方法有两种:
- 基于密码认证
- 基于密钥认证
1.3 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,最后使用exit命令回到以前shell [root@localhost ~]# ssh 172.16.12.138 root@172.16.12.138's password: Last login: Tue Jul 10 07:34:03 2018 from 172.16.12.136 [root@localhost ~]# exit logout Connection to 172.16.12.138 closed. //作为其他用户(remoteuser)在选定主机(remotehost)连接到远程`shell` [root@localhost ~]# ssh user1@172.16.12.138 user1@172.16.12.138's password: [user1@localhost ~]$ exit logout Connection to 172.16.12.138 closed. //作为远程用户(remoteuser)在远程主机(remotehost)将输出返回本地
显示器执行单一命令 [root@localhost ~]# ip a s ens33 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:0e:70:22 brd ff:ff:ff:ff:ff:ff inet 172.16.12.136/24 brd 172.16.12.255 scope global dynamic ens33 valid_lft 1422sec preferred_lft 1422sec inet6 fe80::20c:29ff:fe0e:7022/64 scope link valid_lft forever preferred_lft forever [root@localhost ~]# ssh user1@172.16.12.138 '/usr/sbin/ip a s ens33' user1@172.16.12.138's password: 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:f4:5b:87 brd ff:ff:ff:ff:ff:ff inet 172.16.12.138/24 brd 172.16.12.255 scope global dynamic ens33 valid_lft 1666sec preferred_lft 1666sec inet6 fe80::20c:29ff:fef4:5b87/64 scope link valid_lft forever preferred_lft forever //w命令可以显示当前登录计算机的用户列表。这显示了哪些用户使用它ssh登录哪些远程位置,执行哪些操作特别有用 [root@localhost ~]# w 07:49:18 up 18 min, 2 users, load average: 0.02, 0.02, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 172.16.12.1 07:31 6.00s 0.02s 0.00s w user1 pts/1 172.16.12.136 07:49 5.00s 0.00s 0.00s -bash
1.5 SSH 主机密钥
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
2. 配置基于 SSH 密钥身份验证/h2> 用户可通过使用公钥身份验证进行ssh
登录身份验证。ssh
允许用户使用私钥-公钥方案进行身份验证。这意味着将生成私钥和公钥这两个密钥。私钥文件用作身份验证凭据,像密码一样,必须妥善保管。公钥复制到用户希望登录的系统,用于验证私钥。公钥并不需要保密。拥有公钥的ssh
服务器可以发布仅持有您私钥的系统才可解答的问题。因此,可以根据所持有的密钥进行验证。如此一来,就不必在每次访问系统时键入密码,但安全性仍能得到保证。
使用ssh-keygen
命令生成密码。将会生成私钥~/.ssh/id_rsa
和公钥~/.ssh/id_rsa.pub
。
生成密钥时,系统将提供指定密码的选项,在访问私钥时必须提供该密码。如果私钥被偷,除颁发者之外的其他任何人很难使用该私钥,因为已使用密码对其进行保护。这样,在攻击者破解并使用私钥前,会有足够的时间生成新的密钥对并删除所有涉及旧密钥的内容。
生成ssh
密钥后,密钥将默认存储在家目录下的.ssh/
目录中。私钥和公钥的权限就分别为600
和644
。.ssh
目录权限必须是700
。
在可以使用基于密钥的身份验证前,需要将公钥复制到目标系统上。可以使用ssh-copy-id
完成这一操作
[root@localhost ~]# ssh-copy-id remoteuser@remotehost
通过ssh-copy-id
将密钥复制到另一系统时,它默认复制~/.ssh/id_rsa.pub
文件
//SSH密钥演示
//使用 ssh-keygen 创建公钥-私钥对
[root@localhost ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/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:8VyY3c3UEQvk1Pn95tYIF7sx9enlwG78hDjlX0entN0 root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| .+.o*|
| +o..*o|
| . o o...=|
| + . . +|
| S o .o+*|
| .=BBB|
| o=*XE|
| .*+B|
| . oo|
+----[SHA256]-----+
//使用 ssh-copy-id 将公钥复制到远程系统上的正确位置
[root@localhost ~]# ls .ssh/
id_rsa id_rsa.pub
[root@localhost ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.12.138
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '172.16.12.138 (172.16.12.138)' can't be established.
ECDSA key fingerprint is SHA256:JK5WwrX8hynl3dyWO43e6+lcs6zn9oZn74z1H5X8F90.
ECDSA key fingerprint is MD5:01:4f:4f:4b:0e:45:a9:10:bb:d0:c0:dd:19:9a:9f:96.
Are you sure you want to continue connecting (yes/no)? yes
/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@172.16.12.138's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@172.16.12.138'"
and check to make sure that only the key(s) you wanted were added.
//使用 ssh 命令无命令登录远程主机
[root@localhost ~]# ssh root@172.16.12.138
Last login: Tue Jul 10 18:37:51 2018 from 172.16.12.1
[root@localhost ~]# ip a s ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:f4:5b:87 brd ff:ff:ff:ff:ff:ff
inet 172.16.12.138/24 brd 172.16.12.255 scope global dynamic ens33
valid_lft 1377sec preferred_lft 1377sec
inet6 fe80::20c:29ff:fef4:5b87/64 scope link
valid_lft forever preferred_lft forever
//使用 scp 命令传送文件到远程主机
[root@localhost ~]# scp test.sh root@172.16.12.138:/tmp
root@172.16.12.138's password:
test.sh 100% 45 29.8KB/s 00:00
//使用 scp 命令从远程主机上下载文件到本地
[root@localhost ~]# ls
a anaconda-ks.cfg b nginx-1.12.2 nginx-1.12.2.tar.gz nohup.out outfile test.sh
[root@localhost ~]# rm -f test.sh
[root@localhost ~]# ls
a anaconda-ks.cfg b nginx-1.12.2 nginx-1.12.2.tar.gz nohup.out outfile
[root@localhost ~]# scp root@172.16.12.138:/tmp/test.sh .
root@172.16.12.138's password:
test.sh 100% 45 39.1KB/s 00:00
[root@localhost ~]# ls
a anaconda-ks.cfg b nginx-1.12.2 nginx-1.12.2.tar.gz nohup.out outfile test.sh
//scp命令常用选项
-r //递归复制
-p //保持权限
-P //端口
-q //静默模式
-a //全部复制
ssh
登录身份验证。ssh
允许用户使用私钥-公钥方案进行身份验证。这意味着将生成私钥和公钥这两个密钥。私钥文件用作身份验证凭据,像密码一样,必须妥善保管。公钥复制到用户希望登录的系统,用于验证私钥。公钥并不需要保密。拥有公钥的ssh
服务器可以发布仅持有您私钥的系统才可解答的问题。因此,可以根据所持有的密钥进行验证。如此一来,就不必在每次访问系统时键入密码,但安全性仍能得到保证。ssh-keygen
命令生成密码。将会生成私钥~/.ssh/id_rsa
和公钥~/.ssh/id_rsa.pub
。生成密钥时,系统将提供指定密码的选项,在访问私钥时必须提供该密码。如果私钥被偷,除颁发者之外的其他任何人很难使用该私钥,因为已使用密码对其进行保护。这样,在攻击者破解并使用私钥前,会有足够的时间生成新的密钥对并删除所有涉及旧密钥的内容。
ssh
密钥后,密钥将默认存储在家目录下的.ssh/
目录中。私钥和公钥的权限就分别为600
和644
。.ssh
目录权限必须是700
。ssh-copy-id
完成这一操作[root@localhost ~]# ssh-copy-id remoteuser@remotehost
ssh-copy-id
将密钥复制到另一系统时,它默认复制~/.ssh/id_rsa.pub
文件//SSH密钥演示
//使用 ssh-keygen 创建公钥-私钥对
[root@localhost ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/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:8VyY3c3UEQvk1Pn95tYIF7sx9enlwG78hDjlX0entN0 root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| .+.o*|
| +o..*o|
| . o o...=|
| + . . +|
| S o .o+*|
| .=BBB|
| o=*XE|
| .*+B|
| . oo|
+----[SHA256]-----+
//使用 ssh-copy-id 将公钥复制到远程系统上的正确位置
[root@localhost ~]# ls .ssh/
id_rsa id_rsa.pub
[root@localhost ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.12.138
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '172.16.12.138 (172.16.12.138)' can't be established.
ECDSA key fingerprint is SHA256:JK5WwrX8hynl3dyWO43e6+lcs6zn9oZn74z1H5X8F90.
ECDSA key fingerprint is MD5:01:4f:4f:4b:0e:45:a9:10:bb:d0:c0:dd:19:9a:9f:96.
Are you sure you want to continue connecting (yes/no)? yes
/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@172.16.12.138's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@172.16.12.138'"
and check to make sure that only the key(s) you wanted were added.
//使用 ssh 命令无命令登录远程主机
[root@localhost ~]# ssh root@172.16.12.138
Last login: Tue Jul 10 18:37:51 2018 from 172.16.12.1
[root@localhost ~]# ip a s ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:f4:5b:87 brd ff:ff:ff:ff:ff:ff
inet 172.16.12.138/24 brd 172.16.12.255 scope global dynamic ens33
valid_lft 1377sec preferred_lft 1377sec
inet6 fe80::20c:29ff:fef4:5b87/64 scope link
valid_lft forever preferred_lft forever
//使用 scp 命令传送文件到远程主机
[root@localhost ~]# scp test.sh root@172.16.12.138:/tmp
root@172.16.12.138's password:
test.sh 100% 45 29.8KB/s 00:00
//使用 scp 命令从远程主机上下载文件到本地
[root@localhost ~]# ls
a anaconda-ks.cfg b nginx-1.12.2 nginx-1.12.2.tar.gz nohup.out outfile test.sh
[root@localhost ~]# rm -f test.sh
[root@localhost ~]# ls
a anaconda-ks.cfg b nginx-1.12.2 nginx-1.12.2.tar.gz nohup.out outfile
[root@localhost ~]# scp root@172.16.12.138:/tmp/test.sh .
root@172.16.12.138's password:
test.sh 100% 45 39.1KB/s 00:00
[root@localhost ~]# ls
a anaconda-ks.cfg b nginx-1.12.2 nginx-1.12.2.tar.gz nohup.out outfile test.sh
//scp命令常用选项
-r //递归复制
-p //保持权限
-P //端口
-q //静默模式
-a //全部复制
3. 自定义 SSH 服务配置
虽然OpenSSH
服务器通常无需修改,但会提供其他安全措施,可以在配置文件/etc/ssh/sshd_config
中修改OpenSSH
服务器的各个方面。
PermitRootLogin {yes|no} //是否允许root用户远程登录系统
PermitRootLogin without-password //仅允许root用户基于密钥方式远程登录
PasswordAuthentication {yes|no} //是否启用密码身份验证,默认开启
4. SSH 安全注意事项
- 密码应该经常换且足够复杂
[root@localhost ~]# tr -dc A-Za-z0-9_ < /dev/urandom | head -c 30 |xargs //生成30位的密码
LYH9cbirdT6E_hbColMFjZNf9Kd6If
[root@localhost ~]# openssl rand 20 -base64
Di9ry+dyV40xVvBHirsc3XpBOzg= //生成20位随机密码
- 使用非默认端口
- 限制登录客户端地址
- 仅监听特定的IP地址
- 禁止管理员直接登录
- 仅允许有限制用户登录
- AllowUsers
- AllowGroups
- 使用基于密钥的认证
- 禁止使用空密码
- 禁止使用SSHv1版本
- 设定空闲会话超时时长
- 利用防火墙设置ssh访问策略
- 限制ssh的访问频度和并发在线数
- 做好日志的备份,经常分析(集中于某台服务器)
作业
1.说明密钥认证的过程 2.手动配置密钥认证登录