搜索
首页系统教程LINUX如何将su命令限制在Linux中的授权用户

The su command is a powerful tool that can be used to switch to another user's account. However, it can also be used by bad persons to gain unauthorized access to your system. By restricting the use of the su command, you can help to protect your Linux system from unauthorized access.

Table of Contents

Introduction

In our previous tutorial, we learned how to "grant or deny sudo access to a group" in Linux to make things safer and manage groups better. However, this alone may not be enough.

If a bad person gains access to an account without sudo privileges, they can still try to login as root or another user with sudo privileges by using the su command. As you already know, the su command allows users to execute commands as other accounts, including the root account.

To address this concern, we need to ensure that only specific accounts have the privilege to use the su command. This extra step will add even more security to our system. In this brief tutorial, we will learn how to restrict the su command usage to authorized users in Linux.

Here are some of the reasons why you might want to restrict the use of the su command:

  • To prevent unauthorized users from gaining access to sensitive data.
  • To prevent unauthorized users from making changes to system settings.
  • To prevent unauthorized users from installing malicious software.

1. Restrict Su Command Usage to Particular Users in Debian and its Derivatives

To limit who can use su command, we need to create a dedicated group and allow only the members of that particular group can use su command. Let us see how to do that.

1. For the purpose of this guide, I am going to create two users namely user1 and user2.

$ sudo adduser user1
$ sudo adduser user2

2. Next, create a new group called adminmembers.

$ sudo groupadd adminmembers

3. Add the 'user1' and 'user2' to the group.

$ sudo usermod -aG adminmembers user1
$ sudo usermod -aG adminmembers user2

Similarly, add all other users to this group.

You can verify the members of this group using command:

$ getent group adminmembers

Sample output:

adminmembers:x:1005:user1,user2

4. Now, run the following command to restrict the su command to only the members of the 'adminmembers' group:

$ sudo dpkg-statoverride --update --add root adminmembers 4750 /bin/su

Here, the sudo dpkg-statoverride --update --add command is used to override file permissions and ownership for a specific file on a Debian-based Linux distribution. In this case, the command is setting a special permission and ownership for the /bin/su binary.

Let's break down the command:

  • sudo: This is used to run the command with superuser (root) privileges. You will need administrative privileges to modify system files.
  • dpkg-statoverride: This is a command-line tool in Debian-based systems that allows you to override file permissions and ownership of packages managed by the package manager.
  • --update: This option tells dpkg-statoverride to update the specified override if it already exists.
  • --add: This option indicates that we want to add a new override.
  • root: This specifies the user whose ownership will be set for the file.
  • adminmembers: This specifies the group whose ownership will be set for the file.
  • 4750: This is the numeric representation of the file permissions. The value 4750 is a special permission called "SetUID" (Set User ID) on execution. When the su binary has the SetUID bit set, it runs with the effective user ID of the file owner (root) instead of the user who executed it. This allows regular users to switch to the root user's privileges temporarily when running su.
  • /bin/su: This is the path to the file for which the override is being set. In this case, it's the /bin/su binary, which allows users to switch to another user's account (often the root user) after providing the necessary password.

So, the command is adding an override for the permissions and ownership of the /bin/su binary. It sets the file's owner to root, group to adminmembers, and gives the SetUID permission to the file. This means that when users run the su command, it will run with root privileges, allowing them to switch to the root user or another user with superuser privileges after providing the appropriate password. The non-members of the 'adminmembers' group can't use su command, even if they have sudo privilege.

Let us verify it. Right now, I'm logged into my system as a user named 'ostechnix', and this user has sudo privileges.

$ sudo -lU ostechnix
[sudo] password for ostechnix: 
Matching Defaults entries for ostechnix on debian12:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin,
    use_pty

User ostechnix may run the following commands on debian12:
    <strong><mark>(ALL : ALL) ALL</mark></strong>

As you see in the above output, the user 'ostechnix' can able to run all commands.

Now, let use su command and see what happens.

$ su - user1

Sample output:

-bash: /usr/bin/su: Permission denied

See? The user 'ostechnix' can't use the su command despite having sudo privilege because he is not a member of the 'adminmembers' group. Because, we limited the su command usage to only the members of that specific group.

