Create a new user in Ubuntu

This article is based on the Linux Ubuntu system to create a new ordinary user, the user name of the Linux system is peng , and the host name is ubuntu

  • 1. Create a new user
  • 2. Allow the user to execute instructions as an administrator

1. Create a new user

1.1 Create a new user who can only log in under the console

  • 1) Switch to root user in order to obtain the permission to create users
peng@ubuntu:~$ sudo su
  • 2) Add a new user (for example, the user name is csdn)
root@ubuntu:/home/peng# useradd csdn
  • 3) Set a login password for the user
root@ubuntu:/home/peng# passwd csdn

Write picture description here

  • 4) Specify the command interpreter for the user (usually /bin/bash)
root@ubuntu:/home/peng# usermod -s /bin/bash csdn
  •  
  • Mount a disk to the user:
  • #把/dev/sda1这个盘挂载在abc路径下
    sudo mount /dev/sda1 /home/abc

    Add to the docker group:

  • # 把AABBQW这个用户添加到名为docker的组里
    sudo gpasswd -a AABBQW docker
    
    
    (默认名字叫docker,参考https://blog.csdn.net/u011337602/article/details/104541261)
    

     

  •  
  •  
  •  
  • 5) Specify the user home directory for the user
root@ubuntu:/home/peng# usermod -d /home/csdn csdn
  • 6) View the attributes of the user
root@ubuntu:/home/peng# cat /etc/passwd

Write picture description here

As you can see, the user csdn already exists. A row of records in /etc/passwd corresponds to a user, and each row of records is separated into 7 fields by a colon (:). The format and specific meaning are as follows:

Username: Password: User Identification Number: Group Identification Number: Annotative Description: User Home Directory: Command Interpreter

  • 7) Switch to user csdn
root@ubuntu:/home/peng# su csdn

After switching as follows:

Write picture description here

You can see that the current directory of user csdn after login is still "/home/peng", that is, the home directory of user peng.

This method can only switch users in the console. Once the system is restarted, the user still cannot log in (you can only log in with the original user or root).

1.2 Create a new user who can log in to the graphical user interface

  • 1) Switch to root user in order to obtain the permission to create users
peng@ubuntu:~$ sudo su
  • 2) Add a new user (for example, the user name is csdn)
root@ubuntu:/home/peng# adduser csdn

Then follow the system prompts to configure the password and annotation description. The configuration can be successful without entering other commands by yourself throughout the process. The user's home directory and command parsing program are automatically designated by the system.

Write picture description here

  • 3) View the attributes of the user
root@ubuntu:/home/peng# cat /etc/passwd

Write picture description here

As you can see, compared with 1.1, there is more user full name CSDN we just specified for the user.

  • 4) Log out of the current user and log in to the system as user csdn

Write picture description here

You can see that the current directory of the user csdn after logging in is ~, that is, "/home/csdn".

1.3 The difference between the two commands

The biggest difference between the two methods is that the commands for creating a new user are different. The first is useradd, and the second is adduser . Correspondingly, if you want to delete a user, the first command is userdel, and the second command is deluser.

2. Allow the user to execute instructions as an administrator

When we add "sudo" before the instruction to execute some instructions (such as switching to the root user), an error will occur:

csdn is not in the sudoers file. This incident will be reported.

Write picture description here

  • 1) Switch to the root user again (do not use sudo su, but use su root)
csdn@ubuntu:/home/peng$ su root

If "su: Authentication failure" is prompted here, it is because the login password is not set for root, the solution: 
1. First switch back to user peng: su peng 
2. Set the login password for root: sudo passwd root

  • 2) Execute the visudo command
csdn@ubuntu:/home/peng$ visudo
  • 3) This command actually opens the /etc/sudoers file. Modify the file and add a line below the line "root ALL=(ALL:ALL) ALL":

csdn ALL=(ALL:ALL) ALL

ctrl+o (then press enter) to save, ctrl+c to cancel, ctrl+x to exit

Write picture description here

  • 4) Switch back to csdn
root@ubuntu:/home/peng# su csdn
  • 5) Log in to root again with sudo su
csdn@ubuntu:/home/peng$ sudo su

Write picture description here

As you can see, the user csdn can use the sudo command to execute instructions as an administrator.

Guess you like

Origin blog.csdn.net/yxpandjay/article/details/112672427