当前位置: 移动技术网 > 网络运营>服务器>Linux > Centos 7.4中的远程访问控制的实现方法

Centos 7.4中的远程访问控制的实现方法

2020年03月09日  | 移动技术网网络运营  | 我要评论

一、ssh远程管理

ssh是一种安全通道协议,主要用来实现字符界面的远程登录、远程复制等功能。ssh协议对通信双方的数据传输进行了加密处理,其中包括用户登录时输入的用户口令。与早期的telent、rsh、rcp、等应用相比,ssh协议提供了更好的安全性。

1、配置openssh服务端

在centos 7.4系统中,openssh服务器由openssh、openssh-server等软件包提供(默认已安装),并已将sshd添加为标准的系统服务。执行“systemctl start sshd”命令即可启动sshd服务,包括root在内的大部分用户都可以远程登录系统。sshd服务的配置文件默认位于/etc/ssh/sshd_config目录下,正确调整相关配置项,可以进一步提高sshd远程登录的安全性。

1)服务监听选项

sshd服务使用的默认端口号为22,必要时建议修改此端口号,并指定监听服务的具体ip地址,以提高在网络中的隐蔽性。v2版本要比v1版本的安全性要更好,禁用dns反向解析可以提高服务器的响应速度。

[root@centos01 ~]# vim /etc/ssh/sshd_config  <!--编辑sshd主配置文件-->
17 port 22     <!--监听端口为22-->
19 listenaddress 192.168.100.10  <!--监听地址为192.168.100.10-->
21 protocol 2    <!--使用ssh v2协议-->
118 usedns no  <!--禁用dns反向解析-->
......       <!--此处省略部分内容-->
[root@centos01 ~]# systemctl restart sshd  <!--重启sshd服务-->

2)用户登录控制

sshd服务默认允许root用户登录,但在internet中使用时是非常不安全的。关于sshd服务的用户登录控制,通常应禁止root用户或密码为空的用户登录。另外,可以限制登录验证的时间(默认为2分钟)及最大重试次数,若超过限制后仍未能登录则断开连接。

[root@centos01 ~]# vim /etc/ssh/sshd_config   <!--编辑sshd主配置文件-->
 37 logingracetime 2m    <!--登录验证时间为2分钟-->
 38 permitrootlogin yes   <!--禁止root用户登录-->
 40 maxauthtries 6        <!--最大重试次数为6-->
 67 permitemptypasswords no    <!--禁止空密码用户登录-->
 ......       <!--此处省略部分内容-->
[root@centos01 ~]# systemctl restart sshd      <!--重启sshd服务-->

2、登录验证方式

对于服务器的远程管理,除了用户账户的安全控制以外,登录验证的方式也非常重要。sshd服务支持两种验证方式——密码验证、密钥对验证,可以设置只使用其中一种方式,也可以两种方式都启用。

密码验证:对服务器中本地系统用户的登录名称、密码进行验证。这种方式使用最为简便,但从客户端角度来看,正在连接的服务器有可能被假冒;从服务器角度来看,当遭遇密码穷举第三者时防御能力比较弱。

密钥对验证:要求提供相匹配的密钥信息才能通过验证。通常先在客户端中创建一对密钥文件(公钥、私钥),然后将公钥文件放到服务器中的指定位置。远程登录时,系统将使用公钥,私钥进行加密/解密关联验证,大大增强了远程管理的安全性。该方式不易被假冒,且可以免交互登录,在shell中被广泛使用。

当密码验证,密钥对验证都启用时,服务器将优先使用密钥对验证。对于安全性要求较高的服务器,建议将密码验证方式禁用,只允许启用密钥对验证方式;若没有特殊要求,则两种方式都可以启用。

[root@centos01 ~]# vim /etc/ssh/sshd_config <!--编辑sshd主配置文件-->
 43 pubkeyauthentication yes     <!--启用密钥对验证-->
 47 authorizedkeysfile   .ssh/authorized_keys <!--指定公钥库文件-->
 66 passwordauthentication yes    <!--启用密码验证-->
......       <!--此处省略部分内容-->
[root@centos01 ~]# systemctl restart sshd     <!--重启sshd服务-->

