您尚未登录。

#1 2022-06-22 16:00:59

qwe221133
会员
注册时间: 2022-06-22
帖子: 8

centos7 中更改了host.allow & hosy.deny却还是无法限制ip访问

centos7上设置ssh连线时,希望仅有特定ip可以shh连线 (仅学校ip可访问),
舉例 : 在host.allow 中设定了 sshd:192.168.*.*
         在hosy.deny 中设定了 sshd:ALL
但重新启动shh之后却不生效,我已经确认ssh是应用了libwrapped库文件,请问有没有人知道怎么解决这个问题?

离线

#2 2022-06-22 17:34:14

Watermelon.Rei
会员
注册时间: 2021-12-29
帖子: 114

Re: centos7 中更改了host.allow & hosy.deny却还是无法限制ip访问

使用了什么ssh server?
openssh 的话去改/etc/ssh/sshd_config
里面的ListenAddress

离线

#3 2022-06-22 22:28:47

qwe221133
会员
注册时间: 2022-06-22
帖子: 8

Re: centos7 中更改了host.allow & hosy.deny却还是无法限制ip访问

谢谢Watermelon.Rei的建议!!!确实是使用openssh
我更改/etc/ssh/sshd_config中的ListenAddress 为:
ListenAddress 140.114.*.* (使用*號是希望仅實驗室中多個ip可连线)
但用VPN转换到学校ip后也无法以ssh连线
请问是语法错误吗??

离线

#4 2022-06-22 23:47:37

silkriver
会员
注册时间: 2020-07-24
帖子: 147

Re: centos7 中更改了host.allow & hosy.deny却还是无法限制ip访问

这样写可以的

Match Address 140.114.*.*
    PubkeyAuthentication yes

离线

#5 2022-06-23 09:31:54

Watermelon.Rei
会员
注册时间: 2021-12-29
帖子: 114

Re: centos7 中更改了host.allow & hosy.deny却还是无法限制ip访问

啊对不起我误解了你的意图,ListenAddress是监听的服务端的IP地址,例如你的服务端有两块网卡A,B,网卡A所处的网段为192.168.1.0/24,IP地址为192.168.1.100,网卡B所处的网段为192.168.2.0/24,IP地址为192.168.2.100,这时若只允许A网段的计算机访问,设置ListenAddress 192.168.1.100,则所有和A处于同一网段的计算机均可以访问
若现在有两台计算机α,β,IP地址分别为192.168.1.101和192.168.2.101,单独指定α与β访问服务端则需要设置防火墙或设置Match Address 192.168.1.101,192.168.2.101。需要确保全局密码访问是关闭的,在match块外找到或添加PasswordAuthentication,设置为no即可

对于host.allow的问题,参见https://lwn.net/Articles/615173/,自openssh 6.7后移除了tcpwrappers功能,所以在较新的发行版中这个功能都不能用了

最近编辑记录 Watermelon.Rei (2022-06-23 09:51:03)

离线

#6 2022-06-23 14:02:52

qwe221133
会员
注册时间: 2022-06-22
帖子: 8

Re: centos7 中更改了host.allow & hosy.deny却还是无法限制ip访问

谢谢Watermelon.Rei的建议,我照着新增了
Match Address 140.114.*.*
PubkeyAuthentication yes
这两行指令在/etc/ssh/sshd_config中
但经过测试后还是无法只让指定的ip使用shh
我少做了什么吗?
另外请问PasswordAuthentication no 指令是一定要加吗?
我希望每个使用者都可以有自己的访问密码

离线

#7 2022-06-23 14:47:21

Watermelon.Rei
会员
注册时间: 2021-12-29
帖子: 114

Re: centos7 中更改了host.allow & hosy.deny却还是无法限制ip访问

qwe221133 说:

谢谢Watermelon.Rei的建议,我照着新增了
Match Address 140.114.*.*
PubkeyAuthentication yes
这两行指令在/etc/ssh/sshd_config中
但经过测试后还是无法只让指定的ip使用shh
我少做了什么吗?
另外请问PasswordAuthentication no 指令是一定要加吗?
我希望每个使用者都可以有自己的访问密码

Match中的PubkeyAuthentication是否缩进?
我的理解是你希望ssh通过白名单访问,这样就需要所有在名单外的IP地址无法通过密码访问,因此全局中PasswordAuthentication 设置为 no,在Match匹配中,增加PubkeyAuthentication yes来确保指定IP可以密码登录
附一张我的试验配置文件截图:
a00b93f1ec0c757e.png
配置好文件后别忘了systemctl restart sshd重启ssh服务

非白名单IP地址ssh访问会遇到以下错误:
0cdee0ecc43a3806.png

最近编辑记录 Watermelon.Rei (2022-06-23 14:49:43)

离线

#8 2022-06-24 13:00:26

qwe221133
会员
注册时间: 2022-06-22
帖子: 8

Re: centos7 中更改了host.allow & hosy.deny却还是无法限制ip访问

非常感謝Watermelon.Rei!!!!!問題解決了

离线

#9 2022-07-12 04:36:55

CoelacanthusHex
会员
注册时间: 2021-01-11
帖子: 10
个人网站

Re: centos7 中更改了host.allow & hosy.deny却还是无法限制ip访问

Watermelon.Rei 说:

Match中的PubkeyAuthentication是否缩进?

OpenSSH 配置文件中的缩进是对机器无意义的,只是为了方便人阅读

离线

#10 2022-07-12 09:19:13

Watermelon.Rei
会员
注册时间: 2021-12-29
帖子: 114

Re: centos7 中更改了host.allow & hosy.deny却还是无法限制ip访问

CoelacanthusHex 说:
Watermelon.Rei 说:

Match中的PubkeyAuthentication是否缩进?

OpenSSH 配置文件中的缩进是对机器无意义的,只是为了方便人阅读

https://man.openbsd.org/sshd_config#Match
Match
Introduces a conditional block. If all of the criteria on the Match line are satisfied, the keywords on the following lines override those set in the global section of the config file, until either another Match line or the end of the file. If a keyword appears in multiple Match blocks that are satisfied, only the first instance of the keyword is applied.

确实如此,受教了。条件块的结束判断靠其他Match行和文件尾判断

离线

页脚