第三十九集 Linux SSH远程访问及控制


一、概述

SSH是一种安全通道协议,默认使用TCP22号端口,主要用来远程登录和远程复制等功能,它给通信双方的数据传输进行了加密处理,相比于telnet这种明文传输的额远程登录提供了很好的安全性

二、OpenSSH服务器

服务名称:sshd(CentOS6.5后默认安装开启)
服务端配置文件:/etc/ssh/sshd_config
客户端配置文件:/etc/ssh/ssh_config

三、服务监听选项

端口号、协议版本、监听IP地址
禁用反向解析
Port 22              //端口号
AddressFamily any    
ListenAddress 0.0.0.0   //ipv4监听地址
ListenAddress ::        //ipv6监听地址

四、用户登录控制

禁止root用户、空密码用户
登录时间、重试次数
AllowUsers、DenyUsers(黑白名单,允许和拒绝)
LoginGraceTime 2m      //会话时间
PermitRootLogin yes    //是否进制root登录
StrictModes yes        //是否验证访问权限
MaxAuthTries 6         //验证次数6次
MaxSessions 10         //访问的最大链接数

PubkeyAuthentication yes  //是否验证公钥

使用SSH服务

1、在tast01中进入SSH主服务器配置文件,更改配置文件条目,开启SSH服务。
[root@tast01 ~]# vim /etc/ssh/sshd_config //进入编辑服务器配置文件信息
Port 22 //开启端口
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
:wq //保存退出
[root@tast01 ~]# systemctl restart sshd //重启SSH服务
2、在tast02中使用SSH服务登录tast01。
[root@tast02 ~]# ssh root@192.168.144.133 //使用SSH服务登录tast01服务器
The authenticity of host ‘192.168.144.133 (192.168.144.133)’ can’t be established.
ECDSA key fingerprint is SHA256:B8IsZOFG7FbtVkIK+dMILmo0iA4OEIeVGY0GnnCbXhk.
ECDSA key fingerprint is MD5:c2:d8:09:17🇩🇪6e:ec:07:06:1b:ac:b6:1e:bd:62:09.
Are you sure you want to continue connecting (yes/no)? yes //询问是否建立会话
Warning: Permanently added ‘192.168.144.133’ (ECDSA) to the list of known hosts.
root@192.168.144.133’s password: //输入密码
Last login: Mon Sep 9 13:59:09 2019
[root@tast01 ~]# //成功登录tast01
[root@tast01 ~]# exit //退出
登出
Connection to 192.168.144.133 closed.
[root@tast02 ~]# //回到tast02端口
3、回到tast01服务器,更改SSH服务器配置文件,禁止root用户登录。然后再创建siti用户
[root@tast01 ~]# vim /etc/ssh/sshd_config //进入编辑主配置文件
#LoginGraceTime 2m
PermitRootLogin no //开启是否启用禁用root登录,更改yes为no,禁止root用户登录
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
:wq //保存退出
[root@tast01 ~]# systemctl restart sshd //重启服务
[root@tast01 ~]# useradd siti //创建siti普通用户
[root@tast01 ~]# passwd siti //设置用户密码
更改用户 siti 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@tast01 ~]# id siti //查看新建用户siti信息
uid=1001(siti) gid=1001(siti) 组=1001(siti)
[root@tast01 ~]# id sun //查看用户sun信息
uid=1000(sun) gid=1000(sun) 组=1000(sun),10(wheel)
4、使用tast02登录tast01的root用户,看更改的服务是否生效
[root@tast02 ~]# ssh root@192.168.144.133 //使用SSH服务登录tast01服务器root用户
root@192.168.144.133’s password: //输入密码登录
Permission denied, please try again. //拒绝登录root
root@192.168.144.133’s password:
Permission denied, please try again.
root@192.168.144.133’s password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). //尝试输入密码三次后弹出,拒绝登录
[root@tast02 ~]# ssh siti@192.168.144.133 //使用SSH服务登录siti用户
siti@192.168.144.133’s password:
[siti@tast01 ~]$ //成功登录tast01服务器siti用户
[siti@tast01 ~]$ su - root //再siti用户下使用su切换root用户
]密码: //输入密码
上一次登录:一 9月 9 15:16:00 CST 2019从 192.168.144.135pts/1 上
最后一次失败的登录:一 9月 9 15:33:03 CST 2019从 192.168.144.135ssh:notty 上
最有一次成功登录后有 3 次失败的登录尝试。
[root@tast01 ~]# //成功登录root用户。
[root@tast01 ~]# exit //退出
登出
[siti@tast01 ~]$ exit //退出
登出
Connection to 192.168.144.133 closed.
[root@tast02 ~]# //回到tast02用户
5、通过上面的操作我们禁止了远程登录root但是可以通过普通用户切换登录,这个时候我们就可以开启tast01系统中的pam认证,来提高系统的安全性。
[root@tast01 ~]# vim /etc/pam.d/su //进入编辑pam配置文件
#%PAM-1.0
auth sufficient pam_rootok.so
Uncomment the following line to implicitly trust users in the “wheel” group.
#auth sufficient pam_wheel.so trust use_uid
Uncomment the following line to require a user to be in the “wheel” group.
auth required pam_wheel.so use_uid //开启pam认证
auth substack system-auth
auth include postlogin
account sufficient pam_succeed_if.so uid = 0 use_uid quiet
account include system-auth
password include system-auth
session include system-auth
session include postlogin
session optional pam_xauth.so
~
~
~
:wq //保存退出
6、查看是否还能够通过siti用户切换到root用户
[root@tast02 ~]# ssh siti@192.168.144.133 //登录siti用户
siti@192.168.144.133’s password: //输入密码
Last failed login: Mon Sep 9 16:09:32 CST 2019 from 192.168.144.135 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Mon Sep 9 15:47:20 2019 from 192.168.144.135
[siti@tast01 ~]$ su - root //登录siti用户,并切换root用户
密码: //输入密码
su: 拒绝权限 //权限拒绝,无法切换
[siti@tast01 ~]$
7、因为设定了权限,siti用户不在wheel组,所以无法用siti用户切换root用户,我们可不可以通过siti用户切换wheel组中sun用户,再用sun用户切换root,看是否可以。
[siti@tast01 ~]$ su - sun //切换sun用户
密码: //输入密码
su: 拒绝权限 //权限拒绝,无法切换
[siti@tast01 ~]$
9、回到tast01中开启SSH服务配置密码验证次数服务
[root@tast01 ~]# vim /etc/ssh/sshd_config //进入服务器配置文件
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
MaxAuthTries 6 //开启密码验证次数
#MaxSessions 10
:wq //保存退出
10、进入tast02验证密码次数是否成功开启
[root@tast02 ~]# ssh sun@192.168.144.133 //登录sun用户
sun@192.168.144.133’s password: //输入错误密码
Permission denied, please try again. //1次输错,拒绝登录
sun@192.168.144.133’s password: //输入错误密码
Permission denied, please try again. //2次输错,拒绝登录
sun@192.168.144.133’s password: //输入错误密码
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). //3次输入错误直接登出
11、通过上面的实验发现并没有实现6次密码后再弹出,而是默认的三次,这个时候我们就用通过命令来提高默认密码次数来实现密码次数的设置。
[root@tast02 ~]# ssh -o NumberofPasswordPrompts=8 sun@192.168.144.133 //使用命令提高密码输入次数
sun@192.168.144.133’s password:
Permission denied, please try again.
sun@192.168.144.133’s password:
Permission denied, please try again.
sun@192.168.144.133’s password:
Permission denied, please try again.
sun@192.168.144.133’s password:
Permission denied, please try again.
sun@192.168.144.133’s password:
Permission denied, please try again.
sun@192.168.144.133’s password:
Received disconnect from 192.168.144.133 port 22:2: Too many authentication failures
Authentication failed. //输入密码6次后弹出,设设置生效
黑白名单设置(AllowUsers、DenyUsers)
在VMware 15中再增加一台Linux客户端(tast03IP地址:192.168.144.132),用于远程连接服务器。
1、再tast01中配置ssh服务端配置文件,添加AllowUsers条目,添加仅允许登录的客户端
[root@tast01 ~]# vim /etc/ssh/sshd_config //进入编辑ssh服务端配置文件
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
MaxAuthTries 6
#MaxSessions 10
AllowUsers sun@192.168.144.135 stii //在此处添加条目,仅允许IP地址为192.168.144.135客户机登录sun用户
仅允许客户端登录stii用户
#PubkeyAuthentication yes

