Linux网络服务-远程访问及控制

一、SSH远程管理★★★

1.1 配置Open SSH服务端

1.1.1 OpenSSH服务器

SSH(Sucure Shell)协议

  • 是一种安全通道协议
  • 为客户机提供安全的shell环境,对通信数据进行了加密处理,用于远程管理
  • 默认端口:TCP 22
    密钥简介
    • 对称密钥:加密和解密用同一个密钥。如:AES、DES、3DES,加密速度快,但是不安全
    • 非对称密钥:加密和解密使用不同密钥,分为公钥和私钥,不可以从其中一种密钥推导出另外一个密钥。如:rsa。加密速度慢,但是安全。
      • 私钥:验证身份,保密的
      • 公钥:所有人都知道,公开的
      • Hash 哈希:验证数据完整性

OpenSSH

  • 服务名称:sshd

  • 服务端主程序:/usr/sbin/sshd

  • 服务端配置文件:/etc/ssh/sshd_config

    • /etc/ssh/ssh_config:客户端配置文件
    • /etc/ssh/sshd_config:服务端配置文件
访问形式 端口号
SSH:密文访问默认端口 TCP:22,一般广域网
Telnet:明文形式的访问 TCP 23,一般局域网
远程桌面 3389,图形化界面
名称 作用
mstsc (cmd命令提示符输入mstsc,根据提示操作) 微软中远程桌面的形式,只可一个用户一个终端登录,可复制文件,微软对微软
VNC 跨终端远程软件
teanviewer 远程访问软件

基本操作
开启两台centos 7虚拟机,地址分别为20.0.0.61和20.0.0.41,设置主机名分别为test01和test02方便区分

[root@localhost ~]# hostnamectl set-hostname test01     '设置主机名(永久)'
[root@localhost ~]# su    '切换' 
[root@test01 ~]# 
[root@test01 ~]# grep "bash$" /etc/passwd   '查看test01上能登录的用户'
root:x:0:0:root:/root:/bin/bash
yang:x:1000:1000:yang:/home/yang:/bin/bash


[root@localhost ~]# hostnamectl set-hostname test02
[root@localhost ~]# su
[root@test02 ~]# 

下面用test02去远程登录test01的root用户

[root@test02 ~]# ssh [email protected]      'test02远程登录test01的root用户'
The authenticity of host '20.0.0.61 (20.0.0.61)' can't be established.
ECDSA key fingerprint is SHA256:EA2z2a/Ha3Zkas+OoAZUF1lFnaXqcdgM17GtJynxzx8.
ECDSA key fingerprint is MD5:e2:fd:f0:5f:1d:16:f0:36:8e:49:b2:ae:73:e7:6b:26.
Are you sure you want to continue connecting (yes/no)? yes     '输入yes,继续操作'
Warning: Permanently added '20.0.0.61' (ECDSA) to the list of known hosts.
[email protected]’s password:        '输入root账户的密码'
Last login: Thu Jul  9 09:56:57 2020 from 20.0.0.1
[root@test01 ~]#        '切换成功'


'在test02中对test01进行操作'
[root@test01 ~]# touch /opt/test.txt   '在/opt目录下创建一个test.txt文档'

'进入20.0.0.61主机,查看/opt目录,有刚刚在test02上创建的test.txt'
[root@test01 ~]# ls /opt
rh  test.txt

[root@test01 ~]# exit  '退出当前bash环境'
登出
Connection to 20.0.0.61 closed.

服务监听选项

  • 端口号,协议版本,监听IP地址

  • 禁用反向解析

[root@localhost ~]# vim /etc/ssh/sshd_config    '进入服务端配置文件编辑'
......
 17 #Port 22  	'端口号可以修改'
 18 #AddressFamily any
 19 #ListenAddress 0.0.0.0    	'监听IP地址可修改'
 20 #ListenAddress ::

protocol 2 '协议版本'
UseDNS no  '禁用反向解析' 

用户登录控制

  • 禁止root用户,空密码用户
  • 限制登录验证时间、重试次数
  • AllowUsers、DenyUsers
    • AllowUsers(白名单,仅允许,只有这些可以登录)
    • DenyUsers(黑名单,仅拒绝,只有这些不行)
    • AllowUsers不可与DenyUsers同时使用
