SSH的登录限制
1. ip限制
/etc/hosts.deny
/etc/hosts.allow
1. ip限制
/etc/hosts.deny
/etc/hosts.allow
比如只限制3个外网,1个内网IP连接
解决办法:
直接在/etc/hosts.deny里面加一行
sshd: ALL EXCEPT xxx.xxx.xxx.xxx(允许的ip或网段)
sshd: ALL EXCEPT xxx.xxx.xxx.xxx/255.255.255.0(允许的网段) 就可以了.
要注意网段的书写格式
sshd: ALL EXCEPT xxx.xxx.xxx.xxx(允许的ip或网段)
sshd: ALL EXCEPT xxx.xxx.xxx.xxx/255.255.255.0(允许的网段) 就可以了.
要注意网段的书写格式
上面是TCP Wrappers的过滤机制,它由tcpd程序控制,有点类似TCP包的检验程序,这个程序的主要参数文件在/etc/hosts.allow及 /etc/hosts/hosts.deny两个文件中,必须要某个软件支持tcpd(TCP Wrappers)的功能才能接受TCP Wrappers的校验。
数据包的校验顺序首先是/etc/hosts.allow,然后才是/etc/hosts/hosts.deny。所以我们通常的设置方法是:在 /etc/hosts/hosts.allow里针对某些服务与数据包来源开启它们的使用权限;在/etc/hosts/hosts.deny里将该服务的其它来源过滤掉。
注意:如果您在/etc/hosts/hosts.allow里没有设置,那么TCP Wrappers默认的动作是allow(通过校验并进入主机)。
简单的语法如下:
<服务名称>:
特别注意:其中“服务名称”其实就是程序文件名,比如telnet程序的服务名称是“in.etlnetd”、ssh的是“sshd”;network可以使用192.168.0.0/255.255.255.0,但不可以使用192.168.0.0/24。
特别注意:其中“服务名称”其实就是程序文件名,比如telnet程序的服务名称是“in.etlnetd”、ssh的是“sshd”;network可以使用192.168.0.0/255.255.255.0,但不可以使用192.168.0.0/24。
/etc/hosts.allow应该这样写:
sshd: 192.168.0.0/255.255.255.0,202.101.73.0/255.255.255.0,202.101.74.0/255.255.255.0,202.101.75.0/255.255.255.0
在/etc/hosts.deny里写上:
sshd: ALL
应用时,换成实际IP即可。
sshd: 192.168.0.0/255.255.255.0,202.101.73.0/255.255.255.0,202.101.74.0/255.255.255.0,202.101.75.0/255.255.255.0
在/etc/hosts.deny里写上:
sshd: ALL
应用时,换成实际IP即可。
数据包进入主机的第一道关卡应该是iptables,第二道才是TCP Wrappers,如果已经越过iptables,说明已登入你的主机了,下一步只是通过TCP Wrappers来进行TCP包校验,那么如果这个支持TCP Wrappers功能的程序有漏洞,结果是什么不用我说了吧。所以而是先从iptables进行限制,把TCP Wrappers作为个补充应该更安全些。
2、限制用户登录
vi /etc/sshd_config
在最后一行加入一行,me 为指定的用户
AllowUsers me
重启SSH
在最后一行加入一行,me 为指定的用户
AllowUsers me
重启SSH
如果是多个用户的话:AllowUsers A B C
注意中间是空格,而不是分号
3、指定尝试密码次数
vi /etc/sshd_config
修必
MaxAuthTries 3
将默认的值改掉即可 Fedora 默认可以允许做3次尝试
重启SSH
在 SSH 的配置文件 /etc/ssh/sshd_config,有不同的方法去限制用户的使用。
AllowUsers 选项能指定那些用户能使用 SSH。你可以在这个选项以后加上用户列表,用空格来分开每个用户。
同时,如果你要指定那些用户不能使用 SSH,你要用 DenyUsers 选项。这两个选项都能在用户名中用通配符如'*'或'?'。但是不可以用户的数字ID。默认是容许所有用户使用 SSH。
如果要限制组,你可以用 AllowGroups 和 DenyGroups。这两个选项可以容许或不容许主组或補充组跟限制组相配的用户。
以下是一个 /etc/ssh/sshd_config 文件的例子,限制 haller 用户使用 SSH :
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options change a
# default value.
#Port 22
#Protocol 2,1
#ListenAddress 0.0.0.0
#ListenAddress ::
# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 3600
#ServerKeyBits 768
# Logging
#obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO
# Authentication:
#LoginGraceTime 120
#PermitRootLogin yes
#StrictModes yes
#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile .ssh/authorized_keys
# rhosts authentication should not be used
#RhostsAuthentication no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no
# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#AFSTokenPassing no
# Kerberos TGT Passing only works with the AFS kaserver
#KerberosTgtPassing no
# Set this to 'yes' to enable PAM keyboard-interactive authentication
# Warning: enabling this may bypass the setting of 'PasswordAuthentication'
#PAMAuthenticationViaKbdInt no
#X11Forwarding no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PrintMotd yes
#PrintLastLog yes
#KeepAlive yes
#UseLogin no
UsePrivilegeSeparation no
#PermitUserEnvironment no
Compression no
#MaxStartups 10
# no default banner path
#Banner /some/path
#VerifyReverseMapping no
#ShowPatchLevel no
# override default of no subsystems
Subsystem sftp /usr/libexec/openssh/sftp-server
DenyUsers haller
修改 /etc/ssh/sshd_config 文件后,重启 sshd 服务:
service sshd restart
另一个限制 SSH 用户的方法是使用可插入的认证模块服务(PAM)。你可以参考其他知识库的文章关於怎样配置 SSH 使用 PAM 的用户列表。
--------------------------------------------------------------------
1,只允许某个IP登录,拒绝其他所有IP
在 /etc/hosts.allow 写:
sshd: 1.2.3.4
在 /etc/hosts.deny 写:
sshd: ALL
用 iptables 也行:
iptables -I INPUT -p tcp --dport 22 -j DROP
iptables -I INPUT -p tcp --dport 22 -s 1.2.3.4 -j ACCEPT
2,禁止某个用户通过ssh登录
在/etc/ssh/sshd_conf添加
AllowUsers 用户名
或者
AllowGroups 组名
或者
DenyUsers 用户名
3,设定登录黑名单
vi /etc/pam.d/sshd
增加
auth required /lib/security/pam_listfile.so item=user sense=deny file=/etc/sshd_user_deny_list onerr=succeed
所有/etc/sshd_user_deny_list里面的用户被拒绝ssh登录
AllowUsers 选项能指定那些用户能使用 SSH。你可以在这个选项以后加上用户列表,用空格来分开每个用户。
同时,如果你要指定那些用户不能使用 SSH,你要用 DenyUsers 选项。这两个选项都能在用户名中用通配符如'*'或'?'。但是不可以用户的数字ID。默认是容许所有用户使用 SSH。
如果要限制组,你可以用 AllowGroups 和 DenyGroups。这两个选项可以容许或不容许主组或補充组跟限制组相配的用户。
以下是一个 /etc/ssh/sshd_config 文件的例子,限制 haller 用户使用 SSH :
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options change a
# default value.
#Port 22
#Protocol 2,1
#ListenAddress 0.0.0.0
#ListenAddress ::
# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 3600
#ServerKeyBits 768
# Logging
#obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO
# Authentication:
#LoginGraceTime 120
#PermitRootLogin yes
#StrictModes yes
#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile .ssh/authorized_keys
# rhosts authentication should not be used
#RhostsAuthentication no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no
# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#AFSTokenPassing no
# Kerberos TGT Passing only works with the AFS kaserver
#KerberosTgtPassing no
# Set this to 'yes' to enable PAM keyboard-interactive authentication
# Warning: enabling this may bypass the setting of 'PasswordAuthentication'
#PAMAuthenticationViaKbdInt no
#X11Forwarding no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PrintMotd yes
#PrintLastLog yes
#KeepAlive yes
#UseLogin no
UsePrivilegeSeparation no
#PermitUserEnvironment no
Compression no
#MaxStartups 10
# no default banner path
#Banner /some/path
#VerifyReverseMapping no
#ShowPatchLevel no
# override default of no subsystems
Subsystem sftp /usr/libexec/openssh/sftp-server
DenyUsers haller
修改 /etc/ssh/sshd_config 文件后,重启 sshd 服务:
service sshd restart
另一个限制 SSH 用户的方法是使用可插入的认证模块服务(PAM)。你可以参考其他知识库的文章关於怎样配置 SSH 使用 PAM 的用户列表。
--------------------------------------------------------------------
1,只允许某个IP登录,拒绝其他所有IP
在 /etc/hosts.allow 写:
sshd: 1.2.3.4
在 /etc/hosts.deny 写:
sshd: ALL
用 iptables 也行:
iptables -I INPUT -p tcp --dport 22 -j DROP
iptables -I INPUT -p tcp --dport 22 -s 1.2.3.4 -j ACCEPT
2,禁止某个用户通过ssh登录
在/etc/ssh/sshd_conf添加
AllowUsers 用户名
或者
AllowGroups 组名
或者
DenyUsers 用户名
3,设定登录黑名单
vi /etc/pam.d/sshd
增加
auth required /lib/security/pam_listfile.so item=user sense=deny file=/etc/sshd_user_deny_list onerr=succeed
所有/etc/sshd_user_deny_list里面的用户被拒绝ssh登录