:wq //保存退出
[root@tast01 ~]# useradd stii //添加stii用户
[root@tast01 ~]# passwd stii //设置stii用户密码
更改用户 stii 的密码 。
新的 密码:
无效的密码: 密码少于 8 个���符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@tast01 ~]# systemctl restart sshd //重启ssh服务
2、分别在tast02、tast03客户机使用ssh服务远程登录tast01服务器
[root@tast02 ~]# ssh sun@192.168.144.133 //在tast02客户端中登录服务器sun用户
sun@192.168.144.133’s password: //输入密码
Last failed login: Mon Sep 9 17:24:32 CST 2019 from 192.168.144.135 on ssh:notty
There were 6 failed login attempts since the last successful login.
Last login: Mon Sep 9 17:21:47 2019 from 192.168.144.133
[sun@tast01 ~]$ //成功登录
[sun@tast01 ~]$ exit //退出用户
登出
Connection to 192.168.144.133 closed.
[root@tast02 ~]# ssh siti@192.168.144.133 //使用ssh登录服务器siti用户
siti@192.168.144.133’s password: //输入密码
Permission denied, please try again. //拒绝登录
[root@tast02 ~]# ssh stii@192.168.144.133 //登录stii用户
stii@192.168.144.133’s password: //输入密码
[stii@tast01 ~]$ //成功登录
[root@tast03 ~]# ssh sun@192.168.144.133 //tast03客户机使用ssh服务登录服务器sun用户
The authenticity of host ‘192.168.144.133 (192.168.144.133)’ can’t be established.
ECDSA key fingerprint is SHA256:B8IsZOFG7FbtVkIK+dMILmo0iA4OEIeVGY0GnnCbXhk.
ECDSA key fingerprint is MD5:c2:d8:09:17🇩🇪6e:ec:07:06:1b:ac:b6:1e:bd:62:09.
Are you sure you want to continue connecting (yes/no)? yes //询问是否建立会话,输入yes确定建立会话
Warning: Permanently added ‘192.168.144.133’ (ECDSA) to the list of known hosts.
sun@192.168.144.133’s password: //输入密码
Permission denied, please try again. //拒绝登录
[root@tast03 ~]# ssh siti@192.168.144.133 //tast03客户机使用ssh服务登录服务器siti用户
siti@192.168.144.133’s password: //输入密码
Permission denied, please try again. //拒绝登录
[root@tast03 ~]# ssh stii@192.168.144.133 //tast03客户机使用ssh服务登录服务器stii用户
stii@192.168.144.133’s password: //输入密码
Last login: Mon Sep 9 21:55:49 2019 from 192.168.144.135
[stii@tast01 ~]$ //成功登录
3、回到tast01服务器,编辑ssh服务器配置文件
[root@tast01 ~]# vim /etc/ssh/sshd_config //编辑ssh服务器配置文件
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
MaxAuthTries 6
#MaxSessions 10
DenyUsers sun@192.168.144.135 stii //删除仅允许条目,添加拒绝条目