[root@localhost ~]# vim /etc/ssh/sshd_config   '进入服务端配置文件编辑'
......
 LoginGraceTime 2m   '会话时间(一定要设置)'
 PermitRootLogin yes  '允许root用户登录(一般情况设置为no,管理员不能随意登录)'
 StrictModes yes   '权限验证'
 MaxAuthTries 6    '最大重试次数:6次(其实真正默认重试为3次,另外三次可通过其他路径重试登录)'
 MaxSessions 10
 PermitEmptyPasswords no  '允许空密码登录:no(不免密登录)'

如:
AllowUsers lisi [email protected]	'允许lisi用户在任何终端登录,仅允许admin用户在终端192.168.20.30登录'
先在test01中禁止root远程登录,在test02上测试
[root@localhost ~]# vim /etc/ssh/sshd_config   '进入服务端配置文件编辑'
  PermitRootLogin no   '禁止管理员登录'
[root@test01 ~]# systemctl restart sshd   '重启sshd服务'

[root@test02 ~]# ssh [email protected]    '远程登录test01的root账户'
[email protected]'s password:    
Permission denied, please try again.    '输入密码后被拒绝权限,无法登录'
[email protected]'s password:     
'Ctrl+c退出'
[root@test02 ~]# 

这样操作禁止root用户登录其实并不安全。
在test01上换一个普通用户wangwu,用test02远程连接wangwu,再在test02上使用su - root 命令切换root用户,一样可以切换进去
[root@test02 ~]# ssh [email protected]   '远程连接wangwu'
[email protected]'s password: 
[wangwu@test01 ~]$ su - root     '切换到root用户'
密码:    '输入密码'
上一次登录:四 7月  9 10:43:28 CST 2020从 20.0.0.41pts/2 上
最后一次失败的登录:四 7月  9 11:57:54 CST 2020从 20.0.0.41ssh:notty 上
最有一次成功登录后有 1 次失败的登录尝试。
[root@test01 ~]#     '一样可以登录'
所以这样操作并不安全。

解决办法:
要想彻底禁止root被随意远程登录,使用pam认证模块验证su切换功能
[root@test01 ~]# vim /etc/pam.d/su   '使用pam认证模块验证su切换功能'
......
  6 auth           required        pam_wheel.so use_uid      '把第六行的#去掉'
......'保存退出'

在test 02上远程登录test01用户wangwu
[root@test02 ~]# ssh [email protected]
[email protected]'s password: 
Last login: Thu Jul  9 12:05:39 2020 from 20.0.0.41
[wangwu@test01 ~]$ su - root
密码:
su: 拒绝权限    '此时已经无法切换'
[wangwu@test01 ~]$ 

此时在test01中添加一个zhangsan用户,把zhangsan添加到wheel组中
[root@test01 ~]# useradd zhangsan 
[root@test01 ~]# passwd zhangsan
更改用户 zhangsan 的密码 。
新的 密码:
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@test01 ~]# gpasswd -a zhangsan wheel   '把zhangsan添加到wheel组'
正在将用户“zhangsan”加入到“wheel”组中
[root@test01 ~]# id zhangsan
uid=1002(zhangsan) gid=1002(zhangsan) 组=1002(zhangsan),10(wheel)

这时在test02中远程连接test01的zhangsan用户,看能否切换root:
[root@test02 ~]# ssh [email protected]
[email protected]'s password: 
Last failed login: Thu Jul  9 17:10:27 CST 2020 from 20.0.0.41 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Thu Jul  9 17:09:53 2020 from 20.0.0.41
[zhangsan@test01 ~]$ su - root
密码:
上一次登录:四 7月  9 12:05:48 CST 2020pts/2 上
最后一次失败的登录:四 7月  9 17:10:05 CST 2020pts/2 上
最有一次成功登录后有 3 次失败的登录尝试。
[root@test01 ~]# 
'能够切换到root'
[root@test01 ~]# vim /etc/ssh/sshd_config
......
MaxAuthTries 6   '最大重试次数:6次(其实真正默认重试为3次,另外三次可通过其他路径重试登录)'
.....

[root@test02 ~]# ssh -o NumberOfPasswordPrompts=8 [email protected]   '使用这条命令增加重试次数为8次,默认连接次数为6次,6次后退出'
[email protected]'s password: 

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

