Who is who
id
Your UID, GID and groups
id <user>
Same for another user
id -nG <user>
Group names only
groups <user>
List a user's groups
Shows the groups in the files, not those of your running
session.
whoami
Print your effective user name
getent passwd <user>
Look up an account entry
Uses NSS, so it also finds LDAP/SSSD users.
getent group <group>
Show a group and its members
Lists only supplementary members, not users with it as primary
group.
getent passwd | cut -d: -f1
List all account names
w
Logged in users and what they run
last -n 10
Recent logins
From wtmp. Some newer distros ship it as wtmpdb last.
lslogins -u
Table of user accounts
Create users
useradd -m <user>
Create a user with a home dir
Without -m many distros create no home. Set a password next.
useradd -m -s /bin/bash <user>
Set the login shell too
Debian's default shell for useradd is /bin/sh.
useradd -m -G <g1>,<g2> <user>
Add supplementary groups
useradd -m -c '<full name>'
<user>
Set the comment (GECOS) field
useradd -m -u <uid> <user>
Pick a fixed UID
useradd -g <group> <user>
Set the primary group
useradd -r -s /usr/sbin/nologin <user>
Create a system user for a service
UID from the system range; no home unless you add -m.
useradd -m -d <dir> <user>
Use a non-default home
useradd -m -e <YYYY-MM-DD> <user>
Account expires on a date
useradd -m -k <dir> <user>
Copy a different skeleton dir
useradd -D
Show the defaults useradd uses
Modify users
usermod -aG <group> <user>
Add to a group, keep the rest
usermod -G <g1>,<g2> <user>
Replace all supplementary groups
Drops every group not listed.
gpasswd -d <user> <group>
Remove a user from one group
usermod -s <shell> <user>
Change the login shell
usermod -L <user>
Lock the password
SSH keys still work. Add -e 1 to block all logins.
usermod -U <user>
Unlock the password
usermod -e <YYYY-MM-DD> <user>
Set the account expiry date
-e '' removes it again.
usermod -l <new> <old>
Rename the login
Home dir and group keep their old names.
usermod -d <dir> -m <user>
Move the home directory
usermod -c '<full name>' <user>
Change the comment field
usermod -u <uid> <user>
Change the UID
Files in the home are re-owned; files elsewhere are not.
usermod -g <group> <user>
Change the primary group
Groups
groupadd <group>
Create a group
groupadd -g <gid> <group>
Create a group with a fixed GID
groupadd -r <group>
Create a system group
gpasswd -a <user> <group>
Add a user to a group
gpasswd -M <u1>,<u2> <group>
Set the full member list
gpasswd -A <user> <group>
Make a user group admin
groupmod -n <new> <old>
Rename a group
groupmod -g <gid> <group>
Change a group's GID
groupdel <group>
Delete a group
Fails while it is someone's primary group.
newgrp <group>
Start a shell with a new group
Picks up a fresh membership without logging out.
sg <group> -c '<cmd>'
Run one command as a group
Passwords and aging
passwd
Change your own password
passwd <user>
Set another user's password
passwd -S <user>
Password status: P, L or NP
P usable, L locked, NP none; then last change and ages.
passwd -l <user>
Lock the password
passwd -u <user>
Unlock the password
passwd -e <user>
Force a change at next login
passwd -d <user>
Delete the password
Allows login without a password where PAM permits it.
chage -l <user>
Show aging and expiry info
chage -M <days> <user>
Max days before a change
chage -m <days> <user>
Min days between changes
chage -W <days> <user>
Days of warning before expiry
chage -E <YYYY-MM-DD> <user>
Expire the account on a date
chage -E -1 <user>
Remove the account expiry
chage -d 0 <user>
Force a change at next login
echo '<user>:<pass>' | chpasswd
Set passwords in batch
Reads user:password lines from stdin.
Lock or expire?
| Command |
Blocks |
passwd -l |
password logins |
usermod -L |
password logins |
usermod -e 1 |
all logins, incl. SSH keys |
passwd -e |
nothing; forces a new password |
Files
/etc/passwd fields
| # |
Field |
1 |
login name |
2 |
x = hash in shadow |
3 |
UID |
4 |
primary GID |
5 |
comment (GECOS) |
6 |
home directory |
7 |
login shell |
/etc/shadow fields
| # |
Field |
1 |
login name |
2 |
hash; ! or * = locked |
3 |
last change (days) |
4-5 |
min / max age |
6 |
warn days |
7 |
inactive days |
8 |
account expiry (days) |
/etc/group
name:x:GID:member,member
Members are supplementary only; primary groups live in passwd.
/etc/skel/
Template copied into new homes
/etc/login.defs
UID ranges, aging and home defaults
/etc/default/useradd
Default shell, home base, skel
vipw
Edit /etc/passwd safely, with locking
vipw -s edits shadow; vigr and vigr -s do the group files.
pwck -r
Check passwd/shadow for errors
grpck -r
Check group/gshadow for errors