Skip to content
Search

Linux:User

lastmod 2025-12-04created 2015-01-1629 edits

Notes on how to manage users in Linux.

Communication between users

Users can communicate with each other using the following commands.

  • write: Sends a message to another logged-in user.
  • wall
  • mesg
  • mail
  • biff
  • who - Information about currently logged-in users.
  • w - Detailed information about currently logged-in users.

Creating users

  • adduser: Handles the user's home directory and user group password interactively. (Creates default account information along with it)
  • useradd: The user environment must be specified with options when running the command. (Only creates the account; other information must be created and configured manually)

Creating user passwords

  • passwd: Manages your own password.

Deleting users

  • deluser: Deletes interactively
  • userdel: Must be handled with options when running the command

Changing user information

  • chfn: Changes user information interactively
  • chsh: Changes the user's shell interactively
  • usermod: Non-interactive changes

Checking users/groups

  • cat /etc/passwd: Shows the list of users.
  • cat /etc/group:Shows the list of groups.

Modifying a user's groups

  • gpasswd -a [USERNAME] [GROUPNAME]: Adds a specific user to a group.
  • gpasswd -d [USERNAME] [GROUPNAME]: Removes a specific user from a group.

Creating groups

  • addgroup: Creates interactively
  • groupadd: Non-interactive, works with various options

Changing groups

Deleting groups

  • delgroup: Deletes a group interactively
  • groupdel: Deletes a group non-interactively

How to change a username

To change a username, you must terminate all processes running as that user. Then log in as an administrator.

sudo usermod -l newUsername oldUsername

Some linux distributions, such as ubuntu, use a group name identical to the username. Change this group name too:

sudo groupmod -n newUsername oldUsername

Finally, also change the home directory path that matches the username.

sudo usermod -d /home/newHomeDir -m newUsername

Example Script

Checks whether a user exists and adds it if not.

function install_user
{
    local username=$1
    local homedir=$2

    local exists_user
    exists_user=$(awk -F: '{print $1}' /etc/passwd | grep "$username")

    if [[ -n $exists_user ]]; then
        echo "Exists '$username' user"
    else
        echo "Create '$username' user"
        useradd -l -M -N -d "$homedir" -s /bin/bash "$username"
    fi
}

Checks whether a group exists and adds it if not.

function install_group
{
    local username=$1
    local groupname=$1

    local exists_group
    exists_group=$(awk -F: '{print $1}' /etc/group | grep "$groupname")

    if [[ -n $exists_group ]]; then
        echo "Exists '$groupname' group"
    else
        echo "Create '$groupname' group"
        groupadd "$groupname"
        gpasswd -a "$username" "$groupname"
        usermod -aG docker "$username"
    fi
}

Linux password & shadow file formats

Shadow

root:$6$5H0QpwprRiJQR19Y$bXGOh7dIfOWpUb/Tuqr7yQVCqL3UkrJns9.7msfvMg4ZO/PsFC5Tbt32PXAw9qRFEBs1254aLimFeNM8YsYOv. : 16431 : 0 : 99999 : 7 : : :
|-1–|———————————–2——————————————————————————————————————————————-|—3—|-4-|—5–|-6-|-7-|-8-|
  1. Login Name : User account. The fields are separated by : (colon), and each field between colons is structured as follows.
  2. Encrypted : The encrypted password value
  3. Last Changed : The date the password was last changed, counted in days since January 1, 1970
  4. Minimum : Minimum number of days the password must be used before it can be changed
  5. Maximum : Maximum number of days the password can be used before it must be changed
  6. Warn : Number of days before password expiration to show a warning message
  7. Inactive : Number of days until login access is blocked
  8. Expire : Number of days login use is prohibited (month/day/year)
  9. Reserved : Not used

If you look closely at the front part of field 2, it is separated by $. These separators hold the following values.

[$Hashid $Salt $Hash vlaue]
  • Hashid: Shows which scheme was used for hashing. The id value differs depending on the scheme used, and the most commonly used ones are $1, $5, and $6.
  • Salt: A hash is a one-way function. In other words, A->B is possible but B->A is not.
  • Hash value: The hash value is the result of running the hash function with the hashing method given by the hashid and the salt value.

Passwd

root:x:0:0:root:/root:/bin/bash
  1. Username
  2. Password (encrypted in the /etc/shadow file)
  3. User account uid
  4. User account gid
  5. User account name (information)
  6. User account home directory
  7. User account login shell

Group

See also

Favorite site

author Zer0 sitemap