在sshd_config ,默认情况下,允许所有组和用户login。 allow / deny指令按以下顺序处理:DenyUsers,AllowUsers,DenyGroups,最后AllowGroups。 (在手册页中find)。
所以,我必须以“黑名单”的方式来configuration服务器(全部都是允许的,然后我明确地定义哪些访问可以被拒绝)。
有没有办法做相反的事情? 我想如果用户不能连接到SSH,SFTP连接将被拒绝,除非他符合规则。
我把默认的shell设置为/bin/false (所以经典的ssh连接被禁用)。
locking用户( usermod -L usersftp1 )显然阻止用户同时使用sftp和ssh(这对我来说很好)。
我尝试使用DenyGroups !sftp指令来禁止除sftp以外的用户匹配,但是usersftp1(不属于组sftp(主要或补充)但不起作用。
我configuration了我的服务器来pipe理一些sftp访问(从Subsystem sftp internal-sftp )
在/ etc / passwd中,用户线usersftp1:x:5001:5001::/home/sftp/usersftp1:/bin/false
SSH连接不起作用(这是我想要的),但我可以通过SFTP连接(旁边的事实,我不允许该用户呢)。
如果用户具有/bin/false shell,我希望默认行为是“连接不允许”。
编辑:更多的configuration细节:
file /etc/passwd: usersftp1:x:50001:5001::/home/sftp/usersftp1:/bin/false file /etc/group: usersftp1:x:5001 file /etc/ssh/sshd_config: … Subsystem sftp internal-sftp -l INFO # This is commented, so no rules will be set for user "usersftp1" # Match user usersftp1 # X11Forwarding no # ChrootDirectory %h # AllowTcpForwarding no # ForceCommand internal-sftp # This config works as expected Match user usersftp2 X11Forwarding no ChrootDirectory %h AllowTcpForwarding no ForceCommand internal-sftp
在auth.log ,我可以看到用户使用sftp:
Starting session: subsystem 'sftp' for usersftp1 from xxx.xxx.xxx.xxx port 55552
在这种configuration中, service ssh restart ,用户usersftp1和usersftp2都可以使用sftp进行连接(除了第一个被注释的事实之外)
我认为Match User不会像你认为的那样工作。
比赛
引入一个条件块。 如果匹配行上的所有条件都满足,则以下行中的关键字将覆盖configuration文件全局部分中设置的关键字,直到另一个匹配行或文件末尾。 如果关键字出现在满足的多个匹配块中,则仅应用关键字的第一个实例。
因此,在您的特定情况下, 匹配用户usersftp2仅适用于并覆盖usersftp2的configuration。
当您的用户ftp1连接时,sftp程序由sshd为他们运行,并且不应用任何用户特定的configuration。
如果我正确理解你的问题,你应该可以做一个你想要的小组。
创build一个组,例如sftpusers(groupadd …),然后添加你想要允许的用户(usermod -G …)
然后将sshdconfiguration为仅允许该组中的用户使用sftp
Match Group sftpusers # Force the connection to use SFTP ForceCommand internal-sftp # Disable tunneling, authentication agent, TCP and X11 forwarding. PermitTunnel no AllowAgentForwarding no AllowTcpForwarding no X11Forwarding no