一. 实验环境
client:
westos_client.westos.com
172.25.254.10
server:
westos_node1.westos.com
172.25.254.20
ifconfig ##查看ip地址
nm-connection-editor ##设定ip地址
hostnamectl ##查看主机名称
hostnamectl set-hostname ##设定主机名称
二. Openssh服务的基本信息
Openssh
1.作用:
在企业中的运维工作,不是1v1的运维
运维工程师基本都是1vN
在运维工程师对服务器进行管理时
需要通过网络登陆远程主机
需要通过网络在远程主机中开启shell来进程操作
远程主机必须提供这样的开启shell的方式
以上的需求由openssh服务来提供
远程主机中安装了openssh并开启了这个程序
那么我们就可以通过网络来连接这台主机了
# 2.软件包名称 #
openssh-server
# 3.服务名称 #
sshd
# 4.客户端 #
ssh
ssh 172.25.254.20 -l root
# 172.25.254.20 远程主机ip
# -l login 登陆
# root 远程主机的超级用户
结果:
[root@westos_client ~]# ssh 172.25.254.20 -l root
The authenticity of host '172.25.254.20 (172.25.254.20)' can't be established.
ECDSA key fingerprint is SHA256:+5Ikt8ZExrRdLzOg19xsL5PhAYPt8GB7umvLcSF/dlM.
Are you sure you want to continue connecting (yes/no)? yes #建立签证关系
Warning: Permanently added '172.25.254.20' (ECDSA) to the list of known hosts.
root@172.25.254.20's password: ##输入远程主机密码
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Sat Apr 25 09:02:58 2020
[root@westos_node1 ~]# ##登陆成功
# 5.sshd服务端口 #
22/tcp (tcp协议的22端口)
# 6.配置文件 #
/etc/ssh/sshd_config
三. Openssh服务的key认证
# 1.用户密码认证 (对称加密)
缺点:密码容易泄漏
容易忘记
# 2.**认证 (非对称加密)
优点:安全
不会忘记密码
# 3.建立key认证的方式 #
① 建立key文件
172.25.254.10主机中
[root@westos_client ~]# ssh-****** ##建立认证key文件
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): ##加密字符保存位置(默认地址/root/.ssh/id_rsa)
Enter passphrase (empty for no passphrase): ##key认证密码,通常设定为 空
Enter same passphrase again: ##重复key认证密码
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:Uh8cYTxqCShLnD3NUxpIQ4/NOtSVuyPuMzHuGnxSd1k
root@westos_client.westos.com
The key's randomart image is:
+---[RSA 2048]----+
| . =+=..oo+. |
| = =B=+.oo. |
| . oo.*o.+o.E |
| .. . .=. + |
| o o.S.+ |
| . o+oo. |
| +o.+ . |
| += |
| .+oo |
+----[SHA256]-----+
②上传公钥 (加密远程主机)
#ssh-copy-id 上传**命令
#-i 指定公钥
#/root/.ssh/id_rsa.pub 公钥名称
#root 远程主机被加密用户
#@ 分割副
#172.25.254.20 远程主机地址
/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@172.25.254.20's password: ##远程主机密码
Number of key(s) added: 1
and check to make sure that only the key(s) you wanted were added.
③测试
172.25.254.10主机中
1.ssh root@172.25.254.20 ##免密连接
2.mv /root/.ssh/id_rsa* /mnt
3.reboot
4.ssh root@172.25.254.20 ##因为**被移除所以不能免密
5.mv /mnt/id_rsa /root/.ssh ##重新恢复**
6.ssh root@172.25.254.20 ##免密连接
四.Openssh服务的安全配置
# 1.端口修改
172.25.254.20主机中
vim /etc/ssh/sshd_config
17 port 22 -----> port 1111
systemctl restart sshd
测试
172.25.254.10主机中
[root@westos_client ~]# ssh 172.25.254.20 -l root
ssh: connect to host 172.25.254.20 port 22: Connection refused #默认连接被拒绝
[root@westos_client ~]# ssh 172.25.254.20 -l root -p 1111 #指定的的端口连接 -p
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Sat Apr 25 11:05:31 2020 from 172.25.254.10
[root@westos_node1 ~]#
执行setenforce 0 操作 或vim /etc/sysconfig/selinux 将SELINUX=disabled 再reboot
重启后关闭火墙 systemctl stop firewalld (不用restart)
#2. 端口绑定
vim /etc/ssh/sshd_config
19 ListenAddress 0.0.0.0 ----> ListenAddress 172.25.254.20
测试:
在172.25.254.20主机中
参数未发生变化:
ssh 172.25.254.20 -l root #可以
ssh 127.0.0.1 -l root #可以
参数发生变化
ssh 172.25.254.20 -l root #可以
ssh 127.0.0.1 -l root #拒绝
#3.密码认证的关闭
#vim /etc/ssh/sshd_config
73 PasswordAuthentication no ##禁用密码认证
测试:
172.25.254.10
有**
172.25.254.20
无**
172.25.254.10 -----> ssh root@172.25.254.20 ##可以登陆
172.25.254.20 -----> ssh root@172.25.254.20 ##可以输入密码
参数根改并重启服务(systemctl restart sshd)后
172.25.254.10 -----> ssh root@172.25.254.20 ##可以登陆
172.25.254.20 -----> ssh root@172.25.254.20 ##不可输入密码直接被拒绝
#4. sshd服务的用户控制
建立实验素材
在服务器中:
172.25.254.20
useradd westos
useradd lee
echo lee | passwd --stdin westos
echo lee | passwd --stdin lee
操作完成系统中存在至少3个用户
用户 密码
root lee
westos lee
lee lee
#root用户的访问控制
vim /etc/ssh/sshd_config
46 PermitRootLogin yes --->PermitRootLogin no #禁止root用户使用ssh进行认证
实验:
172.25.254.20:
ssh 172.25.254.20 -l root ##默认输入正确秘密可以完成认证
修改参数后重启服务 执行ssh 172.25.254.20 -l root ##输入正确密码仍然不能登陆
#用户黑名单 (默认可以登陆,名单中的用户被禁止)
vim /etc/ssh/sshd_config
DenyUsers lee ##需手动输入 ##lee用户不能使用sshd服务(多用户用空格分开即可)
实验:
172.25.254.20:
ssh 172.25.254.20 -l lee ##默认可以登陆
ssh 172.25.254.20 -l westos ##可以登陆
修改参数后
ssh 172.25.254.20 -l westos ##可以登陆
ssh 172.25.254.20 -l lee ##不可以登陆
#用户白名单 (默认不可以登陆,名单中的用户被允许)
vim /etc/ssh/sshd_config
AllowUsers westos ##需手动输入 ##westos用户可以使用sshd服务,默认用户被禁止(多用户用空格分开即可)
实验:
172.25.254.20:
ssh 172.25.254.20 -l lee ##默认可以登陆
ssh 172.25.254.20 -l westos ##可以登陆
ssh 172.25.254.20 -l root ##可以登陆
修改参数后:
ssh 172.25.254.20 -l westos ##可以登陆
ssh 172.25.254.20 -l lee ##不可以登陆
ssh 172.25.254.20 -l root ##不可以登陆
五. 远程执行命令
172.25.254.20 登陆模式选择第二个(X11模式)
172.25.254.10:
ssh 172.25.254.20 -l root ##此方法登陆时不能调用远程主机的图形命令(gedit窗口会在172.25.254.20主机打开)
ssh -X 172.25.254.20 -l root ##当前登陆可以调用图形 -X 代表启用x11图形连接
ssh 172.25.254.20 -l root touch /root/Desktop/westosfile{1..10} #直接执行命令在远程主机中
ssh 172.25.254.20 -l root -o StrictHostKeyChecking=no ##在连接主机时不检测HOSTKEY (不检测签证)
##(屏蔽yes、no的输入)
六. sshd登陆信息修改
#172.25.254.20:
#
vim /etc/motd
##在此文件中有什么字符在ssh登陆时就可以看到什么字符