Linux网络服务——远程访问及控制(SSH远程管理+TCP Wrappers)

SSH远程管理

SSH(Secure Shell)是一种安全通道协议,主要用来实现字符界面的远程登录、远程复制等功能。SSH协议对通信双方的数据传输进行加密处理,其中包括用户登录时输入的用户口令。比以往的Telnet(远程登录)、RSH(远程执行命令)等传统的方式相比,SSH协议提供了更好的安全性

OpenSSH服务器

OpenSSH 是 SSH (Secure SHell) 协议的免费开源实现。SSH协议族可以用来进行远程控制, 或在计算机之间传送文件。而实现此功能的传统方式,如telnet(终端仿真协议)、 rcp ftp、 rlogin、rsh都是极为不安全的,并且会使用明文传送密码。OpenSSH提供了服务端后台程序和客户端工具,用来加密远程控制和文件传输过程中的数据,并由此来代替原来的类似服务。

  • SSH(Secure Shell)协议
    是一种安全通道协议
    对通信数据进行了加密处理,用于远程管理
  • OpenSSH
    服务名称:sshd
    服务端主程序:/usr/sbin/sshd
    服务端配置文件: /etc/ssh/sshd_config
  • 服务监听选项
[root@localhost ~]# vim /etc/ssh/sshd_config
......
Port 22                        #端口号     
ListenAddress 172.16.16.22     #监听地址
Protocol 2                     #版本
UseDNS no                      #禁用反向解析 
  • 用户登录控制
[root@localhost ~]# vim /etc/ssh/sshd_config
LoginGraceTime 2m             #会话时间
PermitRootLogin no            #允许root用户进行登录 no
MaxAuthTries 6                #最大的验证尝试次数6次
PermitEmptyPasswords no       #允许空密码登录 no
MaxSessions 10                #允许10个终端连接
......
AllowUsers jerry [email protected]   #允许该用户从固定终端登录

以下需要自己添加
AllowUsers 白名单 :仅允许登陆
DenyUsers 黑名单 :仅拒绝登陆
  • 登录验证方式
    密码验证:核对用户名、密码是否匹配
    密钥对验证:核对客户的私钥、服务端公钥是否匹配
[root@localhost ~]# vim /etc/ssh/sshd_config
······

#PubkeyAuthentication yes		#密钥对验证开启   是
#PasswordAuthentication yes		#身份密码验证 是
AuthorizedKeysFile      .ssh/authorized_keys	#密钥对公钥库文件路径

SSH远程登录

pan1 IP:192.168.110.132(修改配置文件机)
pan2 IP: 192.168.110.129(验证机)

[root@pan1 ~]# rpm -qc openssh		#默认已安装
/etc/ssh/moduli
[root@pan2 ~]# ssh [email protected]		#ssh命令远程登录   用户@目标IP
The authenticity of host '192.168.110.132 (192.168.110.132)' can't be established.
ECDSA key fingerprint is SHA256:mazgSz4fUm5m6V5h7SJvbBB2f+w6WjsEPsZVIRRtO54.
ECDSA key fingerprint is MD5:ab:b5:87:fc:f9:d1:14:b2:6e:00:12:0a:ff:49:63:cf.
Are you sure you want to continue connecting (yes/no)?yes	#输入yes
Warning: Permanently added '192.168.110.132' (ECDSA) to the list of known hosts.
[email protected]'s password: 					#用户输入密码
Last login: Mon Jul 13 08:33:08 2020
[root@pan1 ~]# exit		#退出远程登录
登出
Connection to 192.168.110.132 closed.
[root@pan2 ~]# 

链接后可以根据用户的权限进行对应的修改 维护

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

[root@pan1 ~]# vim /etc/ssh/sshd_config

#       $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $

# This is the sshd server system-wide configuration file.  See
........省略内容
#Port 22			#默认端口是22,可以自己修改后把#注释符去掉
#AddressFamily any
#ListenAddress 0.0.0.0				#监听的ipv4地址,默认监听所有
#ListenAddress ::					#ipv6的

Port

