Skip to main content

LPI E - useradd cmds

The Linux Accountant

Useradd is a powerful command-line tool that allows system administrators to create new user accounts and set up their user environment. In this blog post, we will also cover some of the other useful commands that system administrators can use to manage user accounts on a Linux system.
 

To create a new user account with a specific username and home directory:

useradd -m -d /home/newuser -s /bin/bash newuser


To create a new user account with a specific UID and GID:

useradd -u 1001 -g 1001 newuser


To create a system user account without a home directory:

useradd -r -s /bin/false daemon


To create a new user account with a specific expiration date:

useradd -e 2023-12-31 newuser


To create a new user account with a specific password:

useradd -m -p $(openssl passwd -1 password) newuser


To create a user with a username that contains characters that are not allowed by default, you can use the --badname option

useradd --badname joe_schmoe!


The following command will create a user with the username joe and the home directory /opt/users/joe

useradd -b /opt/users -d /opt/users/joe -m joe


The following command will create a user with the username joe and the home directory /home/joe, using a BTRFS subvolume for the home directory

useradd --btrfs-subvolume-home joe


The following command will create a user with the username joe and the GECOS field "Joe Schmoe,Developer":

useradd -c "Joe Schmoe,Developer" joe


The following command will print the default configuration for useradd:

useradd -D


The following command will create a user with the username joe and an expiration date of June 1, 2024:

useradd -e 2024-06-01 joe


The following command will create a user with the username joe and a password inactivity period of 30 days:

useradd -f 30 joe


The following command will create a system user with the username joe and add entries to the /etc/subuid and /etc/subgid files:

useradd -r -F -m joe


To create a new user with the username "john" and set the primary group to "staff", you can use the following command:

useradd -g staff john


To create a new user with the username "jane" and add her to the "developers" and "users" groups, you can use the following command:

useradd -G developers,users jane


To create a new user with the username "bob" and use the skeleton directory "/etc/skel2" instead of the default "/etc/skel", you can use the following command:

useradd -k /etc/skel2 bob


To create a new user with the username "alex" and set the default password expiration time to 30 days instead of the default 90 days, you can use the following command:

useradd -K PASS_MAX_DAYS=30 alex


To create a new system account with the username "ftp" and the To create a new user with a specific SELinux user, you can use the following command:UID 1001, you can use the following command:

useradd -r -u 1001 ftp


To create a new user with a custom prefix directory, you can use the following command:

useradd -P /opt/myapp/etc -m myuser


To create a new user with the zsh shell, you can use the following command:

useradd -s /bin/zsh -m myuser


To create a new user with a specific user ID, you can use the following command:

useradd -u 1001 -m myuser


To create a new user with a group of the same name, you can use the following command:

useradd -U -m myuser


To create a new user with a specific SELinux user, you can use the following command:

useradd -Z staff_u -m myuser

 

In conclusion, the "useradd" command is an essential tool for system administrators managing users in a network environment. It enables them to create and manage user accounts efficiently, granting or restricting access to specific system resources based on individual user needs. Additionally, by learning about other related commands like "usermod," "userdel," and "passwd," system administrators can handle critical thinking scenarios for user management and ensure the security and integrity of the network. The ability to effectively manage user accounts is crucial for any system administrator, and mastering these commands can make a significant difference in the smooth functioning of a network environment.