Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of running commands as root by ordinary users in Linux

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Xiaobian to share with you the ordinary users in Linux to root identity to run the command method, I believe most people do not know how to share this article for your reference, I hope you have a lot of harvest after reading this article, let us go to understand it together!

Linux follows a very tough permissions model, where root can do anything; ordinary users don't have permissions and can't run any commands.

Method 1: Use the su command

For regular users to run any command, they need to request permission from the superuser. A simple and common way to grant administrative privileges to a normal user is to temporarily become root using the su command, so that the normal user can perform any action because all privileges of root are granted to the normal user.

Note: The su command allows a normal user to run commands as root, provided that the root password is known.

[userA@rhel7 ~]$ su -

Output:

Password:

It requires the root password, but you can't run any commands without knowing the root password.

Method 2: Use the sudo command

The sudo command allows a user to run commands as root or any other user after authenticating with their own password.

Sudo is safer than su. By default, it logs sudo usage, commands, and parameters in/ var / log / secure (Red Hat / Fedora / CentOS Linux) or/var/log/auth.log (Ubuntu / Debian Linux).

If the calling user is root or the target user is the same as the calling user, no password is required. Otherwise, sudo requires users to authenticate themselves with a password by default. Once the user is authenticated, the timestamp is updated and the user can then use sudo for a short time without a password (15 minutes unless overridden in sudoers).

How to use sudo?

Example: Allows user Rocky to access the Stop/Close command and restart Apache Web server. First, log in as root user. Edit the configuration file using the visudo command:

# visudo

Attach the following lines to the file:

rokcy localhost=/sbin/haltrokcy dbserver=/etc/init.d/apache-perl restart

Save and close the file. Rokcy users can now restart the Apache Web server by typing:

$ sudo /etc/init.d/apache-perl restart

Output:

Password:Restarting apache-perl 1.3 web server....

The sudo command has logged the attempt to the log file/var/log/secure or/var/log/auth.log file:

# tail -f /var/log/auth.log

If rokcy wants to shut down the computer, he also needs to enter the command:

$ sudo /sbin/halt

Output:

Password:

Users usually provide a password before running the command with sudo. After authentication, run this command if the/ etc / sudoers configuration file allows user access. Sudo records each command run.

That's all about how a normal user can run commands as root in Linux. Thanks for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report