查看ssh链接的端口
[root@pan2 ~]# netstat -anpt | grep ssh
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      9248/sshd           
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      11825/sshd: root@pt 
tcp        0     52 192.168.110.129:22      192.168.110.1:50367     ESTABLISHED 11825/sshd: root@pt 
tcp6       0      0 :::22                   :::*                    LISTEN      9248/sshd           
tcp6       0      0 ::1:6010                :::*                    LISTEN      11825/sshd: root@pt 
[root@pan2 ~]# 

如果修改了端口要加-p 端口号进行远程链接
比如端口改成123
在这里插入图片描述

[root@pan1 ~]# setenforce 0
[root@pan1 ~]# iptables -F
[root@pan1 ~]# systemctl restart sshd	#修改完后要重启服务
[root@pan2 ~]# ssh -p 123 [email protected]
[email protected]'s password: 
Last failed login: Mon Jul 13 09:15:13 CST 2020 from 192.168.110.129 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Mon Jul 13 08:53:15 2020 from 192.168.110.129
[root@pan1 ~]# 

PermitRootLogin

[root@pan1 ~]# vim /etc/ssh/sshd_config
.....省略内容
 #LoginGraceTime 2m         # 指定时间内没有输入密码,后面输对了也不会让你链接
 #PermitRootLogin yes       # 是否允许root登录,默认为允许
 #StrictModes yes           
 #MaxAuthTries 6            # 密码最大验证次数
 #MaxSessions 10            # 最大连接数量
 PermitRootLogin 
 这个有个弊端,如果改成不允许root登录,他可以先登录其他的用户再利用其他用户切换到root
 解决方法:
 PAM认证模块,将PAM模块开启,只有加入wheel组的用户才可以可以使用su命令
 [root@pan1 ~]# vi /etc/pam.d/su

在这里插入图片描述

在pan2使用没在wheel组的hao用户登录pan1 来验证是否可以用来切换到root
[root@pan1 ~]# useradd hao
[root@pan1 ~]# passwd hao
更改用户 hao 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
————————————————————————————————
[root@pan2 ~]# ssh -p 123 [email protected]
[email protected]'s password: 
[hao@pan1 ~]$ su - root
密码:
su: 拒绝权限

修改完 PermitRootLogin no后也要记得修改PAM认证模块,不然等于没设

MaxAuthTries
密码验证最大为6我们来试试是不是6次

[root@pan2 ~]# ssh -p 123 [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).
[root@pan2 ~]# 
事实证明三次就被踢出来了 系统默认是三次
这条命令可以增加次数
[root@pan2 ~]# ssh -p 123 -o NumberOfPasswordPrompts=8 [email protected]  #密码验证次数成了6次
如果没改端口号就不用-p 端口号,直接-o就行

AllowUsers白名单:仅允许某些用户,拒绝所有人 用在安全性场合比较高的
DenyUsers黑名单:仅拒绝某些用户,允许所有人 安全性场合低

这两个配置文件里没有的要自己添加
在这里插入图片描述
pan 可以在任何设备登录 hao只能在指定IP设备登录 其他任何账号都登录不了

构建密钥对验证的SSH体系
在这里插入图片描述
需要开启这三条