#PubkeyAuthentication yes
:wq //保存退出
[root@tast01 ~]# systemctl restart sshd //重启ssh服务
4、分别在tast02、tast03客户机使用ssh服务远程登录tast01服务器
[root@tast02 ~]# ssh sun@192.168.144.133 //在tast02客户端中登录服务器sun用户
sun@192.168.144.133’s password: //输入密码
Permission denied, please try again. //拒绝登录
[root@tast02 ~]# ssh stii@192.168.144.133 //在tast02客户端中登录服务器stii用户
stii@192.168.144.133’s password: //输入密码
Permission denied, please try again. //拒绝登录
[root@tast02 ~]# ssh siti@192.168.144.133 //在tast02客户端中登录服务器siti用户
siti@192.168.144.133’s password: //输入密码
Last failed login: Mon Sep 9 22:02:00 CST 2019 from 192.168.144.132 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Mon Sep 9 21:53:53 2019 from 192.168.144.135
[siti@tast01 ~]$ //成功登录
[root@tast03 ~]# ssh stii@192.168.144.133 //tast03客户机使用ssh服务登录服务器stii用户
stii@192.168.144.133’s password: //输入密码
Permission denied, please try again. //拒绝登录
[root@tast03 ~]# ssh sun@192.168.144.133 //tast03客户机使用ssh服务登录服务器sun用户
sun@192.168.144.133’s password: //输入密码
Last failed login: Mon Sep 9 22:30:55 CST 2019 from 192.168.144.135 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Mon Sep 9 22:24:51 2019 from 192.168.144.133
[sun@tast01 ~]$ //成功登录
[root@tast03 ~]# ssh siti@192.168.144.133 //tast03客户机使用ssh服务登录服务器siti用户
siti@192.168.144.133’s password: //输入密码
Last login: Mon Sep 9 22:32:16 2019 from 192.168.144.135
[siti@tast01 ~]$ //成功登录
使用密钥对验证登录
1、首先在tast01服务器中进入编辑ssh配置文件,开启密钥验证条目
[root@tast01 ~]# vim /etc/ssh/sshd_config //编辑ssh配置文件
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
MaxAuthTries 6
#MaxSessions 10
DenyUsers sun@192.168.144.135 stii

