How to create new user on CentOS 7 server?

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

  1. Log in to the CentOS server as root user.
  2. Run the following command to create a new user:
    useradd -m -s /bin/bash username
    

    where usernameis the username of the new user you want to create. This command will create a new user and assign it a home directory.

  3. Run the following command to set the new user's password:
    passwd username
    

    where usernameis the username of the user whose password you want to set. The command will prompt you for a new password and confirm it.

  4. 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:
    visudo
    
    Find the following line in the file:
    ## Allow root to run any commands anywhere
    root    ALL=(ALL)       ALL
    
    Add the following line below that line:
    ## Allow username to run any commands anywhere
    username    ALL=(ALL)       ALL
    

    where usernameis the username of the new user you want to add to the sudoers file.

  5. 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 groupnameis the name of the user group you want to add the new user to and usernameis the username of the new user you want to add to the user group.

  6. Run the following command to see which user groups the new user belongs to:
    groups username
    

    where usernameis the username of the user whose user group you want to view.

How to switch to a new user?

  1. Log in to the CentOS server as the currently logged in user.
  2. Run the following command to switch to the newly created user account:
    su - username
    

    where usernameis the username of the newly created user account you want to switch to.

  3. 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 commandis the command that requires root privileges that you want to execute under the newly created user account.

Guess you like

Origin blog.csdn.net/qq_34562959/article/details/130379691