搜尋
首頁系統教程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
Linux系統管理員的主要任務是什麼?Linux系統管理員的主要任務是什麼?Apr 19, 2025 am 12:23 AM

Linux系統管理員的主要任務包括系統監控與性能調優、用戶管理、軟件包管理、安全管理與備份、故障排查與解決、性能優化與最佳實踐。 1.使用top、htop等工具監控系統性能,並進行調優。 2.通過useradd等命令管理用戶賬戶和權限。 3.利用apt、yum管理軟件包,確保系統更新和安全。 4.配置防火牆、監控日誌、進行數據備份以確保系統安全。 5.通過日誌分析和工具使用進行故障排查和解決。 6.優化內核參數和應用配置,遵循最佳實踐提升系統性能和穩定性。

很難學習Linux嗎?很難學習Linux嗎?Apr 18, 2025 am 12:23 AM

學習Linux並不難。 1.Linux是一個開源操作系統,基於Unix,廣泛應用於服務器、嵌入式系統和個人電腦。 2.理解文件系統和權限管理是關鍵,文件系統是層次化的,權限包括讀、寫和執行。 3.包管理系統如apt和dnf使得軟件管理方便。 4.進程管理通過ps和top命令實現。 5.從基本命令如mkdir、cd、touch和nano開始學習,再嘗試高級用法如shell腳本和文本處理。 6.常見錯誤如權限問題可以通過sudo和chmod解決。 7.性能優化建議包括使用htop監控資源、清理不必要文件和使用sy

Linux管理員的薪水是多少?Linux管理員的薪水是多少?Apr 17, 2025 am 12:24 AM

Linux管理員的平均年薪在美國為75,000至95,000美元,歐洲為40,000至60,000歐元。提升薪資可以通過:1.持續學習新技術,如雲計算和容器技術;2.積累項目經驗並建立Portfolio;3.建立職業網絡,拓展人脈。

Linux的主要目的是什麼?Linux的主要目的是什麼?Apr 16, 2025 am 12:19 AM

Linux的主要用途包括:1.服務器操作系統,2.嵌入式系統,3.桌面操作系統,4.開發和測試環境。 Linux在這些領域表現出色,提供了穩定性、安全性和高效的開發工具。

互聯網在Linux上運行嗎?互聯網在Linux上運行嗎?Apr 14, 2025 am 12:03 AM

互聯網運行不依賴單一操作系統,但Linux在其中扮演重要角色。 Linux廣泛應用於服務器和網絡設備,因其穩定性、安全性和可擴展性受歡迎。

Linux操作是什麼?Linux操作是什麼?Apr 13, 2025 am 12:20 AM

Linux操作系統的核心是其命令行界面,通過命令行可以執行各種操作。 1.文件和目錄操作使用ls、cd、mkdir、rm等命令管理文件和目錄。 2.用戶和權限管理通過useradd、passwd、chmod等命令確保系統安全和資源分配。 3.進程管理使用ps、kill等命令監控和控制系統進程。 4.網絡操作包括ping、ifconfig、ssh等命令配置和管理網絡連接。 5.系統監控和維護通過top、df、du等命令了解系統運行狀態和資源使用情況。

使用Linux別名提高自定義命令快捷方式的生產率使用Linux別名提高自定義命令快捷方式的生產率Apr 12, 2025 am 11:43 AM

介紹 Linux是一個強大的操作系統,由於其靈活性和效率,開發人員,系統管理員和電源用戶都喜歡。但是,經常使用長而復雜的命令可能是乏味的

Linux實際上有什麼好處?Linux實際上有什麼好處?Apr 12, 2025 am 12:20 AM

Linux適用於服務器、開發環境和嵌入式系統。 1.作為服務器操作系統,Linux穩定高效,常用於部署高並發應用。 2.作為開發環境,Linux提供高效的命令行工具和包管理系統,提升開發效率。 3.在嵌入式系統中,Linux輕量且可定制,適合資源有限的環境。

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脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中