基于Linux下限制指定用户或IP地址通过SSH登录(访问控制)

本文涉及的产品
访问控制,不限时长
简介: 基于Linux下限制指定用户或IP地址通过SSH登录(访问控制)

环境介绍:


ssh主机:192.168.2.128


客户端:192.168.2.129


客户端:192.168.2.130


IP限制:


 针对指定的IP地址进行限制SSH登录。


1.修改hosts.allow主机允许配置文件,添加允许地址

[root@localhost ~]# vim /etc/hosts.allow
...
sshd:192.168.2.130:allow       //添加只允许连接的IP地址
sshd:192.168.3.0/24:allow      //允许3.0/24这个网段内的IP连接

2.修改hosts.deny主机拒绝配置文件

[root@localhost ~]# vim /etc/hosts.deny
...
sshd:ALL       //这里的ALL表示除了上面文件中允许的,其他的IP地址都拒绝

同时设置上述两个文件时,hosts.allow文件中规则的优先级更高,参考上述两个文件进行设置时,服务器只允许192.168.2.130这个IP地址以及192.168.3.0/24这个IP地址段通过SSH进行登录,其他的IP都会被拒绝SSH登录。


3.重启ssh服务

[root@localhost ~]# systemctl restart sshd

4.测试ssh连接

[root@test2 ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.2.129  netmask 255.255.255.0  broadcast 192.168.2.255
...
[root@test2 ~]# ssh root@192.168.2.128
ssh_exchange_identification: read: Connection reset by peer
[root@test2 ~]# ssh -v root@192.168.2.128
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug1: Connecting to 192.168.2.128 [192.168.2.128] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4
ssh_exchange_identification: read: Connection reset by peer

用户限制


 限制某个指定用户通过SSH登录。


1.编辑/etc/ssh/sshd_config配置文件


增加类似如下的Deny Users和AllowUsers等选项,拒绝/只允许指定用户通过SSH登录。然后重启SSH服务即可。


 AllowUsers:允许某个用户、某些用户能登录,其它都不能登录


 AllowGroups:允许某个组、某些组能登录,其它都不能登录


 DenyUsers:拒绝某个用户、某些用户登录,其它都能登录


 DenyGroups:拒绝某个组、某些组登录,其它都能登录


如:


AllowUsers lisi test@192.168.2.130  


//允许所有网段的lisi用户和192.168.2.130的test用户通过SSH登录系统,其他的都不允许。


AllowUsers test@192.168.2.*


//允许192.168.2.0网段的test用户通过SSH登录系统。


DenyUsers zhangsan lisi  


//拒绝zhangsan、lisi用户通过SSH登录系统。

[root@localhost ~]# vim /etc/ssh/sshd_config
...
AllowUsers root@192.168.2.129     //只允许192.168.2.129的root用户登录
[root@localhost ~]# systemctl restart sshd

2.测试只允许192.168.2.129的root用户通过ssh连接主机

[root@test2 ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.2.129  netmask 255.255.255.0  broadcast 192.168.2.255
        inet6 fe80::6625:cc22:2268:e1f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:1a:8b:61  txqueuelen 1000  (Ethernet)
        RX packets 5466745  bytes 2275431218 (2.1 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4420539  bytes 1082931575 (1.0 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@test2 ~]# ssh root@192.168.2.128
Last failed login: Thu Jun 18 16:23:30 CST 2020 from gateway on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Thu Jun 18 16:23:21 2020 from 192.168.2.129
   //成功登录
--------------------------------------------------
[root@test3 ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.2.130  netmask 255.255.255.0  broadcast 192.168.2.255
        inet6 fe80::2c27:a02c:731a:2219  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:53:71:a2  txqueuelen 1000  (Ethernet)
        RX packets 140126  bytes 20349622 (19.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 31280  bytes 2739647 (2.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@test3 ~]# ssh root@192.168.2.128
root@192.168.2.128's password:
Permission denied, please try again.
   //登录失败
相关实践学习
消息队列+Serverless+Tablestore:实现高弹性的电商订单系统
基于消息队列以及函数计算,快速部署一个高弹性的商品订单系统,能够应对抢购场景下的高并发情况。
云安全基础课 - 访问控制概述
课程大纲 课程目标和内容介绍视频时长 访问控制概述视频时长 身份标识和认证技术视频时长 授权机制视频时长 访问控制的常见攻击视频时长
相关文章
|
23天前
|
监控 Ubuntu 安全
debian或Ubuntu中开启ssh允许root远程ssh登录的方法
在Debian或Ubuntu系统中启用root用户的SSH远程登录需要编辑SSH配置文件、设置root密码并重启SSH服务。虽然这可以在某些情况下提供便利,但必须注意安全性,通过使用强密码、限制IP访问、使用SSH密钥认证等方法来保护服务器的安全。
301 5
|
1月前
|
网络协议 Ubuntu Linux
解决ssh: connect to host IP port 22: Connection timed out报错(scp传文件指定端口)
解决 `ssh: connect to host IP port 22: Connection timed out` 报错涉及检查 SSH 服务状态、防火墙配置、网络连通性和主机名解析等多个方面。通过逐步排查上述问题,并在 `scp` 命令中正确指定端口,可以有效解决连接超时的问题,确保文件传输的顺利进行。希望本文提供的解决方案能帮助您快速定位并解决该错误。
214 3
|
5月前
|
JavaScript 应用服务中间件 Linux
【应用服务 App Service】解决无法从Azure门户SSH登录问题
【应用服务 App Service】解决无法从Azure门户SSH登录问题
|
1月前
|
运维 网络安全
解决ssh: connect to host IP port 22: Connection timed out报错(scp传文件指定端口)
通过这些步骤和方法,您可以有效解决“ssh: connect to host IP port 22: Connection timed out”问题,并顺利使用 `scp`命令传输文件。
750 7
|
4月前
|
安全 Linux 网络安全
Linux端的ssh如何升级?
Linux端的ssh如何升级?
461 59
|
2月前
|
监控 Ubuntu Linux
使用VSCode通过SSH远程登录阿里云Linux服务器异常崩溃
通过 VSCode 的 Remote - SSH 插件远程连接阿里云 Ubuntu 22 服务器时,会因高 CPU 使用率导致连接断开。经排查发现,VSCode 连接根目录 ".." 时会频繁调用"rg"(ripgrep)进行文件搜索,导致 CPU 负载过高。解决方法是将连接目录改为"root"(或其他具体的路径),避免不必要的文件检索,从而恢复正常连接。
|
3月前
|
Ubuntu Linux
Linux实践|设置静态 IP 地址
Linux实践|设置静态 IP 地址
89 0
Linux实践|设置静态 IP 地址
|
5月前
|
安全 Linux
Linux查看和剔除当前登录用户详细教程
Linux查看和剔除当前登录用户详细教程
162 0
Linux查看和剔除当前登录用户详细教程
|
5月前
|
安全 Shell Linux
如何禁止某个用户使用ssh登录
本文介绍了五种禁止用户通过SSH登录的方法:1) 修改`/etc/ssh/sshd_config`文件中的`DenyUsers`和`DenyGroups`来阻止特定用户或用户组登录;2) 将用户的默认shell设置为`/usr/sbin/nologin`或`/bin/false`以禁用其SSH访问;3) 利用PAM(可插入认证模块)通过编辑`/etc/security/sshd.conf`来限制登录权限;4) 通过编辑`/etc/hosts.deny`文件拒绝特定用户的SSH访问;5) 锁定或禁用用户账号以阻止所有类型的登录。每种方法都提供了详细的步骤指导。
813 1
|
5月前
|
Linux 网络安全
Linux开启ssh
Linux开启ssh
54 0