- 一OpenSSH服务器
- 1.SSH (Secure Shell)协议
- 2.OpenSSH
- 二原理
- 1.公钥传输原理
- 2.加密通讯原理
- 三.具体操作
- 四.服务端配置
- 五.使用SSH客户端程序2-1
- 1.ssh命令,
- 2.scp命令远程安全复制
- 3.sftp命令安全FTP上下载
- 六.ssh服务的最佳实践
- 七.轻量级自动化运维工具pssh
- 总结
SSH (Secure Shell)是一种安全通道协议,主要用来实现字符界面的远程登录、远程复制等功能。SSH协议对通信双方的数据传输进行了加密处理,其中包括用户登录时输入的用户口令,SSH为建立在应用层和传输层基础上的安全协议,可以对数据进行压缩,加快传输速度。
SSH客户端<网络->SSH服务端
优点:
数据传输是加密的,可以防止信息泄漏
数据传输是压缩的,可以提高传输速度
客户端:Linux client:ssh, scp, sftp, slogin
windows client: xshel1, Mobaxterm,putty, securecrt, sshsecureshellclient
服务名称:sshd
服务端主程序:/usr/sbin/sshd
服务端配置文件:/etc/ssh/sshd_config
openSSH是实现SSH协议的开源软件项目,适用于各种UNIX、Linux操作系统。
Centos 7系统默认已安装opensSh相关软件包,并将sshd服务添加为开机自启动。
执行"systemctl start sshd"命令即可启动sshd服务
sshd服务默认使用的是TCP的22端口,安全协议版本sshv2,除了2之外还有1(有漏洞)
ssh_config和sshd_config都是ssh服务器的配置文件,二者区别在于前者是针对客户端的配置文件,后者则是针对服务端的配置文件。
客户端发起链接请求
服务端返回自己的公钥,以及一个会话ID(这一步客户端得到服务端公钥)
客户端生成密钥对
客户端用自己的公钥异或会话ID,计算出一个值Res,并用服务端的公钥加密
客户端发送加密后的值到服务端,服务端用私钥解密,得到Res
服务端用解密后的值Res异或会话ID,计算出客户端的公钥(这一步服务端得到客户端公钥)
最终:双方各自持有三个秘钥,分别为自己的一对公、私钥,以及对方的公钥,之后的所有通讯都会被加密
(1)对称加密
1、概念
采用单钥密码系统的加密方法,同一个密钥可以同时用作信息的加密和解密,这种加密方法称为对称加密,由于其速度快,对称性加密通常在消息发送方需要加密大量数据时使用
2、常用算法
在对称加密算法中常用的算法有:DES、3DES、TDEA、Blowfish、RC2、RC4、RC5、IDEA、SKIPJACK等。
3、特点
1、加密方和解密方使用同一个密钥;
2、加密解密的速度比较快,适合数据比较长时的使用;
3、密钥传输的过程不安全,且容易被破解,密钥管理也比较麻烦;
4、优缺点
对称加密算法的优点是算法公开、计算量小、加密速度快、加密效率高。
对称加密算法的缺点是在数据传送前,发送方和接收方必须商定好秘钥,然后使双方都能保存好秘钥。其次如果一方的秘钥被泄露,那么加密信息也就不安全了。另外,每对用户每次使用对称加密算法时,都需要使用其他人不知道的独一秘钥,这会使得收、发双方所拥有的钥匙数量巨大,密钥管理成为双方的负担
(2)非对称加密
1、概念
非对称加密算法需要两个密钥:公开密钥(publickey:简称公钥)和私有密钥(privatekey:简称私钥)。公钥与私钥是一对,如果用公钥对数据进行加密,只有用对应的私钥才能解密。因为加密和解密使用的是两个不同的密钥,所以这种算法叫作非对称加密算法。
2、常用算法
RSA(RSA algorithm):目前使用最广泛的算法
DSA(Digital Signature Algorithm):数字签名算法,和 RSA 不同的是 DSA
仅能用于数字签名,不能进行数据加密解密,其安全性和RSA相当,但其性能要比RSA快
ECC(Elliptic curve cryptography,椭圆曲线加密算法)
ECDSA:Elliptic Curve Digital Signature Algorithm,椭圆曲线签名算法,是ECC和 DSA
的结合,相比于RSA算法,ECC 可以使用更小的秘钥,更高的效率,提供更高的安全保障
3、原理
首先ssh通过加密算法在客户端产生密钥对(公钥和私钥),公钥发送给服务器端,自己保留私钥,如果要想连接到带有公钥的SSH服务器,客户端SSH软件就会向SSH服务器发出请求,请求用联机的用户密钥进行安全验证。SSH服务器收到请求之后,会先在该SSH服务器上连接的用户的家目录下
5、优缺点
相比于对称加密技术,非对称加密技术安全性更好,但性能更慢。
此本次实验中,我们用非对称加密算法ECDSA进行加密,为了方便用root用户,也可给其他普通用户配置
三.具体操作[root@localhost ~]# systemctl stop firewalld [root@localhost ~]# setenforce 0 [root@localhost ~]# ssh root@192.168.133.100 The authenticity of host '192.168.133.100 (192.168.133.100)' can't be established. ECDSA key fingerprint is SHA256:MGLMhxb0TKGHZ9LuJAGhSYEfd+oJm54inKy0OgCSK2U. ECDSA key fingerprint is MD5:41:42:a8:d7:37:a3:fe:d0:62:7b:d5:01:d6:bc:af:4f. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.133.100' (ECDSA) to the list of known hosts. root@192.168.133.100's password: 输入密码即可登录 Last login: Sat Oct 2 19:45:18 2021 from 192.168.133.1 退出链接 [root@localhost ~]# exit logout Connection to 192.168.133.100 closed.
这里可以在客户机器上远程自己,查看自己的密钥,与服务器上进行匹配
[root@localhost ssh]# ssh 127.0.0.1 The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established. ECDSA key fingerprint is SHA256:MGLMhxb0TKGHZ9LuJAGhSYEfd+oJm54inKy0OgCSK2U. ECDSA key fingerprint is MD5:41:42:a8:d7:37:a3:fe:d0:62:7b:d5:01:d6:bc:af:4f. Are you sure you want to continue connecting (yes/no)?四.服务端配置
文件位置
[root@localhost ~]# cat /etc/ssh/sshd_config
40 #MaxAuthTries 6 设置本机允许连接次数为6次,但其实默认只允许3次 取消注释后,命令生效 Warning: Permanently added '192.168.133.50' (ECDSA) to the list of known hosts. root@192.168.133.50's password: Permission denied, please try again. root@192.168.133.50's password: Permission denied, please try again. root@192.168.133.50's password: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). [root@localhost ssh]#
38 #PermitRootLogin yes 不允许root远程ssh登录 取消注释并把yes改成no [root@localhost ~]# systemctl restart sshd
这时到客户机上测试
[root@localhost ssh]# ssh root@192.168.133.50 root@192.168.133.50's password: Permission denied, please try again. 即使密码正确却已经被拒绝登录了
#以下可以限制可登录用户的办法:白名单 黑名单
AllowUsers user1 user2 user3@ip(限制主机) 此条命令只允许限制用户在限制主机上登录,其他都不允许
注意,有可能显示用户su root 所以要
[root@localhost pam.d]# vim /etc/pam.d/su 解除第6行的注释,让普通用户无法su root
DenyUsers user1 user2 user3
AllowGroups g1 g2
DenyGroups g1 g2
ssh user@host 远程安全登录端口选项:-p22
2.scp命令远程安全复制格式1:scp user@host:file1 file2
格式2:scp file1 user@host:file2
sftp user@host
六.ssh服务的最佳实践1.建议使用非默认端口 文件第17行,默认22
2.禁止使用protocol version 1 安全协议版本默认2
3.限制可登录用户 在底层加命令
4.设定空闲会话超时时长 文本37行
5.利用防火墙设置ssh访问策略
6.仅监听特定的IP地址
7.基于口令认证时,使用强密码策略,比如:tr-dcA-Za-z0-9_ 8.使用基于密钥的认证
9.禁止使用空密码
10.禁止root用户直接登录
11.限制ssh的访问频度和并发在线数
12.经常分析日志
-H:主机字符串,内容格式”[user@]host[:port]”
-h file:主机列表文件,内容格式”[user@]host[:port]”
-A:手动输入密码模式
-i:每个服务器内部处理信息输出
-l:登录使用的用户名
-p:并发的线程数【可选】
-o:输出的文件目录【可选】
-e:错误输出文件【可选】
-t:TIMEOUT 超时时间设置,0无限制【可选】
-O:SSH的选项
-P:打印出服务器返回信息
-v:详细模式
–version:查看版本
安装需要配置开发源
[root@ky15-1 yum.repos.d]# cd /etc/yum.repos.d
[root@ky15-1 yum.repos.d]# vim CentOS-base.repo
#最后一行添加
[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/$releasever/x86_64
https://mirrors.cloud.tencent.com/epel/$releasever/x86_64
https://mirrors.huaweicloud.com/epel/$releasever/x86_64
https://mirrors.tuna.tsinghua.edu.cn/epel/$releasever/x86_64
gpgcheck=0
[root@localhost yum.repos.d]# ssh-keygen
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:onzF49e+aD4DgL/L6Y4YrT/EZiLAU3UVdk7nQtV4yIs root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| .. ..+.+oo+ |
| . . . = o+ o |
|. . . o..o |
|.o . .. E.. |
|. .. ...S |
|. .o=..+.. . |
| ..=+ ..... . |
| +.+.. .+o |
| o.o+*. oooo. |
+----[SHA256]-----+
[root@localhost ~]# ssh-copy-id 192.168.133.129 上传密钥对
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.133.129 (192.168.133.129)' can't be established.
ECDSA key fingerprint is SHA256:bMMA+j9IOBfyHgw/JEi9rswV5sOPWeVW36A5AOT08e8.
ECDSA key fingerprint is MD5:a2:60:72:b6:f9:ff:b9:17:38:fb:fc:40:e6:1c:c2:6b.
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@192.168.133.129's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.133.129'"
and check to make sure that only the key(s) you wanted were added.
[root@localhost ~]# pssh -H 192.168.133.100 hostname -i
[1] 14:00:48 [SUCCESS] 192.168.133.100
[root@localhost ~]# pssh -H 192.168.133.129 hostname -i
[1] 14:01:29 [SUCCESS] 192.168.133.129
[root@localhost ~]# pssh -H "192.168.133.129 192.168.133.100" -i cat /etc/passwd |grep "192.168"
[1] 14:08:02 [SUCCESS] 192.168.133.129
[2] 14:08:02 [SUCCESS] 192.168.133.100
[root@localhost ~]# vim /opt/hosts.txt
192.168.133.100
192.168.133.129
[root@localhost ~]# pssh -h /opt/hosts.txt hostname -i
[1] 14:16:34 [SUCCESS] 192.168.133.100
[2] 14:16:34 [SUCCESS] 192.168.133.129
[root@localhost ~]# pssh -H "192.168.133.129 192.168.133.100" -i touch /opt/zhouyan
[1] 14:20:45 [SUCCESS] 192.168.133.129
[2] 14:20:45 [SUCCESS] 192.168.133.100
在客户机上测试
[root@localhost opt]# ls
rh zhouyan
总结
学会对/etc/ssh/sshd_config 的配置,熟悉每条命令的用处,在适当的时候调整策略
学会轻量级自动化运维工具pssh的应用。几十台机器的远程操作



