foreword
Due to work reasons, a new user needs to be created on the CentOS 7 server for others to use. The following are the relevant steps to create a new user, and friends in need can refer to it.
environment
CentOS 7.9
step
- Log in to the CentOS server as root user.
- Run the following command to create a new user:
useradd -m -s /bin/bash username
where
username
is the username of the new user you want to create. This command will create a new user and assign it a home directory. - Run the following command to set the new user's password:
passwd username
where
username
is the username of the user whose password you want to set. The command will prompt you for a new password and confirm it. - If you want the new user to have root privileges, add it to the sudoers file. Run the following command to edit the sudoers file:
Find the following line in the file:visudo
Add the following line below that line:## Allow root to run any commands anywhere root ALL=(ALL) ALL
## Allow username to run any commands anywhere username ALL=(ALL) ALL
where
username
is the username of the new user you want to add to the sudoers file. - If you want to add the newly created user to the user group, you can add it with the following command:
usermod -aG groupname username
where
groupname
is the name of the user group you want to add the new user to andusername
is the username of the new user you want to add to the user group. - Run the following command to see which user groups the new user belongs to:
groups username
where
username
is the username of the user whose user group you want to view.
How to switch to a new user?
- Log in to the CentOS server as the currently logged in user.
- Run the following command to switch to the newly created user account:
su - username
where
username
is the username of the newly created user account you want to switch to. - If you need to perform an operation that requires root privileges, use the sudo command. Run the following command to perform operations requiring root privileges under the newly created user account:
sudo command
where
command
is the command that requires root privileges that you want to execute under the newly created user account.