Linux all operations are done with users. In Linux environment to create a file, start a service, open a network port we need a user with required privileges. Linux users are stored in plain test database at /etc/passwd . As old times user passwords also stored in this file but on modern time for security reasons user passwords are hold in /etc/shadow . There is also user groups to manage permission better. User groups are stored in plain text database /etc/group . In this tutorial we will look how to add new user to the Linux.
获取有关useradd命令的帮助 (Getting Help About useradd Command)
useradd command is very useful command for system administrators. Knowing its parameters and options are very crucial for daily operations. To get quick help about useradd command issue following command.
We can list newly created user with the following command.
$ cat /etc/passwd | cut -d : -f 1 | grep test
创建具有不同主目录的用户 (Create A User With Different Home Directory)
By default useradd command will create a home directory in /home path with the same name as username. For example if the username is test the home directory of the user will be /home/test
默认情况下， useradd命令将在/ home路径中创建一个与用户名同名的主目录。 例如，如果用户名是test ，则用户的主目录将是/ home / test
$ useradd -d /home/mytest test5
This command will create test user with a home directory like /home/mytest
此命令将使用主目录(例如/ home / mytest)创建测试用户
创建具有特定用户ID的用户(Create A User With Specific User ID)
Linux operating system gives users User ID in order to separate them. User ID’s starts from 500 and increments upon new user addition. But we can change this default behavior by setting specific UID without getting default one. -u will be provided to the useradd command to specify new users UID.
创建具有特定组ID的用户(Create A User With Specific Group ID)
While creating user in Linux operating system new group is created too. This group is dedicated to the new user and the new user is automatically assigned to this user group. The new user group ID is assigned by default and starts from 1000. This value can be changed with -g option.
In previous example we have added new user with the specified group ID. We have just provided only single group ID. We may also need to add user to the multiple groups by providing multiple group ID’s in a single command. This can be done with -G options like below.
While creating new user nick we added nick to the groups named root,vboxusers,kvm with this example.
添加没有主目录的用户 (Add A User Without Home Directory)
Linux users are great way to login and use Linux systems. Being a Linux system user means having a home directory. But this is not always true. Should it be? Should all Linux users have a home directory? No. For example services users those used for Linux daemons and services do not needs home directories. Also there may be some situations we do not need home directories. Another example is using Linux users just for authentication and providing VPN service. With -M options new user will be created without a home directory.
创建一个具有帐户到期日期的用户 (Create A User With Account Expire Date)
In a busy system creating a lot of user without any restriction and future notification will create security problems. Some added users will be permanent for the Linux system but some others will be temporary and should be removed or at least locked after some time. While adding new users we can set account expire date for the new user. Just issue -e option to the useradd command like below. In this example we set 28 February 2017 as account expire time for user nick. The date format is YYYY-MM-DD .
After adding user with a account expire date we can check this date with chage command like below.
$ chage -l nick
创建一个密码过期日期的用户(Create A User With Password Expire Date)
Another way to secure newly created user is specifying password expire date. Specifying password expire date will force the user to change his/her password at the specified date. This will make the user credentials more secure by changing the passwords regularly. To set password expire date use -f option with number of days . If we provide value -1 the password will never expire. We need to provide also a start day for this operation
While adding user we may need to add some extra informal information. This informal information can be the full name, phone number etc. The comment can be added with -c option like below. We will add the phone number of the new user named nick in this example.
Linux users use shell to issue commands from command line interface. There are alternative command line interfaces. Default and most popular command line interface or shell is bash . The newly create user will have bash as default shell. The default shell can be changed while creating user with -s option like below. In this example we will set sh as default shell.
In previous example we have set shell preferences for the newly created user. There is also an option where user will have no shell. This will prevent user to login to the Linux system. This can be used also a security step for the system. We will set the created users default shell /bin/nologin which is not a shell.