当前位置: 移动技术网 > 网络运营>服务器>Linux > Linux 为特定的用户或用户组启用或禁用 SSH的方法

Linux 为特定的用户或用户组启用或禁用 SSH的方法

2020年05月12日  | 移动技术网网络运营  | 我要评论

由于你的公司标准规定,你可能只能允许部分人访问 linux 系统。或者你可能只能够允许几个用户组中的用户访问 linux 系统。那么如何实现这样的要求呢?最好的方法是什么呢?如何使用一个简单的方法去实现呢?

是的,我们会有很多种方法去实现它。但是我们应该使用简单轻松的方法。为了简单轻松的完成目的,我们可以通过对 /etc/ssh/sshd_config 文件做必要的修改来实现。在这篇文章中我们将会向你展示实现要求的详细步骤。

为什么我们要这样做呢?是出于安全的原因。你可以访问这个来获取更多关于 openssh 的使用方法。

什么是 ssh ?

openssh 全称为 openbsd secure shell。secure shell(ssh)是一个自由开源的网络工具,它能让我们在一个不安全的网络中通过使用 secure shell(ssh)协议来安全访问远程主机。

它采用了客户端-服务器架构(c/s),拥有用户身份认证、加密、在计算机和隧道之间传输文件等功能。

我们也可以用 telnet 或 rcp 等传统工具来完成,但是这些工具都不安全,因为它们在执行任何动作时都会使用明文来传输密码。

如何在 linux 中允许用户使用 ssh?

通过以下内容,我们可以为指定的用户或用户列表启用 ssh 访问。如果你想要允许多个用户,那么你可以在添加用户时在同一行中用空格来隔开他们。

为了达到目的只需要将下面的值追加到 /etc/ssh/sshd_config 文件中去。 在这个例子中, 我们将会允许用户 user3 使用 ssh。

# echo "allowusers user3" >> /etc/ssh/sshd_config

你可以运行下列命令再次检查是否添加成功。

# cat /etc/ssh/sshd_config | grep -i allowusers allowusers user3

这样就行了, 现在只需要重启 ssh 服务和见证奇迹了。(下面这两条命令效果相同, 请根据你的服务管理方式选择一条执行即可)

# systemctl restart sshd
或
# service restart sshd

接下来很简单,只需打开一个新的终端或者会话尝试用不同的用户身份访问 linux 系统。是的,这里 user2 用户是不被允许使用 ssh 登录的并且会得到如下所示的错误信息。

# ssh user2@192.168.1.4
user2@192.168.1.4's password: 
permission denied, please try again.

输出:

mar 29 02:00:35 centos7 sshd[4900]: user user2 from 192.168.1.6 not allowed because not listed in allowusers mar 29 02:00:35 centos7 sshd[4900]: input_userauth_request: invalid user user2 [preauth] mar 29 02:00:40 centos7 unix_chkpwd[4902]: password check failed for user (user2) mar 29 02:00:40 centos7 sshd[4900]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.1.6 user=user2 mar 29 02:00:43 centos7 sshd[4900]: failed password for invalid user user2 from 192.168.1.6 port 42568 ssh2

与此同时用户 user3 被允许登入系统因为他在被允许的用户列表中。

# ssh user3@192.168.1.4
user3@192.168.1.4's password: 
[user3@centos7 ~]$

输出:

mar 29 02:01:13 centos7 sshd[4939]: accepted password for user3 from 192.168.1.6 port 42590 ssh2 mar 29 02:01:13 centos7 sshd[4939]: pam_unix(sshd:session): session opened for user user3 by (uid=0)

如何在 linux 中阻止用户使用 ssh ?

通过以下内容,我们可以配置指定的用户或用户列表禁用 ssh。如果你想要禁用多个用户,那么你可以在添加用户时在同一行中用空格来隔开他们。

为了达到目的只需要将以下值追加到 /etc/ssh/sshd_config 文件中去。 在这个例子中, 我们将禁用用户 user1 使用 ssh。

# echo "denyusers user1" >> /etc/ssh/sshd_config

你可以运行下列命令再次检查是否添加成功。

# cat /etc/ssh/sshd_config | grep -i denyusers
denyusers user1

这样就行了, 现在只需要重启 ssh 服务和见证奇迹了。

# systemctl restart sshd
活
# service restart sshd

接下来很简单,只需打开一个新的终端或者会话,尝试使用被禁用的用户身份被访问 linux 系统。是的,这里 user1 用户在禁用名单中。所以,当你尝试登录时,你将会得到如下所示的错误信息。

# ssh user1@192.168.1.4
user1@192.168.1.4's password: 
permission denied, please try again.

输出:

mar 29 01:53:42 centos7 sshd[4753]: user user1 from 192.168.1.6 not allowed because listed in denyusers mar 29 01:53:42 centos7 sshd[4753]: input_userauth_request: invalid user user1 [preauth] mar 29 01:53:46 centos7 unix_chkpwd[4755]: password check failed for user (user1) mar 29 01:53:46 centos7 sshd[4753]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.1.6 user=user1 mar 29 01:53:48 centos7 sshd[4753]: failed password for invalid user user1 from 192.168.1.6 port 42522 ssh2

