基于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.
   //登录失败
相关文章
|
4月前
|
Linux 网络安全 Docker
盘古栈云,创建带ssh服务的linux容器
创建带ssh服务的linux容器
366 146
|
8月前
|
应用服务中间件 网络安全 数据安全/隐私保护
网关服务器配置指南:实现自动DHCP地址分配、HTTP服务和SSH无密码登录。
哇哈哈,道具都准备好了,咱们的魔术秀就要开始了。现在,你的网关服务器已经魔法满满,自动分配IP,提供网页服务,SSH登录如入无人之境。而整个世界,只会知道效果,不会知道是你在幕后操控一切。这就是真正的数字世界魔法师,随手拈来,手到擒来。
427 14
|
7月前
|
监控 Linux 网络安全
FinalShell SSH工具下载,服务器管理,远程桌面加速软件,支持Windows,macOS,Linux
FinalShell是一款国人开发的多平台SSH客户端工具,支持Windows、Mac OS X和Linux系统。它提供一体化服务器管理功能,支持shell和sftp同屏显示,命令自动提示,操作便捷。软件还具备加速功能,提升访问服务器速度,适合普通用户和专业人士使用。
2525 0
|
9月前
|
Ubuntu Linux 网络安全
在Linux云服务器上限制特定IP进行SSH远程连接的设置
温馨提示,修改iptables规则时要格外小心,否则可能导致无法远程访问你的服务器。最好在掌握足够技术知识和理解清楚操作含义之后再进行。另外,在已经配置了防火墙的情况下,例如ufw(Ubuntu Firewall)或firewalld,需要按照相应的防火墙的规则来设置。
493 24
|
10月前
|
安全 Linux
阿里云linux服务器使用脚本通过安全组屏蔽异常海外访问ip
公网网站可能会遭受黑客攻击导致访问异常,使用此脚本可以屏蔽掉异常IP 恢复访问。也可自行设置定时任务定期检测屏蔽。
749 28
|
8月前
|
安全 网络协议 Linux
Linux查 ssh端口号和服务状态
本指南详细介绍如何检查SSH服务的运行状态,包括通过进程命令验证服务启动、查看监听端口、检测系统服务状态以及防火墙配置。同时提供安全建议,如修改默认端口、禁用密码登录和定期更新系统,确保SSH服务稳定与安全。适用于不同Linux发行版(Systemd/SysVinit),帮助用户全面排查和优化SSH配置。
|
10月前
|
安全 Linux 网络安全
在Linux(CentOS和AWS)上安装更新的git2的方法并配置github-ssh
经过以上这些步骤,你现在就能在GitHub上顺利往返,如同海洋中的航海者自由驰骋。欢迎你加入码农的世界,享受这编程的乐趣吧!
446 10
|
10月前
|
人工智能 Kubernetes Ubuntu
linux配置IP
linux配置IP
2025 1
|
11月前
|
安全 网络安全 数据安全/隐私保护
Debian 12系统中允许Root远程SSH登录解决方法!
在 Debian 12 系统中开启 SSH 远程 Root 登录需修改 SSH 配置文件 (`sshd_config`),将 `PermitRootLogin` 设置为 `yes` 并确保密码认证启用。完成后重启 SSH 服务并验证连接。若防火墙启用,需放行端口 22。注意,直接开放 Root 登录可能带来安全风险,建议使用普通用户登录后切换至 Root。
1515 1
|
12月前
|
Linux Shell
问题记录:解决Linux登录故障,/etc/passwd配置受损该怎么操作
修复/etc/passwd文件是解决Linux登录故障的重要步骤。通过进入单用户模式、挂载文件系统、恢复或手动修复/etc/passwd文件,可以有效解决该问题。保持定期备份系统配置文件是预防此类问题的最佳实践。
381 13