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
- 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#Creating a new user - The main method I use!!!
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
- groupmod: Non-interactive
Other group-related commands
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.
Some linux distributions, such as ubuntu, use a group name identical to the username. Change this group name too:
Finally, also change the home directory path that matches the username.
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
- KLDP: 6.6. Linux password & shadow file formats
- Description of Linux /etc/passwd and /etc/shadow fields
Shadow
root:$6$5H0QpwprRiJQR19Y$bXGOh7dIfOWpUb/Tuqr7yQVCqL3UkrJns9.7msfvMg4ZO/PsFC5Tbt32PXAw9qRFEBs1254aLimFeNM8YsYOv. : 16431 : 0 : 99999 : 7 : : :
|-1–|———————————–2——————————————————————————————————————————————-|—3—|-4-|—5–|-6-|-7-|-8-|
- Login Name : User account. The fields are separated by : (colon), and each field between colons is structured as follows.
- Encrypted : The encrypted password value
- Last Changed : The date the password was last changed, counted in days since January 1, 1970
- Minimum : Minimum number of days the password must be used before it can be changed
- Maximum : Maximum number of days the password can be used before it must be changed
- Warn : Number of days before password expiration to show a warning message
- Inactive : Number of days until login access is blocked
- Expire : Number of days login use is prohibited (month/day/year)
- 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: 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
- Username
- Password (encrypted in the /etc/shadow file)
- User account uid
- User account gid
- User account name (information)
- User account home directory
- User account login shell
Group
See also
- Linux:AdminSetting: Linux administrator settings.
- InformationSecurity:CheckList:Unix - Security checklist for Unix-like systems.
Favorite site
- Linux user management
- Creating/deleting/checking/adding groups
- Creating a Sudo user on Ubuntu Linux (administrator privileges)
- Viewing the list of Linux accounts
- Managing the Linux user group files /etc/group and /etc/gshadow
Pages that link here