PubkeyAuthentication yes //开启密钥对验证功能
The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys //密钥存放位置

:wq //保存退出
2、进入客户端tast02客户机中,配置密钥
[root@tast02 ~]# useradd siaa //在tast02客户机中创建用户
[root@tast02 ~]# passwd siaa //设置用户目录
更改用户 siaa 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@tast02 ~]# su - siaa //切换至用户siaa
[siaa@tast02 ~]$ ssh-keygen -t ecdsa //制作ecdsa类型密钥
Generating public/private ecdsa key pair.
Enter file in which to save the key (/home/siaa/.ssh/id_ecdsa): //密钥存放位置,保持不变,直接回车
Created directory ‘/home/siaa/.ssh’.
Enter passphrase (empty for no passphrase): //输入要设置的密码
Enter same passphrase again: //再次输入密码
Your identification has been saved in /home/siaa/.ssh/id_ecdsa.
Your public key has been saved in /home/siaa/.ssh/id_ecdsa.pub.
The key fingerprint is:
SHA256:5mTvLU19q7uUUXECnEmNldB3S4gUiNZdvm1zupFUf0Y siaa@tast02
The key’s randomart image is:
±–[ECDSA 256]—+
| o +=B@+o.|
| o o o*.+o=|
| . …oE|
| ++.| //生成ecdsa密钥
| S +.+=|
| = . …=+=|
| . .o o+…|
| …o + |
| …+= |
±—[SHA256]-----+
[siaa@tast02 ~]$ ls -a //查看用户家目录隐藏文件
. … .bash_logout .bash_profile .bashrc .cache .config .mozilla .ssh
[siaa@tast02 ~]$ cd .ssh //进入生成的.ssh目录
[siaa@tast02 .ssh]$ ls //查看目录内容
id_ecdsa id_ecdsa.pub //生成的私钥与公钥文件
[siaa@tast02 .ssh]$ ssh-copy-id -i id_ecdsa.pub siti@192.168.144.133 //指定生成的公钥文件推送到服务器siti用户
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “id_ecdsa.pub”
The authenticity of host ‘192.168.144.133 (192.168.144.133)’ can’t be established.
ECDSA key fingerprint is SHA256:B8IsZOFG7FbtVkIK+dMILmo0iA4OEIeVGY0GnnCbXhk.
ECDSA key fingerprint is MD5:c2:d8:09:17🇩🇪6e:ec:07:06:1b:ac:b6:1e:bd:62:09.
Are you sure you want to continue connecting (yes/no)? yes //询问是推送,输入yes
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed – if you are prompted now it is to install the new keys
siti@192.168.144.133’s password: //输入服务器siti用户密码

