The useradd
command in Unix-like operating systems is used to create new user accounts. One of the options available with this command is -u
or --uid
, which allows specifying the User ID (UID) for the new user. The UID is a unique numeric identifier assigned to each user on the system.
Here are some examples of how to use the useradd
command with the -u
option:
Example 1: Creating a user with a specific UID.
useradd -u 2001 johndoe
This command creates a new user account named johndoe
with the UID set to 2001.
Example 2: Creating a user with a UID outside the usual range.
useradd -u 50000 janedoe
In this case, the command creates a user account named janedoe
with a UID of 50000. This can be useful when managing large numbers of users on a system.
Example 3: Specifying a UID that is already in use.
useradd -u 1000 bobsmith
If the UID 1000 is already assigned to another user, this command will fail and display an error message indicating that the UID is not available.
Example 4: Creating a system user with a specific UID.
useradd -r -u 3001 appuser
Here, the -r
option creates a system user, and appuser
is created with the UID 3001. System users typically have UIDs in a different range than regular users.
Example 5: Creating a user with a UID from a reserved range.
useradd -u 0 root2
The UID 0 is reserved for the root user. This command attempts to create a user named root2
with UID 0, but it will likely fail because the UID is already assigned to the root user.
To verify whether the useradd
command executed successfully, you can check if the user account was created with the specified UID. You can use commands like id
or grep
in conjunction with /etc/passwd
to see if the user and UID exist:
id johndoe
grep johndoe /etc/passwd
These commands will display information about the user johndoe
, including the UID assigned. If the user was created successfully with the specified UID, the output will confirm this information.
Discussion about this post