Linux 远程访问与控制(OpenSSH、scp、sftp、密钥对创建TCP Wrappers)

一、SSH远程管理

1、OpenSSH服务器

  • SSH是一种安全通道协议
  • SSH对通信数据进行了加密处理,用于远程管理

2、OpenSSH

  • 服务名称:sshd
  • 服务主程序:/usr/sbin/sshd
  • 服务端配置文件:/etc/ssh/sshd_config

3、配置文件中一些重要的参数

(1)PermitRootLogin

为了方便识别,我们将两台虚拟机主机名分别命名为test01和test02

[root@localhost ~]# hostname test01
[root@localhost ~]# su
[root@test01 ~]# 
[root@localhost ~]# hostname test02
[root@localhost ~]# su
[root@test02 ~]# 

在test01中添加两个测试用户,分别为tom和tom1设置密码“123”

[root@test01 ~]# useradd tom
[root@test01 ~]# passwd tom
更改用户 tom 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。

[root@test01 ~]# passwd tom1
更改用户 tom1 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。

在test02中使用SSH连接到test01

[root@test02 ~]# ssh [email protected]     ## 使用SSH连接以root用户身份连接到test01
The authenticity of host '192.168.50.131 (192.168.50.131)' can't be established.
ECDSA key fingerprint is SHA256:m40nG2/f7g8IXeEQlA7BVGbVYhlYki8I+VTQphKdraw.
ECDSA key fingerprint is MD5:22:e5:97:c2:ae:42:56:4f:03:a9:40:f2:2e:85:f0:5f.
Are you sure you want to continue connecting (yes/no)? yes     ##  输入yes确认登录
Warning: Permanently added '192.168.50.131' (ECDSA) to the list of known hosts.
[email protected]'s password:           ## 输入test01中root用户的密码
Last login: Sat Jul 11 18:14:09 2020
[root@test01 ~]#               ## 登录成功

如果不想让别人用root用户进行远程登录呢?

## 修改ssh配置文件
[root@test01 ~]# vi /etc/ssh/sshd_config

37 #LoginGraceTime 2m         ## 会话时间,设置指定时间内没有成功登录,将会断开连接,若无单位则默认时间为秒
38 #PermitRootLogin yes       ## 是否允许root登录,默认为允许
39 #StrictModes yes           ## 设置ssh在接收登录请求之前是否检查家目录及rhosts文件权限和所有权
40 #MaxAuthTries 6            ## 密码最大验证次数
41 #MaxSessions 10            ## 最大会话连接数量

## 将第38行的注释取消,将yes改为no即可,重启服务

[root@test01 ~]# service sshd restart
Redirecting to /bin/systemctl restart sshd.service

## 再次尝试以root用户登陆test01

[root@test01 ~]# ssh [email protected]
The authenticity of host '192.168.50.131 (192.168.50.131)' can't be established.
ECDSA key fingerprint is SHA256:m40nG2/f7g8IXeEQlA7BVGbVYhlYki8I+VTQphKdraw.
ECDSA key fingerprint is MD5:22:e5:97:c2:ae:42:56:4f:03:a9:40:f2:2e:85:f0:5f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.50.131' (ECDSA) to the list of known hosts.
[email protected]'s password: 
Permission denied, please try again.   ## 权限拒绝,已经不可以登陆了

但是这样做也会有很严重的漏洞存在,如果test01中有其他用户,而我又知道其中某个用户的密码,那么使用su命令不就可以切换到root了么,我们来验证一下:

## 上一步继续操作,已知test01中有一个用户tom密码为123,现在test02上以tom用户登陆test01

[root@test01 ~]# ssh [email protected]
[email protected]'s password: 
[tom@test01 ~]$      ## 登陆成功

## 使用su命令切换用户

[tom@test01 ~]$ su -
密码:
上一次登录:六 7月 11 18:39:18 CST 2020从 192.168.50.132pts/2 上
最后一次失败的登录:六 7月 11 18:39:59 CST 2020从 test01ssh:notty 上
最有一次成功登录后有 1 次失败的登录尝试。
[root@test01 ~]#         ## 切换root成功!

如果仅修改了PermitRootLogin参数,还是有很大风险可以从其他用户非法切换到root用户的,那如何解决这个问题?很简单,在之前的文章中我们提到了PAM认证模块,将PAM模块开启,只有加入wheel组的用户可以使用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      ## 将这一行的注释符号去掉即可开启PAM验证功能
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