Number of key(s) added: 1 //成功添加文件

Now try logging into the machine, with: “ssh ‘siti@192.168.144.133’”
and check to make sure that only the key(s) you wanted were added.
[siaa@tast02 .ssh]$ ls //查看目录信息
id_ecdsa id_ecdsa.pub known_hosts //创建文件Known_hosts
[siaa@tast02 .ssh]$ vim known_hosts //查看文件信息
192.168.144.133 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBC6sBj5BEqQkEIXTdcRDCzDlQRfhaoaY7OvyWzxcNxt+n6ZjbA1PSYK2SeTW3MAhUZOry7T6gNDFL7YyfMfXOGo= //成功将ecdsa生成的密钥推送给服务器
3、回到tast01服务器中查看siti家目录中是否有推送的文件
[root@tast01 ~]# cd /home/siti //进入siti家目录
[root@tast01 siti]# ls -a //查看隐藏文件
. .bash_history .bash_profile .cache .mozilla
… .bash_logout .bashrc .config .ssh
[root@tast01 siti]# cd .ssh //进入添加的.ssh目录
[root@tast01 .ssh]# ls //查看信息
authorized_keys
[root@tast01 .ssh]# cat authorized_keys //查看信息内容
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBD6B4elJHibp7lYDfogSfd7krTUPyKzvLHZNk75GTm1oibrA0aMirgtwxxfUEOi+9+ZGU2V0C3+zH6vQpjvvPoo= siaa@tast02 //siaa@tast02的ecdsa加密文件
4、在tast02客户端中使用siaa用户进行验证登录服务器tast01中siti用户
[siaa@tast02 .ssh]$ whoami //使用命令查看当前登录用户
siaa //确定当前登录用户为siaa
[siaa@tast02 .ssh]$ ssh siti@192.168.144.133 //使用ssh服务登录服务器siti用户
Enter passphrase for key ‘/home/siaa/.ssh/id_ecdsa’: //输入设置的ecdsa密码
Last login: Mon Sep 9 22:37:19 2019 from 192.168.144.132
[siti@tast01 ~]$ //成功登录服务器siti用户
5、设置客户机信任用户免验证登录服务器
[siti@tast01 ~]$ exit //退出当前用户
登出
Connection to 192.168.144.133 closed.
[siaa@tast02 .ssh]$ ssh-agent bash //回到tast02中siaa用户,使用命令代理bash环境
[siaa@tast02 .ssh]$ ssh-add //使用命令添加验证密码
Enter passphrase for /home/siaa/.ssh/id_ecdsa: //输入验证密码
Identity added: /home/siaa/.ssh/id_ecdsa (/home/siaa/.ssh/id_ecdsa) //成功添加密码
[siaa@tast02 .ssh]$ ssh siti@192.168.144.133 //登录服务器siti用户
Last login: Mon Sep 9 23:31:28 2019 from 192.168.144.135
[siti@tast01 ~]$ //成功登录,免密码验证
SSH客户端程序
1、进入tast01服务器,编辑SSH配置文件,打开root登录,因为在Linux系统中有些路径没有root权限,无法实现复制功能,
[root@tast01 ~]# vim /etc/ssh/sshd_config
…//省略部分内容…
Authentication:

#LoginGraceTime 2m
PermitRootLogin yes //开启登录root用户权限
#StrictModes yes
MaxAuthTries 6
#MaxSessions 10

PubkeyAuthentication yes