如何将su命令限制在Linux中的授权用户

Now log out from the current session and log back in as 'user1' or 'user2' or any member of the 'adminmembers' group.

And, check if su command works:

user1@debian12:~$ whoami
user1
user1@debian12:~$ 
user1@debian12:~$ su - user2
Password: 
user2@debian12:~$ 
user2@debian12:~$ whoami
user2
user2@debian12:~$ 
user2@debian12:~$ su - user1
Password: 
user1@debian12:~$ 
user1@debian12:~$ whoami
user1
user1@debian12:~$ 

如何将su命令限制在Linux中的授权用户

As you can observe in the above output, both 'user1' and 'user2' have the ability to switch between different user accounts using the su command because they are both members of the 'adminmembers' group.

1.1. Revert su Command to its Original Configuration

To undo the changes made by the command sudo dpkg-statoverride --update --add root adminmembers 4750 /bin/su, you need to remove the override that was set for the /bin/su binary. The dpkg-statoverride command allows you to manage file permissions and ownership for packages in Debian-based systems.

To remove the override for the /bin/su binary, follow these steps:

1. Check the current dpkg-statoverride configuration:

Before removing the override, it's a good idea to check if the override for /bin/su exists and what the current configuration is. Run the following command to view the current dpkg-statoverride settings:

$ dpkg-statoverride --list /bin/su

This command will show you the current permissions and ownership set for the /bin/su binary.

Sample output:

root adminmembers 4750 /bin/su

2. Remove the dpkg-statoverride entry:

To remove the override, use the following command:

$ sudo dpkg-statoverride --remove /bin/su

This command will remove the override entry for /bin/su.

3. Reset the original permissions (if needed):

If you want to reset the permissions of /bin/su to the default value, use the following command:

$ sudo chmod 4755 /bin/su

The 4755 value sets the SetUID bit on the file, which allows the su command to run with root privileges.

如何将su命令限制在Linux中的授权用户

With these steps, you should have undone the changes made by the initial sudo dpkg-statoverride --update --add root adminmembers 4750 /bin/su command and reverted the /bin/su binary to its original configuration.

Warning: Always exercise caution when modifying system configurations, especially when dealing with file permissions and ownership, as improper changes can impact system security and stability.

2. Limit the Use of Su Command in RHEL-based Systems

The previous method is only for Debian and its derivatives like Ubuntu. The process of limiting su command usage in RPM-based is different. Let us see how to do that.

To limit the use of the su command in RHEL-based systems, follow these steps:

1. Create a new user (e.g., "user1") and add him to the "wheel" group:

$ sudo adduser user1
$ sudo passwd user1
$ sudo usermod -G wheel user1

This grants the user "user1" access to the su command through the "wheel" group, which is often configured to have special privileges.

2. Open the PAM configuration file for su using a text editor:

$ sudo nano /etc/pam.d/su

3. Append the following line at the end of the file or uncomment this line if it already exists:

auth required /lib/security/pam_wheel.so use_uid

Alternatively, you can uncomment or append this line:

auth required pam_wheel.so use_uid

4. Save the changes and close the file.

With these steps, the su command will be restricted, and only users belonging to the "wheel" group will be allowed to use it.

When you try to use su command from a non-member of wheel group, you would see an output like below:

[user3@Almalinux9ct ~]$ su - user1
Password: 
su: Module is unknown

如何将su命令限制在Linux中的授权用户

To revert back to the original settings, simply comment or remove the line that you've added in the previous step.

Frequently Asked Questions

Q: What is the su command in Linux?

A: The su command, short for "switch user," allows users to execute commands as a different user, often the root user with superuser privileges.

Q: Why should I restrict the su command usage?

A: Restricting su command usage enhances security by limiting access to privileged operations, reducing the risk of unauthorized users gaining unrestricted control over the system.

Q: How can I limit the use of the su command?

A: You can limit su command usage by configuring the PAM (Pluggable Authentication Modules) file, allowing access only to specific groups or users, such as the "wheel" group.

Q: How do I grant access to su for a specific group?

A: Add the desired users to a designated group (e.g., "wheel") and configure the /etc/pam.d/su file to require this group membership for su command access.

