Linux 远程访问与控制
OpenSSH
OpenSSH 是 SSH (Secure SHell) 协议的免费开源实现。SSH协议族可以用来进行远程控制, 或在计算机之间传送文件。而实现此功能的传统方式,如telnet(终端仿真协议)、 rcp ftp、 rlogin、rsh都是极为不安全的,并且会使用明文传送密码。OpenSSH提供了服务端后台程序和客户端工具,用来加密远程控制和文件传输过程中的数据,并由此来代替原来的类似服务。
服务名称:sshd
服务主程序:/usr/sbin/sshd
服务端配置文件:/etc/ssh/sshd_config
配置
这边我们设立两台虚拟机,分别命名为test01和test02
[root@localhost ~]# hostname test01
[root@localhost ~]# su
[root@test01 ~]#
...//
[root@localhost ~]# hostname test02
[root@localhost ~]# su
[root@test02 ~]#
接下来,我们再在test01上建立两个用户,用于后续测试
[root@test01 ~]# useradd tom
[root@test01 ~]# passwd tom
更改用户 tom 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
...//
[root@test01 ~]# useradd tom1
[root@test01 ~]# passwd tom1
更改用户 tom1 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@test01 ~]#
接下来,我们回到test02,通过ssh连接test01
[root@test02 ~]# ssh root@192.168.139.11
The authenticity of host '192.168.139.11 (192.168.139.11)' can't be established.
ECDSA key fingerprint is SHA256:v0Da1cih9XyH2R4eyTTs5T+atEcqrEEdxJIOiOJf3ZI.
ECDSA key fingerprint is MD5:9b:9c:ac:cb:ef:d0:b3:7a:71:36:94:d3:41:e9:10:f1.
这边yes确认
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.139.11' (ECDSA) to the list of known hosts.
这边输入test01中root用户的密码
root@192.168.139.11's password:
Last login: Sun Jul 19 15:28:39 2020 from 192.168.139.1
[root@test01 ~]#
这边注意,若你不想让对方远程登录,你可以在ssh配置文件中设置
[root@test01 ~]# vi /etc/ssh/sshd_config
改之前
37 #LoginGraceTime 2m
38 #PermitRootLogin yes
39 #StrictModes yes
40 #MaxAuthTries 6
41 #MaxSessions 10
改之后
37 #LoginGraceTime 2m
38 PermitRootLogin no
39 #StrictModes yes
40 #MaxAuthTries 6
41 #MaxSessions 10
改完后,我们重启一下ssh服务
[root@test01 ~]# service sshd restart
Redirecting to /bin/systemctl restart sshd.service
接着,我们回到test02上,再次尝试一下
[root@test01 ~]# exit
登出
Connection to 192.168.139.11 closed.
[root@test02 ~]# ssh root@192.168.139.11
root@192.168.139.11's password:
Permission denied, please try again. ///已经无法再次登录了///
root@192.168.139.11's password:
但是,如果我们登录test01中的另一个账号,再进行切换,依旧能登录root
[root@test02 ~]# ssh tom@192.168.139.11
tom@192.168.139.11's password:
[tom@test01 ~]$ su root
密码:
[root@test01 tom]#
对于这个风险,我们可以通过使用PAM认证模块,将加入whell中的用户允许使用su命令
[root@test01 ~]# vi /etc/pam.d/su
1 #%PAM-1.0
2 auth sufficient pam_rootok.so
3 # Uncomment the following line to implicitly trust users in the "wheel" group.
4 #auth sufficient pam_wheel.so trust use_uid
5 # Uncomment the following line to require a user to be in the "wheel" group.
6 auth required pam_wheel.so use_uid
7 auth substack system-auth
8 auth include postlogin
9 account sufficient pam_succeed_if.so uid = 0 use_uid quiet
10 account include system-auth
11 password include system-auth
12 session include system-auth
13 session include postlogin
14 session optional pam_xauth.so
我们可以将第6行,原先开头的#去掉即可
然后我们将tom用户加入wheel中,然后再分别测试一下tom和tom1
[root@test01 ~]# usermod -a -G wheel tom
[root@test01 ~]# ssh tom@192.168.139.11
tom@192.168.139.11's password:
Last login: Sun Jul 19 15:56:35 2020 from test01
[tom@test01 ~]$ su
密码:
[root@test01 tom]#
...//
[root@test01 ~]# ssh tom1@192.168.139.11
tom1@192.168.139.11's password:
Last login: Sun Jul 19 16:20:17 2020 from test01
[tom1@test01 ~]$ su - root
密码:
su: 拒绝权限
这边看到tom1已经无法使用su命令了
所以在修改PermitRootLogin参数时候别忘了启用PAM认证模块
接下来我们模拟一下密码输入错误
回到test02
[root@test02 ~]# ssh tom@192.168.139.11
tom@192.168.139.11's password:
Permission denied, please try again.
tom@192.168.139.11's password:
Permission denied, please try again.
tom@192.168.139.11's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
这边看看到,我们输入3次错误密码后,就拒绝登录了,但是我们之前MaxAuthTries设置的是6
这边注意,其实,系统默认的是3次,系统允许你连接的次数为3次,3次错误后就系统就拒绝登录了
现在我们通过命令,增加验证次数
[root@test02 ~]# ssh -o NumberOfPasswordPrompts=8 tom@192.168.139.11
tom@192.168.139.11's password:
Permission denied, please try again.
tom@192.168.139.11's password:
Permission denied, please try again.
tom@192.168.139.11's password:
Permission denied, please try again.
tom@192.168.139.11's password:
Permission denied, please try again.
tom@192.168.139.11's password:
Permission denied, please try again.
tom@192.168.139.11's password:
Received disconnect from 192.168.139.11 port 22:2: Too many authentication failures
Authentication failed.
[root@test02 ~]#
这边我们看到,现在的验证次数已经是超过3次,达到之前设置的6次了
黑名单和白名单设置
- AllowUsers:仅允许某些用户,拒绝其他所有人访问,适用于一些对安全性要求较高的场合
- DenyUsers:仅拒绝某些用户,允许其他所有人访问,使用于一些对安全性要求较低的场合
配置
[root@test01 ~]# vi /etc/ssh/sshd_config
...//
这边我在最末行加入
140 DenyUsers tom tom1@192.168.139.111
然后我们回到test02,验证一下
Connection to 192.168.139.11 closed.
[root@test02 ~]# ssh tom1@192.168.139.11
tom1@192.168.139.11's password:
Permission denied, please try again.
这边看到已经是无法登陆了
创建并使用密钥对登陆验证
首先,我们进入配置文件,开启密钥对的登陆功能
43 PubkeyAuthentication yes 将#去掉
47 AuthorizedKeysFile .ssh/authorized_keys
65 PasswordAuthentication yes
然后我们回到test02,生成密钥对
[root@test02 ~]# ssh-keygen -t ecdsa
Generating public/private ecdsa key pair.
Enter file in which to save the key (/root/.ssh/id_ecdsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_ecdsa.
Your public key has been saved in /root/.ssh/id_ecdsa.pub.
The key fingerprint is:
SHA256:BJAlCGKMRTFffnvTXUtPlb+71Z4XP8A8skQGituREnw root@test02
The key's randomart image is:
+---[ECDSA 256]---+
|===o+++ o|
|+..o.* E . ..|
| . = = . .+|
| o * . + ..o+|
| + S = + ..o|
| . . . + = o.|
| . o o *|
| . +=|
| o=|
+----[SHA256]-----+
[root@test02 ~]#ssh-copy-id -i id_ecdsa.pub tom@192.168.139.11
然后我们将公钥上传给test01
[root@test02 ~]# cd .ssh/
[root@test02 .ssh]# ssh-copy-id -i id_ecdsa.pub tom@192.168.139.11
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_ecdsa.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
tom@192.168.139.11's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'tom@192.168.139.11'"
and check to make sure that only the key(s) you wanted were added.
接下来我们回到test01,看下tom用户中是否已经有公钥
[root@test01 ~]# su - tom
上一次登录:日 7月 19 16:18:47 CST 2020从 test01pts/3 上
最后一次失败的登录:日 7月 19 16:54:13 CST 2020从 192.168.139.111ssh:notty 上
最有一次成功登录后有 18 次失败的登录尝试。
[tom@test01 ~]$ ls -a
. .. .bash_history .bash_logout .bash_profile .bashrc .mozilla .ssh
[tom@test01 ~]$ cd .ssh
[tom@test01 .ssh]$ ls
authorized_keys
[tom@test01 .ssh]$ cat authorized_keys
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBCubFPlTb8zn52ZsUqzTMwXxMKYKWB34oM2kS2DeZVZaIu5MvUVhbiWbOnrwIW0Curjx5koaKu4ZYYehLbnPRPo= root@test02 ///z这边看到,是test02发过来的
[tom@test01 .ssh]$
最后我们回到test02,通过密钥登陆
[root@test02 .ssh]# ssh tom@192.168.139.11
Enter passphrase for key '/root/.ssh/id_ecdsa': ///输入密钥密码
Last login: Sun Jul 19 16:56:31 2020
[tom@test01 ~]$
我们也可以设置免密登陆
[root@test02 .ssh]# ssh-agent bash
[root@test02 .ssh]# ssh-add
Enter passphrase for /root/.ssh/id_ecdsa:
Identity added: /root/.ssh/id_ecdsa (/root/.ssh/id_ecdsa)
再登陆尝试一下
[root@test02 .ssh]# ssh tom@192.168.139.11
Last login: Sun Jul 19 16:59:09 2020 from 192.168.139.111
[tom@test01 ~]$
scp远程复制
[root@test02 .ssh]# scp root@192.168.139.111:/etc/passwd /opt
The authenticity of host '192.168.139.111 (192.168.139.111)' can't be established.
ECDSA key fingerprint is SHA256:Pc//HIvi8cNob6+2zQ8I3LeDNQiwFW2okx20RmksmAg.
ECDSA key fingerprint is MD5:9b:23:68:5d:a7:9d:fe:9a:38:a3:5f:1a:23:17:7b:cd.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.139.111' (ECDSA) to the list of known hosts.
root@192.168.139.111's password:
passwd 100% 986 1.3MB/s 00:00
[root@test02 .ssh]#
scp远程上传
[root@test02 opt]# scp passwd tom1@192.168.139.11:/home/tom1
tom1@192.168.139.11's password:
passwd 100% 986 865.2KB/s 00:00
[root@test02 opt]#
sftp下载
[root@test02 opt]# sftp tom1@192.168.139.11
tom1@192.168.139.11's password:
Connected to 192.168.139.11.
sftp> get hewei1
Fetching /home/tom1/hewei1 to hewei1
sftp> bye
[root@test02 opt]# ll
总用量 4
-rw-r--r--. 1 root root 0 7月 19 17:15 hewei1
-rw-r--r--. 1 root root 986 7月 19 17:06 passwd
[root@test02 opt]#
TCP_Wrappers
简介
TCP_Wrappers是一个工作在第四层(传输层)的的安全工具,对有状态连接的特定服务进行安全检测并实现访问控制,凡是包含有libwrap.so库文件的的程序就可以受TCP_Wrappers
的安全控制。它的主要功能就是控制谁可以访问,常见的程序有rpcbind、vsftpd、sshd,telnet
工作原理
TCP_Wrappers有一个TCP的守护进程叫作tcpd。以ssh为例,每当有ssh的连接请求时,tcpd即会截获请求,先读取系统管理员所设置的访问控制文件,符合要求,则会把这次连接原封不动的转给真正的ssh进程,由ssh完成后续工作;如果这次连接发起的ip不符合访问控制文件中的设置,则会中断连接请求,拒绝提供ssh服务
TCP_Wrappers的使用
TCP_Wrappers的使用主要是依靠两个配置文件 /etc/hosts.allow,/etc/hosts.deny,用于拒绝和接受具有TCP_Wrappers控制全的程序,详细信息具体可以查看man帮助,这里我们就做简单的演示和使用(man 5 hosts_access, man 5 hosts_options)
要说明的是当我们启动一个受控制的软件的时候,比如ssh,
不过在刚开始的时候,/etc/hosts.allow,/etc/hosts.deny 什么都没有添加,此时没有限制,是都可以连接的,现在我们来说如何设置,禁止和允许连接
[root@test01 ~]# vi /etc/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.120.* ###192.168.120段的都能访问
~
[root@test01 ~]# vi /etc/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 ###禁止所有
验证一下
[root@test02 opt]# ssh root@192.168.139.11
ssh_exchange_identification: read: Connection reset by peer
这边看到,已经被无法登陆了
这边我们注意:
-
客户端地址列表
-
- 多个地址以逗号分隔,ALL表示所有地址
- 允许使用通配符?和*
- 网段地址,如192.168.3. 或者192.168.3.0/255.255.255.0
- 区域地址,如 .test.com
- 网段地址,如192.168.3. 或者192.168.3.0/255.255.255.0
- 允许使用通配符?和*
- 多个地址以逗号分隔,ALL表示所有地址
-
策略的应用程序
-
- 检查hosts.allow,找到匹配则允许访问
- 再检查hosts.deny,找到则拒绝访问
- 若两个文件中均无匹配策略,则默认允许访问
pers
- 若两个文件中均无匹配策略,则默认允许访问
- 再检查hosts.deny,找到则拒绝访问
- 检查hosts.allow,找到匹配则允许访问