The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
:wq //保存退出
[root@tast01 ~]# systemctl restart sshd //重启SSH服务
2、在tast02中验证root用户登录权限是否成功开启。
[root@tast02 ~]# ssh root@192.168.144.133 //使用ssh服务登录服务器root用户
root@192.168.144.133’s password: //输入用户密码
Last login: Wed Sep 11 22:56:28 2019 from 192.168.144.135
[root@tast01 ~]# //成功登录
3、在tast02中退出服务器root用户登录,在opt目录下创建文件,使用scp命令推送给tast01用户
[root@tast01 ~]# exit //退出
登出
Connection to 192.168.144.133 closed.
[root@tast02 ~]# cd /opt/ //进入opt目录
[root@tast02 opt]# ls //查看
rh
[root@tast02 opt]# echo “this is ssh-client” > ssh_client.txt //创建.txt文件
[root@tast02 opt]# mkdir -p tast/si11 //递归创建tast目录并在tast目录下创建si11目录
[root@tast02 opt]# ls //查看
rh ssh_client.txt tast //成功创建文件与目录
[root@tast02 opt]# scp ssh_client.txt root@192.168.144.133:/home/ //将创建的.txt文件推送到服务器root用户home目录下
root@192.168.144.133’s password: //输入密码
ssh_client.txt 100% 19 6.0KB/s 00:00 //成功推送
4、回到tast01服务器中,查看home目录下是否有推送过去的文件。
[root@tast01 ~]# ls /home/ //查看home目录下文件
ssh_client.txt sun //成功添加文件
[root@tast01 ~]# cat /home/ssh_client.txt //查看文件内容
this is ssh-client //显示文件内容
5、在tast02中把刚创建的文件夹推送给tast01服务器,并在tast01服务器中查看是否成功推送
[root@tast02 opt]# scp -r tast/ root@192.168.144.133:/home/ //推送文件夹
root@192.168.144.133’s password: //输入密码
[root@tast02 opt]# //推送成功
[root@tast01 ~]# ls /home/ //查看home目录
ssh_client.txt sun tast //显示推送的文件夹
[root@tast01 ~]# ls /home/tast/ //查看文件夹内容
si11 //显示创建的si11目录
使用sftp命令实现远程上传和下载
1、在tast02中删除创建的文件与文件夹
[root@tast02 opt]# ls //查看信息
rh ssh_client.txt tast //显示内容
[root@tast02 opt]# rm -rf ssh_client.txt //删除txt文件
[root@tast02 opt]# rm -rf tast/ //删除文件夹
[root@tast02 opt]# ls //查看
rh //成功删除
2、使用sftp命令从tast01服务器中下载文件
[root@tast02 opt]# sftp root@192.168.144.133 //使用sftp命令登录tast01服务器root用户
root@192.168.144.133’s password: //输入密码
Connected to 192.168.144.133.
sftp> ls //成功登录并查看目录信息
anaconda-ks.cfg initial-setup-ks.cfg 下载 公共
图片 文档 桌面 模板 //此时在root用户家目录下
视频 音乐
sftp> cd /home/ //进入home目录
sftp> ls //查看
ssh_client.txt sun tast //显示内容
sftp> get ssh_client.txt //使用get命令下载txt文件
Fetching /home/ssh_client.txt to ssh_client.txt
/home/ssh_client.txt 100% 19 19.3KB/s 00:00
sftp> bye //退出
[root@tast02 opt]# ls //查看目录下是否有内容
rh ssh_client.txt //成功下载
3、将下载的文件更改名字,在使用sftp命令将文件上传至tast01服务器home目录,并回到tast01服务器中查看信息
[root@tast02 opt]# mv ssh_client.txt ssh_server.txt //更改文件名称
[root@tast02 opt]# ls //查看
rh ssh_server.txt //已更改
[root@tast02 opt]# sftp root@192.168.144.133 //使用sftp命令登录tast01root用户
root@192.168.144.133’s password: //输入密码
Connected to 192.168.144.133.
sftp> cd /home/ //进入home目录
sftp> ls //查看内容
ssh_client.txt sun tast
sftp> put ssh_server.txt //将文件上传至tast01服务器home目录中
Uploading ssh_server.txt to /home/ssh_server.txt
ssh_server.txt 100% 19 15.6KB/s 00:00
sftp> bye //退出
[root@tast02 opt]#
[root@tast01 ~]# ls /home/ //查看home目录内容
ssh_client.txt ssh_server.txt sun tast //成功上传文件
TCP wrappers访问控制
TCP wrappers概述
保护原理
TCP wrappers将其他的TCP服务程序“包裹”起来,增加了一个安全的检测过程,外来的连接请求必须先通过这层安全检测,获得许可后才能访问真正的服务程序。TCP wrappers还可以记录所有企图访问被保护服务的行为,为管理员提供丰富的安全分析资料。TCP wrappers的访问是基于TCP协议的应用服务。
保护机制的实现方式
方式1:通过tcpd主程序对其他服务程序进行包装
方式2:有其他服务程序调用libwrap.os.连接库
访问控制策略的配置文件
/etc/hosts.allow
/etc/hosts.deny
TCP Wrappers策略应用
设置访问控制策略
策略格式:服务列表:客户机地址列表
服务列表
多个服务以逗号分隔,ALL表示所有服务
客户机地址列表
多个地址以逗号分隔,ALL表示所有地址
允许使用通配符?和

