[Shell command collection system management] Linux adds a new user account adduser command usage guide


Shell command column: Full analysis of Linux Shell commands


describe


The adduser command is used to add a new user account in the Linux system. Its functions include the following aspects:

  1. Create a user account: The adduser command can create a new user account and assign a unique user ID (UID) to the user. Each user account has a unique username and corresponding password.

  2. Assign user home directory: When creating a user account, the adduser command will automatically assign a user home directory to the user. The user's home directory is the user's default working directory after logging into the system. Users can store their own files and configuration information in this directory.

  3. Set the user login shell: The adduser command can specify the shell used when the user logs in. Shell is the interface for users to interact with the operating system. Different Shells provide different functions and commands.

  4. Create user groups: When creating a user account, the adduser command can choose to add the user to one or more user groups. A user group is a collection of users with the same permissions and access rights.

  5. Set user password: The adduser command can set the user's login password. A password is the credential used to authenticate a user when logging into the system.

  6. Assign user permissions: The adduser command can set different permissions and access controls for users. By adding users to different user groups or setting specific permissions, you can restrict users' access to and operations on system resources.

All in all, the adduser command is a tool for creating new user accounts in Linux systems. It can assign unique user IDs to users, assign user home directories, set up login shells, create user groups, set passwords and assign permissions, etc. These features allow system administrators to easily manage and control user accounts.


Syntax format

adduser [选项] 用户名

Parameter Description

  • -c, --comment COMMENT: Add a note for the new user, which can be the user's full name or other relevant information.
  • -d, --home HOME_DIR: Specify the new user's home directory path.
  • -g, --gid GROUP: Specify the initial user group to which the new user belongs.
  • -s, --shell SHELL: Specify the login shell of the new user.
  • -p, --password PASSWORD:Set a password for the new user.
  • -m, --create-home: If the home directory does not exist, create the new user's home directory.
  • -k, --skel SKEL_DIR: Specify the skeleton directory, used to initialize the new user's home directory.
  • -r, --system: Create a system user.
  • -u, --uid UID:Specify the user ID of the new user.
  • -e, --expiredate EXPIRE_DATE:Set the account expiration date for new users.

error condition

  • If you do not have sufficient permissions to execute the adduser command, the error message "adduser: Only root may add a user or group to the system." will be displayed.
  • If the specified username already exists, an error message "adduser: The user 'username' already exists." will be displayed.
  • If the specified home directory already exists, an error message "adduser: The home directory '/home/username' already exists." will be displayed.

Precautions

There are some things to note when using the Linux Shell’s adduser command:

  1. Requires root permissions: The adduser command requires root permissions to execute because creating a user account requires modifications to the system. Therefore, before using the adduser command, make sure you are logged in as the root user or have sudo permissions.

  2. Avoid duplicate usernames: When creating new users, make sure the username is unique and avoids conflicts with existing user accounts. You can use grepthe command or view /etc/passwdthe file to check if the same username already exists.

  3. Set password security: When setting passwords for new users, you should pay attention to password security. It is recommended to use a strong password that includes a combination of uppercase and lowercase letters, numbers, and special characters, and must be no less than 8 characters in length.

  4. Specify a home directory and login shell: If you need to specify a specific home directory and login shell for the new user, make sure the paths to the directory and shell are correct. The home directory should have the appropriate permissions and the shell should be a valid shell that is installed.

  5. User group management: When creating a user, you can choose to add it to one or more user groups. Make sure the user group already exists and has appropriate permissions and access controls.

  6. Usage of skeleton directory: By using -kthe option, you can specify a skeleton directory to initialize the new user's home directory. Make sure the skeleton directory contains the required files and configuration for new users to work properly.

  7. Check error information: When executing the adduser command, pay attention to check the error information output. If any error messages appear, they should be read carefully and resolved to ensure that the user account is created correctly.

In short, when using the adduser command, ensure that you have sufficient permissions, avoid duplicate user names, set secure passwords, correctly specify the home directory and login shell, manage user groups, use skeleton directories, and check and resolve error messages in a timely manner to ensure Ensure smooth creation and management of user accounts.


underlying implementation

