How to add a user and let the user get root privileges under linux

Test environment: CentOS 7

1. To add a user, first add a common user with the adduser command. The command is as follows:

#adduser tommy 
 //Add a user named tommy
 #passwd tommy    //Change password for user tommy . New UNIX password : //Enter the new password here Retype new UNIX password : //Enter the new password again passwd: all authentication tokens updated successfully .

2. Grant root privileges

Method 1: Modify the /etc/sudoers file, find the following line, and remove the previous comment (#)

## Allows people in group wheel to run all commands
%wheel    ALL=(ALL) ALL 

Then modify the user to belong to the root group (wheel), the command is as follows:

#usermod -g root tommy

After the modification is completed, you can now log in with the tommy account, and then use the command su - to obtain root privileges to operate.

Method 2: Modify the /etc/sudoers file, find the following line, and add a line under root, as shown below:

## Allow root to run any commands anywhere
root    ALL=(ALL) ALL tommy ALL=(ALL) ALL 

After the modification is completed, you can now log in with the tommy account, and then use the command su - to obtain root privileges to operate.

Method 3: Modify the /etc/passwd file, find the following line, and change the user ID to 0, as shown below:

tommy:x:500:500:tommy:/home/tommy:/bin/bash 

Modified as follows

tommy:x:0:500:tommy:/home/tommy:/bin/bash 

保存,用tommy账户登录后,直接获取的就是root帐号的权限。

友情提醒:虽然方法三看上去简单方便,但一般不推荐使用,推荐使用方法二。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325790378&siteId=291194637