网段地址,如:192.168.4.0或者192.168.4.0/255.255.255.0
区域地址,如:.benet.com
策略的应用顺序
先检查hosts.allow,找到匹配则允许访问
否则再检查hosts.deny,找到则拒绝访问
若两个文件中均无匹配策略,则默认允许访问
Demo
实验环境:
在VMware 15中开启三台CentOS 7系统计算机,主机名分别为tast01、tast02、tast03,tast01作为服务器系统,tast02、tast03分别为两台客户机,tast01IP地址:192.168.144.133、tast02IP地址:192.168.144.135、tast03IP地址:192.168.144.132
搭建实验
使用TCP Wrappers之前我们先要查看etc目录下是否存在hosts.allow配置文件,有此配置文件才可以正常使用TCP Wrappers功能。(一般系统默认配置此文件)
[root@tast01 ~]# cd /etc //进入etc目录
[root@tast01 etc]# ls ./ | grep *.allow //查看当前目录内容,并过滤所有后缀为.allow的文件
hosts.allow //显示文件
进入 hosts.allow配置文件,设置仅允许192.168.144.132客户机可以通过ssh服务访问服务器,在hosts.deny配置文件中添加拒绝所有客户机使用SSH服务访问服务器。
[root@tast01 etc]# vim hosts.allow //进入编辑配置文件
hosts.allow This file contains access rules which are used to
allow or deny connections to network services that
either use the tcp_wrappers library or that have been
started through a tcp_wrappers-enabled xinetd.

          See 'man 5 hosts_options' and 'man 5 hosts_access'
           for information on rule syntax.
           See 'man tcpd' for information on tcp_wrappers

sshd:192.168.144.132 //添加条目,仅允许192.168.144.132客户机访问服务器
~
~
~
:wq //保存退出
[root@tast01 etc]# vim hosts.deny

hosts.deny This file contains access rules which are used to
deny connections to network services that either use
the tcp_wrappers library or that have been
started through a tcp_wrappers-enabled xinetd.

           The rules in this file can also be set up in
           /etc/hosts.allow with a 'deny' option instead.

           See 'man 5 hosts_options' and 'man 5 hosts_access'
          for information on rule syntax.
          See 'man tcpd' for information on tcp_wrappers

sshd:ALL //编辑条目拒绝所有客户机访问(注意,此处使用大写)
~
~
~
:wq //保存退出
[root@tast03 ~]# ssh root@192.168.144.133 //使用tast03客户机访问服务器
root@192.168.144.133’s password: //输入密码
Last login: Mon Sep 16 13:43:33 2019
[root@tast01 ~]# //成功登录
[root@tast02 ~]# ssh root@192.168.144.133 //使用tast02客户机访问服务器
ssh_exchange_identification: read: Connection reset by peer //拒绝访问

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值