[root@test01 ~]# vim /etc/ssh/sshd_config 
......
AllowUsers zhangsan [email protected]  '(需要自己添加一行)允许zhangsan用户在任何终端访问,只允许wangwu在20.0.0.61终端访问'

1.1.2 OpenSSH服务

登录验证对象

  • 服务器中的本地用户账号

登录验证方式

  • 密码验证:核对用户名,密码是否匹配

  • 密钥对验证:核对客户的私钥,服务端公钥是否匹配

密钥对:包含公钥,私钥

  • 公钥:服务器使用

  • 私钥:客户保留

  • 非对称秘钥:RSA

  • 对称秘钥:3DES,AES

[root@test01 ~]# vim /etc/ssh/sshd_config
PasswordAuthentication yes		'是否使用密码'
PermitEmptyPasswords no	'禁止空密码'
PasswordAuthentication yes	'是否需要密码验证'
    
PubkeyAuthentication yes	'开启公钥验证'

AuthorizedKeysFile      .ssh/authorized_keys  '指定公钥库位置'

1.2 使用SSH客户端程序

1.2.1 SSH客户端程序命令

  • ssh命令–远程安全登录
    命令基本格式
    ssh user@host(端口选项:-p 22)
例如:
[root@test02 ~]# ssh [email protected]		'以root用户登录对方主机'
The authenticity of host '20.0.0.61 (20.0.0.61)' can't be established.
ECDSA key fingerprint is SHA256:Eer6tAEbaZylH0v8F1nr+ShthK1rjZl3eRi7UTw4RX4.
ECDSA key fingerprint is MD5:de:d7:cf:23:bd:8d:a1:02:ff:23:a2:4b:94:fe:e7:02.
Are you sure you want to continue connecting (yes/no)? yes	'输入yes'
Warning: Permanently added '20.0.0.61' (ECDSA) to the list of known hosts.
[email protected]'s password: 	输入对方密码'
Last login: Thu Jul 21 17:37:59 2020 from 20.0.0.61
[root@test01 ~]# 
    还有会开启 /etc/pam.d/su服务模块的情况,需要注意权限
  • scp命令–远程安全复制
    命令基本格式
    格式一:scp user@host:file 1 file 2(@在前,把对方的文件复过来到file2)
    格式二:scp file 1 user@host:file 2(@在后,把自己的file1复制给对方)

  • sftp命令–安全FTP上下载
    命令基本格式
    sftp user@host
    get:下载
    put:上传

1.3 密钥对验证的SSH体系

1.3.1 构建密钥对验证的SSH体系

整体实现过程

  • 第一步:创建密钥对(由客户端的用户lisi在本地 创建密钥对)

    • 公钥文件:id_rsa
    • 公钥文件:id_rsa.pub
  • 第二步:上传公钥文件 id_rsa.pub

    • 任何方式均可(共享,FTP,Email,SCP,。。。)
  • 第三步:导入公钥信息(导入到服务端用户66的公钥数据库)

    • 将公钥文本添加到目标用户的公钥库

    • 默认公钥库文件:~/.ssh/authorized_keys

  • 第四步:使用密钥对验证方式(以服务端用户66的身份进行登录)

    • 客户端使用秘钥对验证登录

    • 验证用户:服务端用户66

    • 验证密码:客户端的用户55的私钥短语

1、在客户机中创建密钥对

  • ssh-keygen命令
  • 可用的加密算法:RSA、ECDSA或DSA
[caiwu@test02 ~]$ ls -a   '查看caiwu底下所有文件'
.  ..  .bash_logout  .bash_profile  .bashrc  .cache  .config  .mozilla
[caiwu@test02 ~]$ ssh-keygen -t ecdsa   '创建密钥对'
Generating public/private ecdsa key pair.
Enter file in which to save the key (/home/caiwu/.ssh/id_ecdsa): 
Created directory '/home/caiwu/.ssh'.
Enter passphrase (empty for no passphrase):   '输入密钥短语'
Enter same passphrase again: 
Your identification has been saved in /home/caiwu/.ssh/id_ecdsa.    '私钥文件位置'
Your public key has been saved in /home/caiwu/.ssh/id_ecdsa.pub.   '公钥文件位置'
The key fingerprint is:
SHA256:fyCHp+4dlIOFvC4hyrq7XKeMztzALBrM+qQusVySAsU caiwu@test02
The key's randomart image is:
+---[ECDSA 256]---+
| .               |
|  E    . .       |
| .      o .      |
|.        = .     |
|. . . . S B      |
|B+ o . o B o     |
|=O*. .. o o .    |
|X** o  o . o     |
|%%.+   .o .      |
+----[SHA256]-----+
[caiwu@test02 ~]$ ls -a   '再次查看caiwu底下所有文件'
.  ..  .bash_logout  .bash_profile  .bashrc  .cache  .config  .mozilla  .ssh  '有存放密钥对的.ssh文件'
[caiwu@test02 ~]$ cd .ssh/   '进入.ssh文件'
[caiwu@test02 .ssh]$ ls   '查看'
id_ecdsa  id_ecdsa.pub     '有公钥和私钥两个文件'