如何在 linux 中允许用户组使用 ssh?

通过以下内容,我们可以允许一个指定的组或多个组使用 ssh。

如果你想要允许多个组使用 ssh 那么你在添加用户组时需要在同一行中使用空格来隔开他们。

为了达到目的只需将以下值追加到 /etc/ssh/sshd_config 文件中去。在这个例子中,我们将允许 2g-admin 组使用 ssh。

# echo "allowgroups 2g-admin" >> /etc/ssh/sshd_config

你可以运行下列命令再次检查是否添加成功。

# cat /etc/ssh/sshd_config | grep -i allowgroups
allowgroups 2g-admin

运行下列命令查看属于该用户组的用户有哪些。

# getent group 2g-admin
2g-admin:x:1005:user1,user2,user3

这样就行了, 现在只需要重启 ssh 服务和见证奇迹了。

# systemctl restart sshd
或
# service restart sshd

是的, user1 被允许登入系统因为用户 user1 属于 2g-admin 组。

# ssh user1@192.168.1.4
user1@192.168.1.4's password: 
[user1@centos7 ~]$

输出:

mar 29 02:10:21 centos7 sshd[5165]: accepted password for user1 from 192.168.1.6 port 42640 ssh2
mar 29 02:10:22 centos7 sshd[5165]: pam_unix(sshd:session): session opened for user user1 by (uid=0)

是的, user2 被允许登入系统因为用户 user2 同样属于 2g-admin 组。

# ssh user2@192.168.1.4
user2@192.168.1.4's password: 
[user2@centos7 ~]$

输出:

mar 29 02:10:38 centos7 sshd[5225]: accepted password for user2 from 192.168.1.6 port 42642 ssh2 mar 29 02:10:38 centos7 sshd[5225]: pam_unix(sshd:session): session opened for user user2 by (uid=0)

当你尝试使用其他不在被允许的组中的用户去登入系统时, 你将会得到如下所示的错误信息。

# ssh password: permission denied, please try again.

输出:

mar 29 02:12:36 centos7 sshd[5306]: user ladmin from 192.168.1.6 not allowed because none of user's groups are listed in allowgroups mar 29 02:12:36 centos7 sshd[5306]: input_userauth_request: invalid user ladmin [preauth] mar 29 02:12:56 centos7 unix_chkpwd[5310]: password check failed for user (ladmin) mar 29 02:12:56 centos7 sshd[5306]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.1.6 user=ladmin mar 29 02:12:58 centos7 sshd[5306]: failed password for invalid user ladmin from 192.168.1.6 port 42674 ssh2

如何在 linux 中阻止用户组使用 ssh?

通过以下内容,我们可以禁用指定的组或多个组使用 ssh。

如果你想要禁用多个用户组使用 ssh,那么你需要在添加用户组时在同一行中使用空格来隔开他们。

为了达到目的只需要将下面的值追加到 /etc/ssh/sshd_config 文件中去。

# echo "denygroups 2g-admin" >> /etc/ssh/sshd_config

你可以运行下列命令再次检查是否添加成功。

# # cat /etc/ssh/sshd_config | grep -i denygroups
denygroups 2g-admin
# getent group 2g-admin
2g-admin:x:1005:user1,user2,user3

这样就行了, 现在只需要重启 ssh 服务和见证奇迹了。

# systemctl restart sshd
或
# service restart sshd

是的 user1 不被允许登入系统,因为他是 2g-admin 用户组中的一员。他属于被禁用 ssh 的组中。

# ssh user1@192.168.1.4
user1@192.168.1.4's password: 
permission denied, please try again.

输出:

mar 29 02:17:32 centos7 sshd[5400]: user user1 from 192.168.1.6 not allowed because a group is listed in denygroups mar 29 02:17:32 centos7 sshd[5400]: input_userauth_request: invalid user user1 [preauth] mar 29 02:17:38 centos7 unix_chkpwd[5402]: password check failed for user (user1) mar 29 02:17:38 centos7 sshd[5400]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.1.6 user=user1 mar 29 02:17:41 centos7 sshd[5400]: failed password for invalid user user1 from 192.168.1.6 port 42710 ssh2

除了 2g-admin 用户组之外的用户都可以使用 ssh 登入系统。 例如,ladmin 等用户就允许登入系统。

# ssh ladmin@192.168.1.4
ladmin@192.168.1.4's password: 
[ladmin@centos7 ~]$

输出:

mar 29 02:19:13 centos7 sshd[5432]: accepted password for ladmin from 192.168.1.6 port 42716 ssh2 mar 29 02:19:13 centos7 sshd[5432]: pam_unix(sshd:session): session opened for user ladmin by (uid=0) via:

总结

到此这篇关于如何在 linux 上为特定的用户或用户组启用或禁用 ssh的文章就介绍到这了,更多相关linux 用户或用户组ssh内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网