基于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:实现高弹性的电商订单系统
基于消息队列以及函数计算,快速部署一个高弹性的商品订单系统,能够应对抢购场景下的高并发情况。
云安全基础课 - 访问控制概述
课程大纲 课程目标和内容介绍视频时长 访问控制概述视频时长 身份标识和认证技术视频时长 授权机制视频时长 访问控制的常见攻击视频时长
相关文章
|
1月前
|
安全 Linux Shell
Linux中SSH命令介绍
Linux中SSH命令介绍
34 2
|
4天前
|
安全 Linux 网络安全
|
5天前
|
Shell 网络安全 数据安全/隐私保护
MacOS Sonoma14.2.1系统SSH免密登录
【7月更文挑战第9天】在MacOS Sonoma 14.2.1中设置SSH免密登录,包括:1) 使用`ssh-keygen`生成RSA密钥对;2) 使用`ssh-copy-id`将公钥传到远程主机;3) 用`ssh-add --apple-use-keychain`添加私钥到ssh-agent,并为重启后自动添加配置自动化脚本;4) 可选地,编辑`~/.ssh/config`设置别名简化登录。确保远程主机的`.ssh/authorized_keys`文件权限为600。
|
22天前
|
SQL 自然语言处理 网络协议
【Linux开发实战指南】基于TCP、进程数据结构与SQL数据库:构建在线云词典系统(含注册、登录、查询、历史记录管理功能及源码分享)
TCP(Transmission Control Protocol)连接是互联网上最常用的一种面向连接、可靠的、基于字节流的传输层通信协议。建立TCP连接需要经过著名的“三次握手”过程: 1. SYN(同步序列编号):客户端发送一个SYN包给服务器,并进入SYN_SEND状态,等待服务器确认。 2. SYN-ACK:服务器收到SYN包后,回应一个SYN-ACK(SYN+ACKnowledgment)包,告诉客户端其接收到了请求,并同意建立连接,此时服务器进入SYN_RECV状态。 3. ACK(确认字符):客户端收到服务器的SYN-ACK包后,发送一个ACK包给服务器,确认收到了服务器的确
147 1
|
28天前
|
网络协议 Linux 网络安全
蓝易云 - centos用ssh登录连接缓慢处理
请根据自己的实际情况尝试以上方法,找出导致SSH登录缓慢的原因,并进行相应的处理。
16 1
|
22天前
|
网络安全 数据安全/隐私保护
服务器密码登录出现了:SSH connection failed: connect ECONNREFUSEDxxxxxxxx:22 * Xshell提示 SSH connection fa
服务器密码登录出现了:SSH connection failed: connect ECONNREFUSEDxxxxxxxx:22 * Xshell提示 SSH connection fa
|
22天前
|
网络协议 Linux 开发工具
配置Linux固定IP地址,为什么要固定IP,因为他是通DHCP服务获取的,DHCP服务每次重启都会重新获取一次ip,VMware编辑中有一个虚拟网络编辑器
配置Linux固定IP地址,为什么要固定IP,因为他是通DHCP服务获取的,DHCP服务每次重启都会重新获取一次ip,VMware编辑中有一个虚拟网络编辑器
|
24天前
|
监控 算法 Linux
Linux下工具tc详细讲解及限制IP和端口实例
TC (Traffic Control) 是Linux内核中提供的一个用于控制和管理网络流量的强大工具,它允许用户实现QoS(Quality of Service)策略,包括带宽限制、优先级控制、延迟保证等。TC基于内核的队列 discipline (qdisc) 和流量类别(class) 体系结构,允许对进入或离开网络接口的数据流进行复杂的整形和过滤。
|
24天前
|
安全 Linux 网络安全
ssh中的密码登录和密钥登录
ssh中的密码登录和密钥登录
|
24天前
|
域名解析 网络协议 Linux
linux网络-- 手动配置ip地址
linux网络-- 手动配置ip地址