其中,公钥文件用来保存多个客户端上传的公钥文本,以便与客户端本地的私钥文件进行匹配。

二、使用ssh客户端程序

在centos 7.4系统中,openssh客户端由openssh-clients软件包提供(默认已安装),其中包括ssh远程登录命令,以及scp、sftp远程复制和文件传输命令等。

1、命令程序ssh远程登录

通过ssh命令可以远程登录sshd服务,为用户提供一个安全的shell环境,以便对服务器进行管理和维护。使用时应指定登录用户、目标主机地址作为参数。示例如下:

[root@centos02 ~]# ssh root@192.168.100.10
root@192.168.100.10's password: 
last login: mon nov 11 19:02:50 2019 from 192.168.100.254
[root@centos01 ~]# 
[root@centos01 ~]# 
[root@centos01 ~]# ssh root@192.168.100.10
the authenticity of host '192.168.100.10 (192.168.100.10)' can't be established.
ecdsa key fingerprint is sha256:puuet9fu9qbsynb5nc5hbsxzawxxqavbxxmfoknxl4i.
ecdsa key fingerprint is md5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b.
are you sure you want to continue connecting (yes/no)? yes  <!--接受密钥-->
warning: permanently added '192.168.100.10' (ecdsa) to the list of known hosts.
root@192.168.100.10's password:   <!--输入密码-->
last login: mon nov 11 19:03:08 2019 from 192.168.100.20
[root@centos01 ~]# who     <!--确认当前用户-->
root   pts/1    2019-11-11 19:03 (192.168.100.20)
root   pts/2    2019-11-11 19:04 (192.168.100.10)

如果sshd服务器使用了非默认的端口(如2222),则在登录时必须通过“-p”选项指定端口号。示例如下:

[root@centos01 ~]# vim /etc/ssh/sshd_config<!--修改ssh主配置文件-->
port 2222     <!--修改监听端口号为2222-->
[root@centos01 ~]# systemctl restart sshd  <!--重启sshd服务-->
[root@centos02 ~]# ssh -p 2222 root@192.168.100.10   <!--客户端登录ssh-->
root@192.168.100.10's password:     <!--输入密码-->
last login: mon nov 11 19:20:28 2019 from 192.168.100.10
[root@centos01 ~]#      <!--成功登录-->

2、scp远程复制

通过scp命令可以利用ssh安全连接与远程主机相互复制文件,使用scp命令时,除了必须指定复制源、目标之外,还应指定目标主机地址、登录用户,执行后根据提示输入验证口令即可。示例如下:

[root@centos02 ~]# scp
root@192.168.100.10:/etc/ssh/sshd_config ./ 
     <!--将远程主机数据复制到本地数据,保存在当前位置-->
root@192.168.100.10's password:   <!--输入密码-->
sshd_config          100% 3910   3.6mb/s  00:00  
[root@centos02 ~]# scp -r ./sshd_config
root@192.168.100.10:/opt   
     <!--将本地数据上传到远程主机目录的opt中-->
root@192.168.100.10's password:   <!--输入密码-->
sshd_config          100% 3910   1.2mb/s  00:00  

3、sftp安装ftp

通过sftp命令可以利用ssh安全连接与远程主机上传、下载文件,采用了与ftp类似的登录过程和交互环境,便于目录资源管理。示例如下:

[root@centos01 ~]# cd /opt/    <!--进入opt目录-->
[root@centos01 opt]# sftp root@192.168.100.20  <!--登录sftp-->
root@192.168.100.20's password:   <!--输入密码-->
connected to 192.168.100.20.
sftp> pwd    <!--查看客户端登录sftp的位置默认在宿主目录-->
remote working directory: /root
sftp> put sshd_config    <!--上传数据到远程主机-->
uploading sshd_config to /root/sshd_config
sshd_config          100% 3910   6.4mb/s  00:00  
sftp> get sshd_config     <!--下载数据到本地-->
fetching /root/sshd_config to sshd_config
/root/sshd_config       100% 3910   3.6mb/s  00:00  
sftp> exit         <!--退出登录-->

