openSSH服务
1.使用SSH访问远程命令行
1.1 OpenSSH简介
openssh这一术语指系统中使用的Secure Shell软件的软件实施。用于在远程系统上安全运行shell。如果您在可提供ssh服务的远程Linux系统中拥护用户账户,则ssh是通常用来远程登录到该系统的命令。ssh命令也可用于在远程系统中运行命令。
1.2 SSH版本
openssh有两个版本,分别为v1和v2,其特点如下:
v1:基于CRC-32做MAC,无法防范中间人(man-in-middle)攻击
v2:双方主机协议选择安全的MAC方式。基于DH算法做密钥交换,基于RSA或DSA算法实现身份认证
1.3 SSH认证方式
openssh有两种人证方式,分别是:
- 基于口令认证
- 基于秘钥认证
1.4 openSSH的工作模式
-
openSSH是基于C/S架构工作的
服务器端 //sshd,配置文件在/etc/ssh/sshd_config 客户端 //ssh,配置文件在/etc/ssh/ssh_config ssh-keygen //密钥生成器 ssh-copy-id //将公钥传输至远程服务器 scp //跨主机安全复制工具
1.5 Secure Shell示例
- //以当前用户身份创建远程交互式shell,然后在结束时使用exit命令返回到之前的shell
[root@xaii33 ~]# ssh 192.168.157.19
The authenticity of host '192.168.157.19 (192.168.157.19)' can't be established.
ECDSA key fingerprint is SHA256:iwNXVlPVQkxYNIHIsmxGb8ZDyFzcpv1PYWJtdL1EOgU.
ECDSA key fingerprint is MD5:78:c4:1d:c0:d8:9b:ed:7e:60:cc:d0:b8:0c:f2:b5:45.
Are you sure you want to continue connecting (yes/no)? yes
root@192.168.157.19's password:
Last login: Mon Jan 7 23:44:42 2019 from 192.168.157.2
[root@xaii19 ~]# ip a
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:3e:79:c2 brd ff:ff:ff:ff:ff:ff
inet 192.168.157.19/24 brd 192.168.157.255 scope global ens32
valid_lft forever preferred_lft forever
inet6 fe80::187f:35f2:c5b8:5665/64 scope link
valid_lft forever preferred_lft forever
//以远程用户身份(remoteuser)在远程主机(remotehost)上通过将输出返回到本地显示器的方式来执行单一命令
[root@xaii33 ~]# ssh tom@192.168.157.19 '/usr/bin/touch /tmp/host1'
tom@192.168.157.19's password:
[root@xaii19 tmp]# ls
host1
1.6 SSH 主机密钥
ssh通过公钥加密的方式保持通信安全。当某一ssh客户端连接到ssh服务器时,在该客户端登录之前,服务器会向其发送公钥副本。这可用于为通信渠道设置安全加密,并可验证客户端的服务器。
当用户第一次使用ssh连接到特定服务器时,ssh命令可在用户的/.ssh/known_hosts文件中存储该服务器的公钥。在此之后每当用户进行连接时,客户端都会通过对比/.ssh/known_hosts文件中的服务器条目和服务器发送的公钥,确保从服务器获得相同的公钥。如果公钥不匹配,客户端会假定网络通信已遭劫持或服务器已被入侵,并且中断连接。
这意味着,如果服务器的公钥发生更改(由于硬盘出现故障导致公钥丢失,或者出于某些正当理由替换公钥),用户则需要更新其~/.ssh/known_hosts文件并删除旧的条目才能够进行登录。
//主机ID存储在本地客户端系统上的 ~/.ssh/known_hosts 中
192.168.157.19 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBH1/4g+ky1HBDnQNnt7E5OMS7fug6m7wVz5OtpxKx9vXAoINvr0fkxFAjT041I5Qi0BGj6/I1VDE4FC/P2GJk/4=
//主机密钥存储在SSH服务器上的 /etc/ssh/ssh_host_key* 中
[root@xaii33 ~]# ls /etc/ssh/ssh_host_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密钥的身份验证
用户可通过使用公钥身份验证进行ssh登录身份验证。ssh允许用户使用私钥-公钥方案进行身份验证。这意味着将生成私钥和公钥这两个密钥。私钥文件用作身份验证凭据,像密码一样,必须妥善保管。公钥复制到用户希望登录的系统,用于验证私钥。公钥并不需要保密。拥有公钥的ssh服务器可以发布仅持有您私钥的系统才可解答的问题。因此,可以根据所持有的密钥进行验证。如此一来,就不必在每次访问系统时键入密码,但安全性仍能得到保证。
-
使用ssh-keygen命令生成密码。将会生成私钥/.ssh/id_rsa和公钥/.ssh/id_rsa.pub。
-
生成ssh密钥后,密钥将默认存储在家目录下的.ssh/目录中。私钥和公钥的权限就分别为600和644。.ssh目录权限必须是700
-
①在可以使用基于密钥的身份验证前,需要将公钥复制到目标系统上。可以使用ssh-copy-id完成这一操作
[root@xaii33 ~]# 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:SOJUfGD3WrQEApuaKBmUhJ4MntyZGJCuzf0hMci4qEQ root@xaii33 The key's randomart image is: +---[RSA 2048]----+ |=+. .o=.o.o | |=o =.o.+ . | |Oo*.B .. + | |.EoOoo . o | |B+o..o. S | |=.o o . | |o o . | |. . | | | +----[SHA256]-----+
-
②//使用 ssh-copy-id 将公钥复制到远程系统上的正确位置
[root@xaii33 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.157.19 /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.157.19's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.157.19'" and check to make sure that only the key(s) you wanted were added.
-
③//使用 ssh 命令无命令登录远程主机
[root@xaii33 ~]# ssh root@192.168.157.19
Last login: Tue Jan 8 00:50:53 2019 from 192.168.157.33
-
//使用 scp 命令传送文件到远程主机
[root@xaii33 ~]# scp hhh root@192.168.157.19:/root hhh 100% 0 0.0KB/s 00:00 [root@xaii19 ~]# ls anaconda-ks.cfg hhh
-
//使用 scp 命令从远程主机上下载文件到本地
[root@xaii33 ~]# rm -rf hhh [root@xaii33 ~]# ls anaconda-ks.cfg [root@xaii33 ~]# scp root@192.168.157.19:/root/hhh . hhh 100% 0 0.0KB/s 00:00 [root@xaii33 ~]# ls anaconda-ks.cfg hhh
-
//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@xaii33 ~]# openssl rand 20 -base64 qhmPVp4zs4G12rE3eWwQ5OCDrtU= //生成20位随机密码
5. 防火墙
-
iptables //默认所有都允许访问
-
firewalld //默认所有都不能访问
-
ebtables //rhel8里的
-
过程:
防火墙 --> tcp wrappers --> service --> selinux -
//关闭iptables和ebtables,并且设置为开机不自动启动,最后利用mask设置为不可启动
[root@xaii33 ~]# systemctl stop iptables ebtables Failed to stop iptables.service: Unit iptables.service not loaded. [root@xaii33 ~]# systemctl disable iptables [root@xaii33 ~]# systemctl disable ebtables [root@xaii33 ~]# systemctl mask ebtables Created symlink from /etc/systemd/system/ebtables.service to /dev/null. [root@xaii33 ~]# systemctl mask ipatables Created symlink from /etc/systemd/system/ipatables.service to /dev/null.
-
查看当前防火墙的规则
[root@xaii33 ~]# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: ens32 sources: services: ssh dhcpv6-client //默认是允许的 ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: //添加规则到此处
-
移除service,添加新的规则到rich rules
[root@xaii33 ~]# firewall-cmd --remove-service=ssh --permanent //永久删除 success [root@xaii33 ~]# firewall-cmd --add-rich-rule 'rule family=ipv4 source address=192.168.157.19/32 service name=ssh accept' --permanent //永久添加 success
-
重新加载规则并查看
[root@xaii33 ~]# firewall-cmd --reload success [root@xaii33 ~]# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: ens32 sources: services: dhcpv6-client ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: rule family="ipv4" source address="192.168.157.19/32" service name="ssh" accept
-
在192.168.157.19主机上远程登录192.168.157.33
[root@xaii19 ~]# ssh 192.168.157.33 Last login: Mon Jan 7 14:55:55 2019 from 192.168.157.19 [root@xaii33 ~]#
-
将ip更改为192.168.157.20,再尝试远程登录192.168.157.33
[root@xaii19 ~]# ssh 192.168.157.33 ssh: connect to host 192.168.157.33 port 22: No route to host