2、验证密码后,会将公钥自动添加到目标主机wangwu宿主目录下的.ssh_authorized_keys文件结尾

[caiwu@test02 .ssh]$ ssh-copy-id -i id_ecdsa.pub [email protected]   '验证密码后,会将公钥自动添加到目标主机wangwu宿主目录下的.ssh_authorized_keys文件结尾'
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_ecdsa.pub"
The authenticity of host '20.0.0.61 (20.0.0.61)' can't be established.
ECDSA key fingerprint is SHA256:EA2z2a/Ha3Zkas+OoAZUF1lFnaXqcdgM17GtJynxzx8.
ECDSA key fingerprint is MD5:e2:fd:f0:5f:1d:16:f0:36:8e:49:b2:ae:73:e7:6b:26.
Are you sure you want to continue connecting (yes/no)? 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
[email protected]‘s password:    '输入密钥短语'
Permission denied, please try again.
[email protected]'s password: 

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.

3、客户端使用密钥对验证登录

验证用户:服务端的wangwu
验证密码:客户端的用户caiwu的私钥短语

[caiwu@test02 .ssh]$ ssh [email protected]   '远程访问wangwu'
Enter passphrase for key '/home/caiwu/.ssh/id_ecdsa':   '输入密钥短语' 
Last failed login: Sun Jul 12 21:51:18 CST 2020 from 20.0.0.41 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Thu Jul  9 12:27:47 2020 from 20.0.0.41
[wangwu@test01 ~]$ 

1.3.3 免密登录

[caiwu@test02 ~]$ ssh-agent bash  '代理bash终端'
[caiwu@test02 ~]$ ssh-add  '添加免密登录'
Enter passphrase for /home/caiwu/.ssh/id_ecdsa: 
Identity added: /home/caiwu/.ssh/id_ecdsa (/home/caiwu/.ssh/id_ecdsa)
[caiwu@test02 ~]$ ssh [email protected]  '远程登录'
Last login: Sun Jul 12 21:53:28 2020 from 20.0.0.41
[wangwu@test01 ~]$ 

二、TCP Wrappers访问控制

2.1 TCP Wrappers概述

2.1.1 保护原理

在这里插入图片描述

2.1.2 保护机制的实现方式

  • 方式1:通过tcpd主程序对其他服务程序进行包装

  • 方式2:由凄然服务程序调用libwrap.so.*链接库

2.1.3:访问控制策略的配置文件

ldd `which sshd` 查看模块
  • /etc/hosts.allow
  • /etc/hosts.deny

2.2 TCP Wrappers访问策略

2.2.1 设置访问控制策略

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

  • 服务程序列表

    • 多个服务以逗号分隔,ALL表示所有服务
  • 客户机地址列表

    • 多个地址以逗号分隔,ALL表示所有服务

    • 允许使用通配符*和?

    • 网段地址,如192.168.1 或者 192.168.1.0/255.255.255.0

    • 区域地址,如.benet.com

2.2.2 策略的应用程序

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

2.2.3 策略应用实例

  • 仅允许从以下地址访问sshd服务

    • 主机192.168.100.100

    • 网段192.168.200.0/24

  • 禁止其他所有地址访问受保护的服务

[root@55 ~]# vim /etc/hosts.allow
sshd:20.0.0.41,192.168.100.*
[root@55 ~]# vim /etc/hosts.deny
sshd:ALL
    
    '优先读取allow,然后再读取deny'
    '如果做黑名单,name白名单就不用写'

猜你喜欢

转载自blog.csdn.net/ycycyyc_/article/details/107221764
今日推荐