将tom用户添加到wheel组

[root@test01 ~]# usermod -a -G wheel tom
[root@test01 ~]# id tom
uid=1000(tom) gid=1000(tom) 组=1000(tom),10(wheel)
[root@test01 ~]# id tom1    ## 用户tom1不属于wheel组
uid=1001(tom1) gid=1001(tom1) 组=1001(tom1)

在test01上分别使用tom与tom1登录test01

[tom@test01 ~]$ ssh [email protected]
[email protected]'s password: 
Last login: Sat Jul 11 18:44:38 2020 from test01
[tom@test01 ~]$ su -
密码:
上一次登录:六 7月 11 18:45:20 CST 2020pts/3 上
最后一次失败的登录:六 7月 11 20:36:51 CST 2020pts/0 上
最有一次成功登录后有 1 次失败的登录尝试。
[root@test01 ~]# 
[tom@test01 ~]$ ssh [email protected]
The authenticity of host '192.168.50.131 (192.168.50.131)' can't be established.
ECDSA key fingerprint is SHA256:m40nG2/f7g8IXeEQlA7BVGbVYhlYki8I+VTQphKdraw.
ECDSA key fingerprint is MD5:22:e5:97:c2:ae:42:56:4f:03:a9:40:f2:2e:85:f0:5f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.50.131' (ECDSA) to the list of known hosts.
[email protected]'s password: 
[tom1@test01 ~]$ su -
密码:
su: 拒绝权限

从上面的实验可以看出,加入wheel组的用户tom可以切换root权限,而tom1不行,所以在修改PermitRootLogin参数时候别忘了启用PAM认证模块

(2)MaxAuthTries

在test02上使用tom账户登录test1,我们模拟密码输入错误:

[root@test02 ~]# ssh [email protected]
[email protected]'s password: 
Permission denied, please try again.
[email protected]'s password: 
Permission denied, please try again.
[email protected]'s password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

从上面的结果可以看出,三次错误的密码输入就被系统拒绝登录了,但明明MaxAuthTries设置的是6啊?

其实,系统默认的是三次,系统允许你连接的次数为三次,三次错误后就系统就拒绝登录了

但我们可以通过如下命令来增加密码尝试次数,是该参数生效

[root@test02 ~]# ssh -o NumberOfPasswordPrompts=8 [email protected]

[email protected]'s password: 
Permission denied, please try again.
[email protected]'s password: 
Permission denied, please try again.
[email protected]'s password: 
Permission denied, please try again.
[email protected]'s password: 
Permission denied, please try again.
[email protected]'s password: 
Permission denied, please try again.
[email protected]'s password: 
Received disconnect from 192.168.50.131 port 22:2: Too many authentication failures
Authentication failed.

## 现在密码最大尝试次数就变为配置文件中设置的6次了

4、黑白名单设置(AllowUsers、DenyUsers)

  • AllowUsers:仅允许某些用户,拒绝其他所有人访问,适用于一些对安全性要求较高的场合
  • DenyUsers:仅拒绝某些用户,允许其他所有人访问,使用于一些对安全性要求较低的场合

现在我们尝试操作一下:

## 编辑配置文件
[root@test01 ~]# vi /etc/ssh/sshd_config 

## 搜索DenyUsers发现并没有这个设置选项存在,这时候就需要我们自己手动插入了,这里我就从42行开始插入:
37 #LoginGraceTime 2m
38 PermitRootLogin no
39 #StrictModes yes
40 #MaxAuthTries 6
41 #MaxSessions 10
42 DenyUsers tom [email protected]
PS:含义:不允许tom用户任意终端进行访问,不允许tom1从192.168.50.131终端进行访问

## 配置完成后别忘了重启服务:
[root@test01 ~]# systemctl restart sshd

尝试从test02上登录test01上的tom1用户:

[root@test02 ~]# ssh [email protected]
[email protected]'s password: 
Permission denied, please try again.    ## 提示权限拒绝,无法登陆

5、创建并使用密匙对验证登录服务器

首先要进入配置文件开启没密钥登陆功能:

[root@test01 ~]# vi /etc/ssh/sshd_config 