Q: How can I check if the su command is appropriately restricted?

A: Use the su command with a non-privileged user to verify that access is limited only to authorized users or groups.

Conclusion

Restricting the usage of the su command in Linux is a vital step towards strengthening Linux system security and safeguarding sensitive information. By carefully controlling access to privileged commands, such as su, we can mitigate the risk of unauthorized users gaining elevated privileges and potentially compromising the system.

Implementing these restrictions, in conjunction with other security best practices, creates a more resilient and secure Linux environment.

Related Read:

  • How To Run Commands As Another User Via Sudo In Linux

以上是如何将su命令限制在Linux中的授权用户的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
用sed命令掌握文本操纵用sed命令掌握文本操纵Mar 16, 2025 am 09:48 AM

Linux 命令行界面提供了丰富的文本处理工具,其中最强大的工具之一是 sed 命令。sed 是 Stream EDitor 的缩写,是一个多功能工具,允许对文本文件和流进行复杂的处理。 什么是 Sed? sed 是一款非交互式文本编辑器,它操作管道输入或文本文件。通过提供指令,您可以让它修改和处理文件或流中的文本。sed 最常见的用例包括选择文本、替换文本、修改原始文件、向文本添加行或从文本中删除行等操作。它可以在 Bash 和其他命令行 shell 中从命令行使用。 Sed 命令语法 sed

如何计算Linux中的文件和目录:初学者指南如何计算Linux中的文件和目录:初学者指南Mar 19, 2025 am 10:48 AM

有效地计数Linux中的文件和文件夹:综合指南 知道如何快速计算Linux中的文件和目录对于系统管理员和管理大型数据集的任何人至关重要。本指南使用简单命令l演示

如何将用户添加到Linux中的多个组如何将用户添加到Linux中的多个组Mar 18, 2025 am 11:44 AM

有效管理用户帐户和组成员资格对于Linux/UNIX系统管理至关重要。 这样可以确保适当的资源和数据访问控制。 本教程详细介绍了如何将用户添加到Linux和Unix系统中的多个组中。 我们

用Liquorix内核增强Linux系统的秘密武器用Liquorix内核增强Linux系统的秘密武器Mar 08, 2025 pm 12:12 PM

Liquorix内核:提升Linux系统性能的利器 Linux以其灵活、安全和高性能而闻名,成为开发人员、系统管理员和高级用户的首选操作系统。然而,通用Linux内核并非总是能满足寻求最大性能和响应速度用户的需求。这就是Liquorix内核发挥作用的地方——一个针对性能优化的替代方案,有望增强您的Linux系统。本文将探讨Liquorix内核是什么,为什么您可能想要使用它,以及如何安装和配置它以充分发挥系统的性能。 Liquorix内核详解 Liquorix内核是一个预编译的Linux内核,专为

如何从Commandline列出或检查所有已安装的Linux内核如何从Commandline列出或检查所有已安装的Linux内核Mar 23, 2025 am 10:43 AM

Linux内核是GNU/Linux操作系统的核心组件。由Linus Torvalds于1991年开发,是一种免费的开源,单片,模块化和多任务Unix样核。在Linux中,可以在Sing上安装多个内核

如何在Ubuntu Linux中输入印度卢比符号如何在Ubuntu Linux中输入印度卢比符号Mar 22, 2025 am 10:39 AM

该简短指南说明了如何在Linux操作系统中键入印度卢比符号。前几天,我想在Word文档中键入“ Indian Rupee符号(€)”。我的键盘上有一个卢比符号,但我不知道如何键入它。后

在Linux中找到Leviathan文件在Linux中找到Leviathan文件Mar 13, 2025 pm 12:11 PM

介绍 在Linux领域,命令行通常是我们导航的指南针,磁盘空间的有效管理至关重要。无论您是通过个人项目航行还是转向O

安装Fedora Linux 41工作站[逐步指南]安装Fedora Linux 41工作站[逐步指南]Mar 09, 2025 am 11:21 AM

本指南为安装Fedora Linux 41 Workstation Edition提供了全面的演练。 让我们开始吧! 目录 - 步骤1:准备Fedora 41安装媒体 步骤2:启动Fedora 41安装媒体 步骤3:

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具