Linux中远程访问及控制
SSH远程管理
SSH远程控制介绍
SSH
是一种安全通道协议,主要用来实现字符界面的远程登录、远程复制等功能。SSH
协议对通信双方的数据传输进行了加密处理,其中包括用户登录时输入的用户口令。与早期的telnet、rsh、rcp
等应用相比,SSH
协议提供了更好的安全性。
OpenSSH服务器
SSH协议
- 为客户机提供安全的
shell
环境,用于与远程管理 - 默认端口:
TCP 22
OpenSSH
- 服务名称:
sshd
- 服务端主程序:
/usr/sbin/sshd
- 服务端配置文件:
/etc/ssh/sshd_config
- 客户端配置文件:
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客户端程序
ssh
命令--远程安全登录scp
命令--远程安全复制sftp
命令--安全FTP上下载- get 下载
- put 上传
- bye 退出
Demo
在VMware 15
中打开两天Linux
系统,一台用作主服务器(tast01IP地址:192.168.144.133
),一台用作远程终端(tast02IP地址:192.168.144.135
),使用SSH
协议登录主服务器
使用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:de: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 2mPermitRootLogin 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. //拒绝登录rootroot@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.0auth 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-authauth include postloginaccount sufficient pam_succeed_if.so uid = 0 use_uid quietaccount include system-authpassword include system-authsession include system-authsession include postloginsession 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:nottyThere 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 2mPermitRootLogin no#StrictModes yesMaxAuthTries 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 failuresAuthentication 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 2mPermitRootLogin no#StrictModes yesMaxAuthTries 6#MaxSessions 10AllowUsers 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:nottyThere 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:de: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 2mPermitRootLogin no#StrictModes yesMaxAuthTries 6#MaxSessions 10DenyUsers 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:nottyThere 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:nottyThere 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 2mPermitRootLogin no#StrictModes yesMaxAuthTries 6#MaxSessions 10DenyUsers sun@192.168.144.135 stiiPubkeyAuthentication 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_keysAuthorizedKeysFile .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@tast02The 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:de: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 keyssiti@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 2mPermitRootLogin yes //开启登录root用户权限#StrictModes yesMaxAuthTries 6#MaxSessions 10PubkeyAuthentication 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.txtssh_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 //拒绝访问[root@tast02 ~]#