linux系统中配置sshd服务(远程控制服务)

        SSH(Secure Shell)是一种能够以安全的方式提供远程登录的协议,也是目前远程管理Linux的首先方式。在此之前,一般使用FTP或Telnet来进行远程登录。但是因为它们以明文的形式在网络中传输账户密码和数据信息,因此很不安全,很容易受到黑客发起的中间人的攻击,者轻则篡改传输的数据信息,重则直接抓取服务器的账户密码。

       sshd是基于SSH协议开发的一款远程管理服务程序,不仅使用起来方便快捷,而且能够提供两种安全验证的方法:

       基于口令的验证---用账户和密码来验证登录;

       基于密钥的验证---需要在本地生成密钥对,然后把密钥对中的公钥上传至服务器,并与服务器中的公钥进行比较;该方式相较来说更安全。

       sshd服务的配置信息保存在/etc/ssh/sshd_config文件中。运维人员一般会把保存着最主要配置信息的文件称为主配置文件,而配置文件中有许多以#开头的注释行,要想让这些配置参数生效,需要在修改参数后再去掉前面的#号。

 RHEL7中,已经默认安装并启用了sshd服务程序。

使用两台虚拟机进行测试,两台虚拟机模式均为仅主机模式,手动配置IP地址、子网、网关等

1、查看虚拟机基本信息

[root@host1 network-scripts]# cat ifcfg-eno16777728  ## 虚拟机1 网卡配置
TYPE=Ethernet
BOOTPROTO=static
NAME=eno16777728
ONBOOT=yes
IPADDR=192.168.10.10
NETMASK=255.255.255.0
GATEWAY=192.168.10.1
DNS1=192.168.10.1
[root@host1 network-scripts]# ifconfig | head -n 3  ## IP
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fee4:f7b9 prefixlen 64 scopeid 0x20<link>

[root@host2 network-scripts]# cat ifcfg-eno16777728   ## 虚拟机2网卡配置
TYPE=Ethernet
BOOTPROTO=static
NAME=eno16777728
ONBOOT=yes
IPADDR=192.168.10.20
NETMASK=255.255.255.0
GATEWAY=192.168.10.1
DNS1=192.168.10.1
[root@host2 network-scripts]# ifconfig | head -n 3  ## IP
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.20 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:feaa:2b29 prefixlen 64 scopeid 0x20<link>

2、测试使用ssh命令在虚拟机1中远程控制虚拟机2:

[root@host1 network-scripts]# ifconfig | head -n 3  ## 当前主机IP
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.10  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::20c:29ff:fee4:f7b9  prefixlen 64  scopeid 0x20<link>
[root@host1 network-scripts]# ping -c 3 192.168.10.20  ##  测试和虚拟机2的联通性
PING 192.168.10.20 (192.168.10.20) 56(84) bytes of data.
64 bytes from 192.168.10.20: icmp_seq=1 ttl=64 time=0.177 ms
64 bytes from 192.168.10.20: icmp_seq=2 ttl=64 time=0.241 ms
64 bytes from 192.168.10.20: icmp_seq=3 ttl=64 time=0.221 ms

--- 192.168.10.20 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 0.177/0.213/0.241/0.026 ms
[root@host1 network-scripts]# ssh 192.168.10.20  ## 远程控制
root@192.168.10.20's password:   ## 此处输入虚拟机2的root密码
Last login: Tue Nov  3 20:25:41 2020
[root@host2 ~]# ifconfig | head -n 3  ## 查看当前IP,已经变为虚拟机2的IP
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.20  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::20c:29ff:feaa:2b29  prefixlen 64  scopeid 0x20<link>
[root@host2 ~]# exit  ## 退出
logout
Connection to 192.168.10.20 closed.
[root@host1 network-scripts]# ifconfig | head -n 3  ## 回到虚拟机1的终端
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fee4:f7b9 prefixlen 64 scopeid 0x20<link>

3、通过调整虚拟机2的配置文件,将虚拟机2设置为拒绝root远程登录

[root@host2 network-scripts]# vim /etc/ssh/sshd_config 

将下图箭头所指处yes改为no(第48行),去掉行首的#号,保存退出:

 

4、在虚拟机1中测试:

[root@host1 network-scripts]# ssh 192.168.10.20  ## 仍然可以正常远程控制
root@192.168.10.20's password: 
Last login: Tue Nov  3 20:34:34 2020 from 192.168.10.10
[root@host2 ~]# exit
logout
Connection to 192.168.10.20 closed.

5、一般的服务程序并不会在修改配置文件之后立即生效,需要手动重启相应的服务程序。

[root@host2 network-scripts]# systemctl restart sshd
[root@host2 network-scripts]# systemctl enable sshd

6、再次在虚拟机1中测试

[root@host1 network-scripts]# ssh 192.168.10.20  ## 测试,已经拒绝登录
root@192.168.10.20's password: 
Permission denied, please try again.
root@192.168.10.20's password: 
原文地址:https://www.cnblogs.com/liujiaxin2018/p/13922464.html