## 开启密钥登陆:(分别开启第43、47、65行)
43 PubkeyAuthentication yes
47 AuthorizedKeysFile      .ssh/authorized_keys
65 PasswordAuthentication yes

## 重启SSH服务
[root@test01 ~]# systemctl restart sshd

生成密钥对:

[root@test02 ~]# ssh-keygen -t ecdsa    ## 生成秘钥对,算法为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:fTZDO4HsCT4BT1lun9FAP2cBKK0Tr5E0KT02J5uDtPI root@test02
The key's randomart image is:
+---[ECDSA 256]---+
|      . oo+.+... |
|       *.# = +  .|
|      . X ^ + + o|
|     . + # = = + |
|      o S O X    |
|       E o o +   |
|                 |
|                 |
|                 |
+----[SHA256]-----+

这时候我们查看一下家目录下是否已经创建了秘钥:

[root@test02 ~]# ll -a
总用量 40
dr-xr-x---.  6 root root  256 7月  11 20:45 .
dr-xr-xr-x. 17 root root  224 7月   6 22:54 ..
-rw-------.  1 root root 1903 7月   6 22:55 anaconda-ks.cfg
-rw-------.  1 root root   59 7月  11 20:45 .bash_history
-rw-r--r--.  1 root root   18 12月 29 2013 .bash_logout
-rw-r--r--.  1 root root  176 12月 29 2013 .bash_profile
-rw-r--r--.  1 root root  176 12月 29 2013 .bashrc
drwx------.  4 root root   31 7月   6 23:39 .cache
drwxr-xr-x.  3 root root   18 7月   6 23:39 .config
-rw-r--r--.  1 root root  100 12月 29 2013 .cshrc
drwx------.  3 root root   25 7月   6 23:30 .dbus
-rw-r--r--.  1 root root 1951 7月   6 23:30 initial-setup-ks.cfg
drwx------.  2 root root   61 7月  11 21:23 .ssh      ## 这个就是
-rw-r--r--.  1 root root  129 12月 29 2013 .tcshrc
-rw-------.  1 root root  132 7月   6 23:39 .xauth2eg07U
-rw-------.  1 root root  119 7月  11 20:45 .Xauthority

## 进入ssh目录,查看目录内文件
[root@test02 ~]# cd .ssh/

[root@test02 .ssh]# ls
id_ecdsa  id_ecdsa.pub  known_hosts     ## 以pub结尾的就是公钥,另一个就是私钥

秘钥对已经生成,下一步就是讲公钥上传到服务端

[root@test02 .ssh]# ssh-copy-id -i id_ecdsa.pub [email protected]

/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
[email protected]'s password:       ## 输入tom用户的密码

Number of key(s) added: 1     ## 秘钥添加成功

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

进入tom用户家目录下查看:

[tom@test01 ~]$ ls -a
.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc  .ssh
[tom@test01 ~]$ cd .ssh/
[tom@test01 .ssh]$ ls
authorized_keys  known_hosts     ## 秘钥已经上传

## 查看一下公钥内容:
[tom@test01 .ssh]$ cat authorized_keys 
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBB6eSY2pLpU2MM2xT8hGpM5ogSVOLCbgtHxNnzAIg+lsxG95kkSV8LN++6q9IhRpTxyXdt86bs5hznP+JfWxpgk= root@test02

## 可以看出是test02的root用户上传的

使用密钥对进行登陆:

[root@test02 .ssh]# ssh [email protected]      
Enter passphrase for key '/root/.ssh/id_ecdsa':      ## 提示输入密钥对的密码
Last login: Sat Jul 11 21:31:11 2020
[tom@test01 ~]$                         ## 登陆成功

但是,使用密匙登录后还是每次都要验证密码很麻烦,是否可以免密登陆呢?

当然可以,只要在输入以下命令即可:

[root@test02 .ssh]# ssh-agent bash    ## 代理bash终端
[root@test02 .ssh]# ssh-add          ## 添加免密登陆的密码
Enter passphrase for /root/.ssh/id_ecdsa:       ## 输入创建秘钥对时的密码
Identity added: /root/.ssh/id_ecdsa (/root/.ssh/id_ecdsa)

现在再次尝试从test02登陆test01的tom账户

