ssh白名单_SSH密码登陆IP白名单
在很多場景下,我們需要設置SSH密碼登陸IP白名單,只允許特定的IP地址使用密碼登陸。
首先,修改/etc/ssh/sshd_config配置文件,通過修改PasswordAuthentication全局關閉密碼登錄:
PasswordAuthentication no
1
PasswordAuthenticationno
然后,在/etc/ssh/sshd_config配置文件的末尾添加Match:
sshd_config中的Match
Match的語法是:
Match condition
Override config option 1
Override config option 2
1
2
3
Matchcondition
Overrideconfigoption1
Overrideconfigoption2
其中的條件包括:
User – Specifies the user to match. For example, if user is root allow login with ssh-keys but disallow everyone else.
Group – Specifies the group to match. For example, If user in group admin, allow login but disallow everyone else.
Host – Specifies the host to match
Address – Specifies the IP address or IP/subnet to match in CIDR format.
因此我們可以在sshd_config的最后增加:
Match Address XXX.XXX.XXX.XXX
PasswordAuthentication yes
1
2
MatchAddressXXX.XXX.XXX.XXX
PasswordAuthenticationyes
其中的XXX.XXX.XXX.XXX就是允許使用密碼登陸的IP地址,即所謂的白名單。其含義為,當登錄IP匹配XXX.XXX.XXX.XXX的形式時,允許采用密碼登錄。這里可以添加多個使用逗號分割的IP地址,也可以使用通配符、CIDR。
對于通配符而言:
* – It matches matches zero or more characters.
? – It matches exactly one character.
! – Patterns within pattern-lists may be negated with !.
例如:
## Match 192.168.1.1 to 192.168.1.9 ##
Match Address 192.168.1.?
PermitRootLogin yes
## Match 192.168.1.{2,3....} ##
Match Address 192.168.2.*
X11Forwarding no
## Allow any host in the ".home.lan" set of domains ##
Match Host *.home.lan
X11Forwarding yes
## Allow everyone except foo user ##
Match User *,!foo
X11Forwarding yes
PermitTunnel yes
PermitTTY no
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
## Match 192.168.1.1 to 192.168.1.9 ##
MatchAddress192.168.1.?
PermitRootLoginyes
## Match 192.168.1.{2,3....}??##
MatchAddress192.168.2.*
X11Forwardingno
## Allow any host in the ".home.lan" set of domains ##
MatchHost *.home.lan
X11Forwardingyes
## Allow everyone except foo user ##
MatchUser *,!foo
X11Forwardingyes
PermitTunnelyes
PermitTTYno
最后,重啟sshd服務:
systemctl restart sshd
1
systemctlrestartsshd
我們就會發現之前能夠使用密碼登陸的機器已經不能登陸了:
SSH被限制為僅有指定IP能夠使用密碼登陸
參考資料:
1、https://www.cyberciti.biz/faq/match-address-sshd_config-allow-root-loginfrom-one_ip_address-on-linux-unix/
總結
以上是生活随笔為你收集整理的ssh白名单_SSH密码登陆IP白名单的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 配置MySQL单个用户多个IP段白名单
- 下一篇: java ip 白名单_Java代码中对