How to add users in linux
In Linux, you can use the useradd command to add users. The function of this command is to create a new user and add a new system user. The basic syntax format is "useradd [option] username".
#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.
In Linux, you can use the useradd command to create a new user and add a new system user.
The basic format of this command is as follows:
[root@localhost ~]#useradd [选项] 用户名
The commonly used options of this command and their respective meanings are shown in Table 1.
Option | Meaning |
---|---|
-u UID | Manually specify the user's UID, pay attention to the UID range (not less than 500). |
-d Home directory | Manually specify the user's home directory. The home directory must be an absolute path, and if you need to manually specify the home directory, you must pay attention to the permissions; |
-c User instructions | Manually specify the /etc/passwd file The descriptive content of the fifth field in each user information can be configured at will; |
-g Group name | Manually specify the user's initial group. Generally, the group with the same name as the user is used as the user's initial group. When a user is created, the initial group will be established by default. Once specified manually, the system will not create this default initial group directory. |
-G Group name | Specifies the user's additional group. When we add users to other groups, we generally use additional groups; |
-s shell | Manually specify the user's login shell, the default is /bin/bash; |
-e Date | Specifies the expiration date of the user, in the format of "YYYY-MM-DD". That is, the eighth field of the /etc/shadow file; |
-o | allows the created users to have the same UID. For example, execute the "useradd -u 0 -o usertest" command to create user usertest. Its UID is the same as the root user's UID, both are 0; |
-m | When creating a user, it is mandatory to create the user's home directory. When creating a system user, this option is the default; |
-r | Create a system user, that is, the UID is between 1 and 499, for use by system programs. User. Since system users are mainly used to configure permissions for services required to run the system, the creation of system users does not create a home directory by default. |
In fact, the system has provided us with a lot of default values. Without special requirements, users can be successfully created without using any options. For example:
[root@localhost ~]# useradd lamp
This command line means creating a normal user lamp.
Don’t underestimate this simple command, it will complete the following operations:
Create a row of data related to the lamp user in the /etc/passwd file :
[root@localhost ~]# grep "lamp" /etc/passwd lamp:x:500:500::/home/lamp:/bin/bash
You can see that the user's UID is calculated starting from 500. At the same time, the user's home directory is specified as /home/lamp/ by default, and the user's login shell is /bin/bash.
A new line of data related to the lamp user password has been added to the /etc/shadow file:
[root@localhost ~]# grep "lamp" /etc/shadow lamp:!!:15710:0:99999:7:::
Of course, this user has not yet Set the password, so the password field is "!!", which means that the user does not have a reasonable password and cannot log in normally. At the same time, the time field will be set according to the default value. For example, the password is valid for 99999 days, and the system will prompt the user that "the password is about to expire" 7 days before the password expires.
Create a group in the /etc/group file with the same line as the user name:
[root@localhost ~]# grep "lamp" /etc/group lamp:x:500:
This group will be used as the name of the new user Initial group.
Add a new line of password information related to the new group in the /etc/gshadow file:
[root@localhost ~]# grep "lamp" /etc/gshadow lamp:!::
Of course, we did not set The group password is set, so there is no password and no group administrator.
The user's home directory and mailbox are created by default:
[root@localhost ~]#ll -d /home/lamp/ drwx------ 3 lamp lamp 4096 1月6 00:19 /home/lamp/ [root@localhost ~]#ll /var/spod/mail/lamp -rw-rw---- 1 lamp mail 0 1月6 00:19 /var/spool/mail/lamp
Note that the permissions of these two files must be given to the lamp user. permissions.
Copy the configuration files in the
/etc/skel
directory to the new user’s home directory.
As you can see, the process of creating a user with the useradd command actually modifies several files or directories related to the user. These files have been introduced in detail in the previous chapter.
In addition to creating users by default, we can also use various options of the useradd command to customize the users to be created, for example:
[root@localhost ~]# groupadd lamp1 #先手工添加lamp1用户组,因为我一会儿要把lamp1用户的初始迎指定过来,如果不事先建立,则会报告用户组不存在 [root@localhost ~]# useradd -u 550 -g lamp1 -G root -d /home/lamp1 -c "test user" -s /bin/bash lamp1 #在建立用户lamp1的同时,指定了UID(550)、初始组(lamp1)、附加组(root)、家目录(/home/lamp1/)、用户说明(test user)和用户登录Shell(/bin/bash) [root@localhost ~]# grep "lamp1" /etc/passwd /etc/shadow /etc/group #同时查看三个文件 /etc/passwd:lamp1:x:550:502:test user:/home/lamp1:/bin/bash #用户的UID、初始组、用户说明、家目录和登录Shell都和命令手工指定的一致 /etc/shadow:lamp1:!!:15710:0:99999:7::: #lamp1用户还没有设定密码 /etc/group:root:x:0:lamp1 #lamp1用户加入了root组,root组是lamp1用户的附加组 /etc/group:lampl:x:502: #GID为502的组是lamp1组 [root@localhost ~]#ll -d /home/lamp1/ drwx------ 3 lamp1 lamp1 4096 1月6 01:13 /home/lamp1/ #家目录也建立了,不需要手工建立
Using the above 2 methods, users can be successfully created. Often, there is no need to manually specify anything at all, since using the default values will satisfy our requirements. Have you ever thought about where the default values of the useradd command are stored and whether they can be modified manually?
The answer is yes. There are two main default value files that the useradd command refers to when adding users, namely /etc/default/useradd and /etc/login.defs.
Related recommendations: "Linux Video Tutorial"
The above is the detailed content of How to add users in linux. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The methods for basic Linux learning from scratch include: 1. Understand the file system and command line interface, 2. Master basic commands such as ls, cd, mkdir, 3. Learn file operations, such as creating and editing files, 4. Explore advanced usage such as pipelines and grep commands, 5. Master debugging skills and performance optimization, 6. Continuously improve skills through practice and exploration.

The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

The five pillars of the Linux system are: 1. Kernel, 2. System library, 3. Shell, 4. File system, 5. System tools. The kernel manages hardware resources and provides basic services; the system library provides precompiled functions for applications; the shell is the interface for users to interact with the system; the file system organizes and stores data; and system tools are used for system management and maintenance.

How to restart the Redis service in different operating systems: Linux/macOS: Use the systemctl command (systemctl restart redis-server) or the service command (service redis-server restart). Windows: Use the services.msc tool (enter "services.msc" in the Run dialog box and press Enter) and right-click the "Redis" service and select "Restart".

There are three ways to view instance names in Oracle: use the "sqlplus" and "select instance_name from v$instance;" commands on the command line. Use the "show instance_name;" command in SQL*Plus. Check environment variables (ORACLE_SID on Linux) through the operating system's Task Manager, Oracle Enterprise Manager, or through the operating system.

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings

Linux is suitable for servers, development environments, and embedded systems. 1. As a server operating system, Linux is stable and efficient, and is often used to deploy high-concurrency applications. 2. As a development environment, Linux provides efficient command line tools and package management systems to improve development efficiency. 3. In embedded systems, Linux is lightweight and customizable, suitable for environments with limited resources.
