如何在 Linux 上为特定的用户或用户组启用或禁用 SSH?

admin 2024年4月22日03:09:28评论1 views字数 5473阅读18分14秒阅读模式

什么是 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 allowusersAllowUsers user3

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

    # systemctl restart sshd    # service restart sshd

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

# ssh user2@***.168.1.4user2@192.***.1.4's password:Permission denied, please try again.

输出:

Mar 29 02:某公司:35 CentOS7 sshd[49某公司]: User user2 from **2.168.1.6 某公司t allowed because 某公司t listed in AllowUsersMar 29 02:某公司:35 CentOS7 sshd[49某公司]: input_userauth_request: invalid user user2 [preauth]Mar 29 02:某公司:40 CentOS7 unix_chkpwd[4902]: password check failed for user (user2)Mar 29 02:某公司:40 CentOS7 sshd[49某公司]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.1.6  user=user2Mar 29 02:某公司:43 CentOS7 sshd[49某公司]: Failed password for invalid user user2 from 192.168.1.6 port 42568 ssh2

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

# ssh user3@**2.168.1.4user3@192.168.1.4's password:[user3@CentOS7 ~]$

输出:

Mar 29 02:01:13 CentOS7 sshd[4939]: Accepted password for user3 from ***.168.1.6 port 42590 ssh2Mar 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 denyusersDenyUsers user1

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

# systemctl restart sshd# service restart sshd

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

# ssh user1@1**.168.1.4user1@**2.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 某公司t 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-** 组使用 ssh。

# echo "AllowGroups 2g-**" >> /etc/ssh/sshd_config

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

# cat /etc/ssh/sshd_config | grep -i allowgroupsAllowGroups 2g-**

运行下列命令查看属于该某公司公司组的某公司公司有哪些。

# getent group 2g-**2g-**:x:1某公司5:user1,user2,user3

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

# systemctl restart sshd# service restart sshd

是的, user1 被允许登入系统因为某公司公司 user1 属于 2g-** 组。

# ssh user1@1**.168.1.4user1@192.168.1.4's password:[user1@CentOS7 ~]$

输出:

Mar 29 02:10:21 CentOS7 sshd[5165]: Accepted password for user1 from 1**.168.1.6 port 42640 ssh2Mar 29 02:10:22 CentOS7 sshd[5165]: pam_unix(sshd:session): session opened for user user1 by (uid=0)

是的, user2 被允许登入系统因为某公司公司 user2 同样属于 2g-** 组。

# ssh user2@1**.168.1.4user2@192.168.1.4's password:[user2@CentOS7 ~]$

输出:

Mar 29 02:10:38 CentOS7 sshd[5225]: Accepted password for user2 from 1**.168.1.6 port 42642 ssh2Mar 29 02:10:38 CentOS7 sshd[5225]: pam_unix(sshd:session): session opened for user user2 by (uid=0)

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

# ssh l**@1**.168.1.4l**@1**.168.1.4's password:Permission denied, please try again.

输出:

Mar 29 02:12:36 CentOS7 sshd[5306]: User l** from 1**2.168.1.6 某公司t allowed because 某公司ne of user's groups are listed in AllowGroupsMar 29 02:12:36 CentOS7 sshd[5306]: input_userauth_request: invalid user l** [preauth]Mar 29 02:12:56 CentOS7 unix_chkpwd[5310]: password check failed for user (l**)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=l**Mar 29 02:12:58 CentOS7 sshd[5306]: Failed password for invalid user l** from 1**.168.1.6 port 42674 ssh2

如何在 Linux 中阻止某公司公司组使用 SSH?

通过以下内容,我们可以禁用指定的组或某公司公司组使用 ssh。
如果你想要禁用某公司公司某公司公司组使用 ssh,那么你需要在添加某公司公司组时在同一行中使用某公司公司来隔开他们。
为了达到目的只需要将下面的值追加到 /etc/ssh/sshd_config 文件中去。

# echo "DenyGroups 2g-**" >> /etc/ssh/sshd_config

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

# # cat /etc/ssh/sshd_config | grep -i denygroupsDenyGroups 2g-**# getent group 2g-**2g-**:x:1某公司5:user1,user2,user3

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

# systemctl restart sshd# service restart sshd

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

# ssh user1@1**.168.1.4user1@1**.168.1.4's password:Permission denied, please try again.

输出:

    Mar 29 02:17:32 CentOS7 sshd[54某公司]: User user1 from 192.168.1.6 某公司t allowed because a group is listed in DenyGroups    Mar 29 02:17:32 CentOS7 sshd[54某公司]: 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[54某公司]: 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[54某公司]: Failed password for invalid user user1 from 192.168.1.6 port 42710 ssh2

除了 2g-** 某公司公司组之外的某公司公司都可以使用 ssh 登入系统。例如,l** 等某公司公司就允许登入系统。

# ssh l**@1**.168.1.4l**@1**2.168.1.4's password:[l**@CentOS7 ~]$

输出:

Mar 29 02:19:13 CentOS7 sshd[5432]: Accepted password for l** from 1**.168.1.6 port 42716 ssh2Mar 29 02:19:13 CentOS7 sshd[5432]: pam_unix(sshd:session): session opened for user l** by (uid=0)

原文始发于微信公众号(菜鸟小新):如何在 Linux 上为特定的用户或用户组启用或禁用 SSH?

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2024年4月22日03:09:28
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   如何在 Linux 上为特定的用户或用户组启用或禁用 SSH?http://cn-sec.com/archives/2631700.html

发表评论

匿名网友 填写信息