[root@test02 .ssh]# ssh [email protected]
Last login: Sat Jul 11 21:38:31 2020 from 192.168.50.132
[tom@test01 ~]$                       ## 登陆成功,没有提示验证密码

6、使用scp命令进行远程复制

将test01 /etc/passwd文件远程复制到test02的/opt目录下

[root@test02 .ssh]# scp [email protected]:/etc/passwd /opt
[email protected]'s password:    ## 验证test01的root密码
passwd                                                                                                        100%  922   328.5KB/s   00:00    

进入opt目录下查看:

[root@test02 .ssh]# cd /opt
[root@test02 opt]# ll
总用量 4
-rw-r--r--. 1 root root 922 7月  11 21:55 passwd     ## 复制成功
drwxr-xr-x. 2 root root   6 10月 31 2018 rh

既然能从别的服务器复制,那也可以上传:

将刚才的passwd文件传到test01的tom1家目录下

[root@test02 opt]# scp passwd [email protected]:/home/tom1
[email protected]'s password:   ## 验证用户tom1的密码
passwd    

## 进入test01的tom1家目录下查看是否有passwd文件

[root@test01 ~]# cd /home/tom1
[root@test01 tom1]# ll
总用量 4
-rw-r--r--. 1 tom1 tom1 922 7月  11 21:59 passwd

【注意】:如果要远程复制或上传的是目录,则在scp命令后跟-r选项即可

7、使用sftp命令上传或下载文件

从test01的tom1用户的家目录下载文件abc到test02中

[root@test02 opt]# sftp [email protected]

[email protected]'s password:        ## 验证用户tom1的密码
Connected to 192.168.50.131.
sftp> get abc     ## 下载abc文件大本地当前目录(如果上传用put命令)
Fetching /home/tom1/abc to abc
/home/tom1/abc                                                                                                100%  922   362.7KB/s   00:00    
sftp> bye        ## 退出远程登录

[root@test02 opt]# ll
总用量 8
-rw-r--r--. 1 root root 922 7月  11 22:08 abc   ## 已经下载到本地了
-rw-r--r--. 1 root root 922 7月  11 21:55 passwd
drwxr-xr-x. 2 root root   6 10月 31 2018 rh

二、TCP Wrappers访问控制

1、TCP Wrappers原理

TCP_Wrappers是一个工作在第四层(传输层)的的安全工具,对有状态连接的特定服务进行安全检测并实现访问控制,凡是包含有libwrap.so库文件的的程序就可以受TCP_Wrappers的安全控制。它的主要功能就是控制谁可以访问,常见的程序有rpcbind、vsftpd、sshd,telnet。直接编辑/etc/hosts.allow和/etc/hosts.deny这两个文件就可以实现主机、ip、服务等控制

2、保护机制的实现方式

  • 方式一:通过tcpd程序对其他服务程序进行包装
  • 方式二:由其他服务程序调用libwrap.so*链接库

3、访问控制策略配置文件路径

  • /etc/hosts.allow
  • /etc/hosts.deny

4、设置访问控制策略

  • 策略格式:服务程序列表:客户端地址列表

  • 服务程序列表:

    多个服务端以逗号分隔,ALL表示所有服务

  • 客户端地址列表

    1. 多个地址以逗号分隔,ALL表示所有地址
    2. 允许使用通配符?和*
    3. 网段地址,如192.168.3. 或者192.168.3.0/255.255.255.0
    4. 区域地址,如 .test.com
  • 策略的应用程序

    1. 检查hosts.allow,找到匹配则允许访问
    2. 再检查hosts.deny,找到则拒绝访问
    3. 若两个文件中均无匹配策略,则默认允许访问

配置示例:

## 白名单设置:
[root@test01 ~]# vi /etc/hosts.allow    ##编辑白名单
sshd:49.53.62.17,192.168.3.*     ## 仅允许49.53.62.17和192.168.3段的IP访问ssh

## 黑名单设置:
[root@test01 ~]# vi /etc/hosts.deny   ## 编辑黑名单
sshd:ALL     ## 禁止其他所有地址访问

## 再次尝试登陆:
[root@test02 ~]# ssh [email protected]
ssh_exchange_identification: read: Connection reset by peer    ## 权限拒绝

猜你喜欢

转载自blog.csdn.net/u014042047/article/details/107298408
今日推荐