三、构建密钥对验证的ssh体系

密钥对验证方式可以远程登录提供更好的安全性。在linux服务器、客户端中构建密钥对验证ssh体系的基本过程。如下图所示,整个过程包括四步,首先要在ssh客户端以zhangsan用户身份创建密钥对,并且要将创建的公钥文件上传至ssh服务器端,然后要将公钥信息导入服务器端的目标用户lisi的公钥数据库,最后以服务器端用户lisi的身份登录验证。

1、在客户端创建密钥对

在客户端中,通过ssh-keygen工具为当前用户创建密钥对文件。可用的加密算法为ecdsa或dsa(ssh-keygen命令的“-t”选项用于指定算法类型)。示例如下:

[root@centos02 ~]# ssh-keygen -t dsa   <!--创建密钥对-->
generating public/private dsa key pair.
enter file in which to save the key (/root/.ssh/id_dsa):  <!--指定私钥位置-->
created directory '/root/.ssh'.
enter passphrase (empty for no passphrase): <!--设置私钥短语-->
enter same passphrase again:    <!--确认所设置的短语-->
your identification has been saved in /root/.ssh/id_dsa.
your public key has been saved in /root/.ssh/id_dsa.pub.
the key fingerprint is:
sha256:zv0edqiuofwsovn2dkij08y9wz0f1+iyhy7lfnkkzkk root@centos02
the key's randomart image is:
+---[dsa 1024]----+
|         |
|         |
|         |
|   .      |
| o . o s.+ .  |
| * *.+.=.+.=   |
|o e.*o+==.+ o  |
| =o..*oo++ +   |
| ++oo+*+o. .  |
+----[sha256]-----+
[root@centos02 ~]# ls -lh ~/.ssh/id_dsa* <!--确认生成的密钥文件-->
-rw------- 1 root root 668 11月 12 16:11 /root/.ssh/id_dsa
-rw-r--r-- 1 root root 603 11月 12 16:11 /root/.ssh/id_dsa.pub

新生成的密钥对文件中,id_das是私钥文件,权限默认为600,对于私钥文件必须妥善保管,不能泄露给他人;id_dsa.pub是公钥文件,用来提供给ssh服务器。

2、将公钥文件上传至服务器

将上一步生成的公钥文件上传至服务器,并部署到服务器端用户的公钥数据库中。上传公钥文件时可以选择scp、ftp、http甚至发送e-mail等任何方式。

root@centos02 ~]# ssh-copy-id -i ./.ssh/id_dsa.pub 
root@192.168.100.10 <!--将公钥文件上传至服务器并导入公钥文本-->
/usr/bin/ssh-copy-id: info: source of key(s) to be installed: "./.ssh/id_dsa.pub"
the authenticity of host '192.168.100.10 (192.168.100.10)' can't be established.
ecdsa key fingerprint is sha256:puuet9fu9qbsynb5nc5hbsxzawxxqavbxxmfoknxl4i.
ecdsa key fingerprint is md5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b.
are you sure you want to continue connecting (yes/no)? yes  <!--输入yes-->
/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
root@192.168.100.10's password:   <!--输入密码-->

number of key(s) added: 1

now try logging into the machine, with:  "ssh 'root@192.168.100.10'"
and check to make sure that only the key(s) you wanted were added.

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

当私钥文件(客户端)、公钥文件(服务器)均部署到位以后,就可以在客户端中进行测试了。首先确认客户端中当前的用户为root,然后通过ssh命令以服务器端用户root的身份进行远程登录。如果密钥对验证方式配置成功,则在客户端将会要求输入私钥短语,以便调用私钥文件进行匹配(若未设置私钥短语,则直接登入目标服务器)。

[root@centos02 ~]# ssh root@192.168.100.10   <!--登录ssh服务器-->
last login: tue nov 12 16:03:56 2019 from 192.168.100.254
[root@centos01 ~]# who  <!--登录成功服务器,查看都有哪些用户-->
root   pts/0    2019-11-12 17:35 (192.168.100.20)
root   pts/2    2019-11-12 16:03 (192.168.100.254)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网