[root@pan2 ~]# 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:vsGLGiaFLy07tqlzw3/sPXCeXIThv5YyyYI2ntBiAZw root@pan2
The key's randomart image is:
+---[ECDSA 256]---+
|                 |
|. .     .        |
|.E     . o       |
| . .    o .      |
|  o .   So       |
|   *  .o. o      |
| .B *o *++ o     |
|..*X+o+oX++      |
|.=+B==o.+=       |
+----[SHA256]-----+
[root@pan2 ~]# ls -a
.                .bash_logout   .cache   .dbus          initial-setup-ks.cfg  .pki     .Xauthority  视频  下载
..               .bash_profile  .config  .esd_auth      .local                .ssh     公共         图片  音乐
anaconda-ks.cfg  .bashrc        .cshrc   .ICEauthority  .mozilla              .tcshrc  模板         文档  桌面
[root@pan2 ~]# ls .ssh/			#存放密钥的位置
id_ecdsa (私钥) id_ecdsa.pub (公钥) known_hosts
[root@pan2 ~]# cd .ssh/
[root@pan2 .ssh]# ssh-copy-id -i id_ecdsa.pub [email protected]				#拷贝公钥到pan1的hao用户
/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:    #输入hao密码

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.
—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
[root@pan1 ~]# cd /home/hao/
[root@pan1 hao]# ls -a
.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc  .cache  .config  .mozilla  .ssh
[root@pan1 hao]# ls .ssh/
authorized_keys
[root@pan1 hao]# cat .ssh/authorized_keys 
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBCnGyR5RUaWaf2aL0Nx+B5C3nRxE+n2EjUsDcqjRKNF5hGQ8aAE9HmGRWuaJWtpCMBl+06147azshxU/PnBpDwI= root@pan2
—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
[root@pan2 .ssh]# ssh [email protected]
Enter passphrase for key '/root/.ssh/id_ecdsa': 				##再登录时用的是密钥密码而不是用户的密码了
Last login: Mon Jul 13 10:01:03 2020 from 192.168.110.129
[hao@pan1 ~]$ 
添加免交互密码
[root@pan2 .ssh]# ssh-agent bash
[root@pan2 .ssh]# ssh-add
Enter passphrase for /root/.ssh/id_ecdsa: 
Identity added: /root/.ssh/id_ecdsa (/root/.ssh/id_ecdsa)
[root@pan2 .ssh]# ssh [email protected]
Last login: Mon Jul 13 10:46:41 2020 from 192.168.110.129
[hao@pan1 ~]$ 

scp命令-远程安全复制

scp复制文件命令分为两种:
scp user@host:file1 file2      #从服务端复制文件到客户端
scp file1 user@host:file2      #从客户端复制文件到服务端
参数 -r        #递归复制

[root@pan2 .ssh]# scp [email protected]:/etc/passwd /opt/		#复制pan1 里的目录到pan2 opt里
[email protected]'s password: 
passwd                                                                                               100% 2383     2.5MB/s   00:00    
[root@pan2 .ssh]# ls /opt/
passwd  rh
[root@pan2 .ssh]# 
(白名单里没有root记得删除白名单那一行)
[root@pan2 .ssh]# scp /opt/passwd [email protected]:/opt/
[email protected]'s password: 
passwd                                                                                               100% 2383   874.7KB/s   00:00

[root@pan1 opt]# ls /opt/
passwd  qq  rh

sftp命令-安全ftp上传下载

[root@pan2 .ssh]# sftp [email protected]			#使用sftp进入pan1
sftp> cd /opt/qq/
sftp> ls
111.txt  
sftp> get 111.txt			#get命令下载文件到进入时的目录
Fetching /opt/qq/111.txt to 111.txt
/opt/qq/111.txt                                                       100%   26    20.3KB/s   00:00    
sftp> cd /opt/
sftp> put passwd 		#put命令帮自己文件上传到pan1
Uploading passwd to /root/passwd
passwd                                                                100% 2383   830.0KB/s   00:00    

退出命令:quit、exit、bye
在这里插入图片描述
在这里插入图片描述

TCP Wrappers

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

  • 保护机制的实现方式
    方式1:通过tcpd程序对其他服务程序进行包装
    方式2:由其他服务程序调用libwrap.so. *链接库

  • 访问控制策略的配置文件
    /etc/hosts. allow
    /etc/hosts.deny

  • 设置访问控制策略

    • 策略格式:服务程序列表:客户端地址列表
    • 服务程序列表
      多个服务以逗号分隔,ALL表示所有服务
    • 客户端地址列表
      多个地址以逗号分隔,ALL表示所有服务
      允许使用通配符*和?
      网段地址,如192.168.1 或者 192.168.1.0/255.255.255.0
      区域地址,如.benet.com
  • 策略的应用顺序
    检查hosts.allow,找到匹配则允许访问
    再检查hosts.deny,找到则拒绝访问
    若两个文件中均无匹配策略,则默认允许访问
    在这里插入图片描述
    在这里插入图片描述
    先读取allow,后读取deny
    在这里插入图片描述
    把这网段添加到黑名单后
    在这里插入图片描述
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/CN_PanHao/article/details/107303895