In Linux systems, the underlying implementation of the adduser command is completed by calling a series of system calls and tools. The following are the rough implementation steps:

  1. First, the adduser command checks whether the current user has sufficient permissions to execute the command. Generally, only the root user or a user with sudo permissions can execute the adduser command.

  2. Next, the adduser command calls the useradd command to create a new user account. The useradd command is a low-level tool used to create and modify user account related information.

  3. The useradd command generates a unique user ID (UID) and an initial group ID (GID) and saves this information in the /etc/passwd file. At the same time, the useradd command creates a default home directory if it does not exist and sets its ownership to the new user.

  4. The adduser command also calls the groupadd command to create new user groups (if needed). The groupadd command is used to create and modify user group related information, and save this information in the /etc/group file.

  5. If necessary, the adduser command will call the usermod command to modify some attributes of the new user, such as setting the login shell, home directory, user group, etc.

  6. Finally, the adduser command calls the passwd command to set the new user's password. The passwd command is used to modify the user's password and save the password in the /etc/shadow file.

In general, the underlying implementation of the adduser command is to complete the creation and management of user accounts by calling useradd, groupadd, usermod, passwd and other related tools and system calls. By combining these tools and system calls, the adduser command can easily complete operations such as creating user accounts, setting attributes and passwords.


Example

Example 1

Create a user account named "john" and add it to the "developers" user group.

Example 2

Create a user account named "mary" and specify the login shell as /bin/bash.

Example three

Create a user account named "guest" and set its home directory to /home/guest.

Example 4

Create a user account named "testuser" and set its password.

Example five

Create a user group named "sales" and add users "jane" and "peter" to the user group.

Example 6

Create a user account named "admin" and add it to the sudoers file to gain administrator rights.

Example 7

Create a user account named "ftpuser" and set its home directory to /var/ftp. At the same time, specify its login shell as /bin/false to limit its permission to log in to the system.


Implemented in c language


The following is a simple example of implementing the adduser command in C language, with comments explaining the role of each step:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[]) {
    
    
    // 检查参数数量
    if (argc != 2) {
    
    
        printf("Usage: %s username\n", argv[0]);
        return 1;
    }

    // 创建用户账户
    if (system("useradd -m -s /bin/bash -U -p '' -d /home/%s %s", argv[1], argv[1]) == -1) {
    
    
        printf("Failed to create user account.\n");
        return 1;
    }

    // 设置用户密码
    char cmd[256];
    sprintf(cmd, "echo %s:%s | chpasswd", argv[1], argv[1]);
    if (system(cmd) == -1) {
    
    
        printf("Failed to set user password.\n");
        return 1;
    }

    // 设置用户权限
    char homeDir[256];
    sprintf(homeDir, "/home/%s", argv[1]);
    if (chown(homeDir, getuid(), getgid()) == -1) {
    
    
        printf("Failed to set user permissions.\n");
        return 1;
    }

    printf("User account created successfully.\n");

    return 0;
}

This example uses C language system calls and library functions to implement the functions of the adduser command. In the main function, first check the number of parameters to ensure that only one username is passed to the program as a parameter.

Then, use systemthe function call useraddcommand to create the user account. Among them, -mthe option is used to create the user's home directory, -sthe option specifies the login shell /bin/bash, -Uthe option creates a user group with the same user name, -pthe option sets the password to an empty string, and -dthe option specifies the home directory path.

Next, set the user's password using sprintfa function build chpasswdcommand that pipes the username and password to the command chpasswd.

最后,使用chown函数设置新用户主目录的所有者为当前用户的UID和GID,以确保用户具有适当的权限。

这个示例只是一个简单的实现,实际上,adduser命令的实现要复杂得多,涉及更多的系统调用和错误处理。此示例仅用于演示基本的实现思路。


结语

在我们的探索过程中,我们已经深入了解了Shell命令的强大功能和广泛应用。然而,学习这些技术只是开始。真正的力量来自于你如何将它们融入到你的日常工作中,以提高效率和生产力。

心理学告诉我们,学习是一个持续且积极参与的过程。所以,我鼓励你不仅要阅读和理解这些命令,还要动手实践它们。尝试创建自己的命令,逐步掌握Shell编程,使其成为你日常工作的一部分。

同时,请记住分享是学习过程中非常重要的一环。如果你发现本博客对你有帮助,请不吝点赞并留下评论。分享你自己在使用Shell命令时遇到的问题或者有趣的经验,可以帮助更多人从中学习。
此外,我也欢迎你收藏本博客,并随时回来查阅。因为复习和反复实践也是巩固知识、提高技能的关键。

最后,请记住:每个人都可以通过持续学习和实践成为Shell编程专家。我期待看到你在这个旅途中取得更大进步!


阅读我的CSDN主页,解锁更多精彩内容:泡沫的CSDN主页

在这里插入图片描述

Guess you like

Origin blog.csdn.net/qq_21438461/article/details/131427436