Linux 为特定的用户或用户组启用或禁用 SSH的方法
由于你的公司标准规定,你可能只能允许部分人访问Linux系统。或者你可能只能够允许几个用户组中的用户访问Linux系统。那么如何实现这样的要求呢?最好的方法是什么呢?如何使用一个简单的方法去实现呢?
是的,我们会有很多种方法去实现它。但是我们应该使用简单轻松的方法。为了简单轻松的完成目的,我们可以通过对/etc/ssh/sshd_config文件做必要的修改来实现。在这篇文章中我们将会向你展示实现要求的详细步骤。
为什么我们要这样做呢?是出于安全的原因。你可以访问这个链接来获取更多关于openSSH的使用方法。
什么是SSH?
openssh全称为OpenBSDSecureShell。SecureShell(ssh)是一个自由开源的网络工具,它能让我们在一个不安全的网络中通过使用SecureShell(SSH)协议来安全访问远程主机。
它采用了客户端-服务器架构(C/S),拥有用户身份认证、加密、在计算机和隧道之间传输文件等功能。
我们也可以用telnet或rcp等传统工具来完成,但是这些工具都不安全,因为它们在执行任何动作时都会使用明文来传输密码。
如何在Linux中允许用户使用SSH?
通过以下内容,我们可以为指定的用户或用户列表启用ssh访问。如果你想要允许多个用户,那么你可以在添加用户时在同一行中用空格来隔开他们。
为了达到目的只需要将下面的值追加到/etc/ssh/sshd_config文件中去。在这个例子中,我们将会允许用户user3使用ssh。
#echo"AllowUsersuser3">>/etc/ssh/sshd_config
你可以运行下列命令再次检查是否添加成功。
#cat/etc/ssh/sshd_config|grep-iallowusersAllowUsersuser3
这样就行了,现在只需要重启ssh服务和见证奇迹了。(下面这两条命令效果相同,请根据你的服务管理方式选择一条执行即可)
#systemctlrestartsshd 或 #servicerestartsshd
接下来很简单,只需打开一个新的终端或者会话尝试用不同的用户身份访问Linux系统。是的,这里user2用户是不被允许使用SSH登录的并且会得到如下所示的错误信息。
#sshuser2@192.168.1.4 user2@192.168.1.4'spassword: Permissiondenied,pleasetryagain.
输出:
Mar2902:00:35CentOS7sshd[4900]:Useruser2from192.168.1.6notallowedbecausenotlistedinAllowUsersMar2902:00:35CentOS7sshd[4900]:input_userauth_request:invaliduseruser2[preauth]Mar2902:00:40CentOS7unix_chkpwd[4902]:passwordcheckfailedforuser(user2)Mar2902:00:40CentOS7sshd[4900]:pam_unix(sshd:auth):authenticationfailure;logname=uid=0euid=0tty=sshruser=rhost=192.168.1.6user=user2Mar2902:00:43CentOS7sshd[4900]:Failedpasswordforinvaliduseruser2from192.168.1.6port42568ssh2
与此同时用户user3被允许登入系统因为他在被允许的用户列表中。
#sshuser3@192.168.1.4 user3@192.168.1.4'spassword: [user3@CentOS7~]$
输出:
Mar2902:01:13CentOS7sshd[4939]:Acceptedpasswordforuser3from192.168.1.6port42590ssh2Mar2902:01:13CentOS7sshd[4939]:pam_unix(sshd:session):sessionopenedforuseruser3by(uid=0)
如何在Linux中阻止用户使用SSH?
通过以下内容,我们可以配置指定的用户或用户列表禁用ssh。如果你想要禁用多个用户,那么你可以在添加用户时在同一行中用空格来隔开他们。
为了达到目的只需要将以下值追加到/etc/ssh/sshd_config文件中去。在这个例子中,我们将禁用用户user1使用ssh。
#echo"DenyUsersuser1">>/etc/ssh/sshd_config
你可以运行下列命令再次检查是否添加成功。
#cat/etc/ssh/sshd_config|grep-idenyusers DenyUsersuser1
这样就行了,现在只需要重启ssh服务和见证奇迹了。
#systemctlrestartsshd 活 #servicerestartsshd
接下来很简单,只需打开一个新的终端或者会话,尝试使用被禁用的用户身份被访问Linux系统。是的,这里user1用户在禁用名单中。所以,当你尝试登录时,你将会得到如下所示的错误信息。
#sshuser1@192.168.1.4 user1@192.168.1.4'spassword: Permissiondenied,pleasetryagain.
输出:
Mar2901:53:42CentOS7sshd[4753]:Useruser1from192.168.1.6notallowedbecauselistedinDenyUsersMar2901:53:42CentOS7sshd[4753]:input_userauth_request:invaliduseruser1[preauth]Mar2901:53:46CentOS7unix_chkpwd[4755]:passwordcheckfailedforuser(user1)Mar2901:53:46CentOS7sshd[4753]:pam_unix(sshd:auth):authenticationfailure;logname=uid=0euid=0tty=sshruser=rhost=192.168.1.6user=user1Mar2901:53:48CentOS7sshd[4753]:Failedpasswordforinvaliduseruser1from192.168.1.6port42522ssh2
如何在Linux中允许用户组使用SSH?
通过以下内容,我们可以允许一个指定的组或多个组使用ssh。
如果你想要允许多个组使用ssh那么你在添加用户组时需要在同一行中使用空格来隔开他们。
为了达到目的只需将以下值追加到/etc/ssh/sshd_config文件中去。在这个例子中,我们将允许2g-admin组使用ssh。
#echo"AllowGroups2g-admin">>/etc/ssh/sshd_config
你可以运行下列命令再次检查是否添加成功。
#cat/etc/ssh/sshd_config|grep-iallowgroups AllowGroups2g-admin
运行下列命令查看属于该用户组的用户有哪些。
#getentgroup2g-admin 2g-admin:x:1005:user1,user2,user3
这样就行了,现在只需要重启ssh服务和见证奇迹了。
#systemctlrestartsshd 或 #servicerestartsshd
是的,user1被允许登入系统因为用户user1属于2g-admin组。
#sshuser1@192.168.1.4 user1@192.168.1.4'spassword: [user1@CentOS7~]$
输出:
Mar2902:10:21CentOS7sshd[5165]:Acceptedpasswordforuser1from192.168.1.6port42640ssh2
Mar2902:10:22CentOS7sshd[5165]:pam_unix(sshd:session):sessionopenedforuseruser1by(uid=0)
是的,user2被允许登入系统因为用户user2同样属于2g-admin组。
#sshuser2@192.168.1.4 user2@192.168.1.4'spassword: [user2@CentOS7~]$
输出:
Mar2902:10:38CentOS7sshd[5225]:Acceptedpasswordforuser2from192.168.1.6port42642ssh2Mar2902:10:38CentOS7sshd[5225]:pam_unix(sshd:session):sessionopenedforuseruser2by(uid=0)
当你尝试使用其他不在被允许的组中的用户去登入系统时,你将会得到如下所示的错误信息。
#sshladmin@192.168.1.4ladmin@192.168.1.4'spassword:Permissiondenied,pleasetryagain.
输出:
Mar2902:12:36CentOS7sshd[5306]:Userladminfrom192.168.1.6notallowedbecausenoneofuser'sgroupsarelistedinAllowGroupsMar2902:12:36CentOS7sshd[5306]:input_userauth_request:invaliduserladmin[preauth]Mar2902:12:56CentOS7unix_chkpwd[5310]:passwordcheckfailedforuser(ladmin)Mar2902:12:56CentOS7sshd[5306]:pam_unix(sshd:auth):authenticationfailure;logname=uid=0euid=0tty=sshruser=rhost=192.168.1.6user=ladminMar2902:12:58CentOS7sshd[5306]:Failedpasswordforinvaliduserladminfrom192.168.1.6port42674ssh2
如何在Linux中阻止用户组使用SSH?
通过以下内容,我们可以禁用指定的组或多个组使用ssh。
如果你想要禁用多个用户组使用ssh,那么你需要在添加用户组时在同一行中使用空格来隔开他们。
为了达到目的只需要将下面的值追加到/etc/ssh/sshd_config文件中去。
#echo"DenyGroups2g-admin">>/etc/ssh/sshd_config
你可以运行下列命令再次检查是否添加成功。
##cat/etc/ssh/sshd_config|grep-idenygroups DenyGroups2g-admin #getentgroup2g-admin 2g-admin:x:1005:user1,user2,user3
这样就行了,现在只需要重启ssh服务和见证奇迹了。
#systemctlrestartsshd 或 #servicerestartsshd
是的user1不被允许登入系统,因为他是2g-admin用户组中的一员。他属于被禁用ssh的组中。
#sshuser1@192.168.1.4 user1@192.168.1.4'spassword: Permissiondenied,pleasetryagain.
输出:
Mar2902:17:32CentOS7sshd[5400]:Useruser1from192.168.1.6notallowedbecauseagroupislistedinDenyGroupsMar2902:17:32CentOS7sshd[5400]:input_userauth_request:invaliduseruser1[preauth]Mar2902:17:38CentOS7unix_chkpwd[5402]:passwordcheckfailedforuser(user1)Mar2902:17:38CentOS7sshd[5400]:pam_unix(sshd:auth):authenticationfailure;logname=uid=0euid=0tty=sshruser=rhost=192.168.1.6user=user1Mar2902:17:41CentOS7sshd[5400]:Failedpasswordforinvaliduseruser1from192.168.1.6port42710ssh2
除了2g-admin用户组之外的用户都可以使用ssh登入系统。例如,ladmin等用户就允许登入系统。
#sshladmin@192.168.1.4 ladmin@192.168.1.4'spassword: [ladmin@CentOS7~]$
输出:
Mar2902:19:13CentOS7sshd[5432]:Acceptedpasswordforladminfrom192.168.1.6port42716ssh2Mar2902:19:13CentOS7sshd[5432]:pam_unix(sshd:session):sessionopenedforuserladminby(uid=0)via:https://www.2daygeek.com/allow-deny-enable-disable-ssh-access-user-group-in-linux/
总结
到此这篇关于如何在Linux上为特定的用户或用户组启用或禁用SSH的文章就介绍到这了,更多相关linux用户或用户组ssh内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!