Adding a user from the command line.

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,260
Reaction score
3,015
Credits
28,917

Adding a User in Linux​

Adding a user in Linux is a common administrative task. The useradd command is used to create a new user account. Here’s a step-by-step guide on how to use it, along with explanations of various flags and related files.

Basic Command​

To add a new user, you can use the following command:
Code:
sudo useradd username
Replace username with the desired username for the new account.

Common Flags​

  • Comment (-c): This flag allows you to add a comment or description for the user. For example:
    Code:
    sudo useradd -c "John Doe" johndoe
  • Shell (-s): This flag specifies the user's login shell. For example, to set the shell to /bin/bash:
    Code:
    sudo useradd -s /bin/bash johndoe
  • Home Directory (-d): This flag sets the user's home directory. By default, the home directory is created under /home/username. To specify a different directory:
    Code:
    sudo useradd -d /custom/home/directory johndoe
  • Create Home Directory (-m): This flag ensures that the home directory is created if it does not exist:
    Code:
    sudo useradd -m johndoe

Using /etc/skel​

The /etc/skel directory contains default files and directories that are copied to a new user's home directory when the account is created. This can include configuration files, directories, and other initial setup files.
For example, if /etc/skel contains a .bashrc file, this file will be copied to the new user's home directory when the account is created.

Setting or Changing the Password​

To set or change the password for a user, you can use the passwd command:
Code:
sudo passwd johndoe
You will be prompted to enter and confirm the new password.

Understanding UID and GID​

  • UID (User ID): This is a unique identifier assigned to each user on the system. It is used by the system to identify the user and manage permissions. UIDs are typically assigned sequentially starting from a predefined number (e.g., 1000 for regular users).
  • GID (Group ID): This is a unique identifier assigned to each group on the system. It is used to manage group permissions. Each user is assigned a primary GID, which corresponds to their primary group.

Understanding /etc/passwd and /etc/shadow​

  • /etc/passwd: This file contains user account information. Each line represents a user and includes fields such as username, user ID (UID), group ID (GID), home directory, and login shell. Passwords are not stored here for security reasons.
  • /etc/shadow: This file contains encrypted password information and additional account security details. Only root and privileged users can read this file. Each line corresponds to a user and includes fields such as the username, encrypted password, and password expiration information.

Sample useradd Command with All Flags​

Here’s an example of a useradd command using all the flags on the same line:
Code:
sudo useradd -c "John Doe" -s /bin/bash -d /custom/home/directory -m johndoe
 


Modifying an Existing User in Linux​

The usermod command is used to modify an existing user account in Linux. This command allows you to change various parameters of a user, such as their comment, shell, or home directory. Here’s a step-by-step guide on how to use it.

Changing the Comment​

To change the comment or description of an existing user, you can use the -c flag:

Code:
sudo usermod -c "New Comment" username

Replace New Comment with the desired comment and username with the existing username.

Changing the Shell​

To change the login shell of an existing user, you can use the -s flag:

Code:
sudo usermod -s /new/shell username

Replace /new/shell with the desired shell (e.g., /bin/bash) and username with the existing username.

Changing the Home Directory​

To change the home directory of an existing user, you can use the -d flag. Additionally, you can use the -m flag to move the contents of the current home directory to the new one:

Code:
sudo usermod -d /new/home/directory -m username

Replace /new/home/directory with the desired home directory path and username with the existing username.
 
Admins are supposed to use adduser which will call useradd.

Signed,

Matthew Campbell
 
Both useradd and adduser commands are used to create new user accounts, but they have some key differences:

useradd:
  • Low-level command: useradd is a basic, low-level command available on all Linux distributions.
  • Manual setup: It requires additional options to fully set up a user account. For example, it doesn't automatically create a home directory or set a password.
  • Flexibility: Offers more control and customization over the user creation process, but can be more complex to use

adduser:
  • High-level command: adduser is a higher-level command that acts as a front-end to useradd.
  • Interactive: It provides an interactive prompt that guides you through the user creation process, making it easier to use.
  • Automatic setup: Automatically creates a home directory, sets the default shell to Bash, and prompts for a password
  • Availability: Not available on all Linux distributions, as it is essentially a Perl script that uses useradd in the background
In summary, useradd is more flexible but requires more manual configuration, while adduser is more user-friendly and automates many of the steps involved in creating a new user.
 
Last edited:
... so many sudo's !

The only time I use SUDO, is this:

SUDO SU -

And then, all works ...
But seriously, USERADD is a root file, so it's typical for ROOT to perform, and ROOT never needs SUDO for anything.
 
Last edited:
... so many sudo's !

The only time I use SUDO, is this:

SUDO SU -

And then, all works ...
But seriously, USERADD is a root file, so it's typical for ROOT to perform, and ROOT never needs SUDO for anything.
Is your su only available to root? Did you forget the root password? If you still remember the root password and su is available to your user then you don't need sudo su, but simply su.

Signed,

Matthew Campbell
 
Is your su only available to root? Did you forget the root password? If you still remember the root password and su is available to your user then you don't need sudo su, but simply su.

Signed,

Matthew Campbell

Well, maybe direct login as ROOT is prohibited
 

Members online


Top