Linux Command - user commands

Commands to create, delete, and modify users.

/etc/passwd file

There are two levels of users root and non-root. In Linux, user info is stored in /etc/passwd file.

sample output for cat /etc/passwd command

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
root:x:0:0:root:/root:/bin/ash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
news:x:9:13:news:/usr/lib/news:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucppublic:/sbin/nologin
operator:x:11:0:operator:/root:/bin/sh
man:x:13:15:man:/usr/man:/sbin/nologin
postmaster:x:14:12:postmaster:/var/spool/mail:/sbin/nologin
cron:x:16:16:cron:/var/spool/cron:/sbin/nologin
ftp:x:21:21::/var/lib/ftp:/sbin/nologin
sshd:x:22:22:sshd:/dev/null:/sbin/nologin
at:x:25:25:at:/var/spool/cron/atjobs:/sbin/nologin
squid:x:31:31:Squid:/var/cache/squid:/sbin/nologin
xfs:x:33:33:X Font Server:/etc/X11/fs:/sbin/nologin
games:x:35:35:games:/usr/games:/sbin/nologin
postgres:x:70:70::/var/lib/postgresql:/bin/sh
cyrus:x:85:12::/usr/cyrus:/sbin/nologin
vpopmail:x:89:89::/var/vpopmail:/sbin/nologin
ntp:x:123:123:NTP:/var/empty:/sbin/nologin
smmsp:x:209:209:smmsp:/var/spool/mqueue:/sbin/nologin
guest:x:405:100:guest:/dev/null:/sbin/nologin
nobody:x:65534:65534:nobody:/:/sbin/nologin

Format of /etc/passwd

  1. User name.
  2. Encrypted password. usually ‘x’ - for security reason, passwords are stored in /etc/shadow file( only users with root permission can visit)
  3. User ID number (UID)
  4. User’s primary group ID number (GID)
  5. Full name of the user (GECOS)
  6. User home directory.
  7. Login shell.

useradd

useradd - create a new user or update default new user information. useradd is a privileged command. You need root access to run this command.

Syntax

1
useradd [options] LOGIN
option description
-d, –home HOME_DIR specify new user’s home directory
-m, –create-home create user’s home directory if it does not exist
-g, –gid GROUP user’s initial login group
-G, –groups GROUPS supplementary groups. separated by comma
-s, –shell SHELL user’s default shell. default is usally /bin/sh, you can set it as /bin/bash if bash is available.
1
useradd -m -G dev,tester -s /bin/bash jimmy

After creating the user, you usually set the password for the newly created user

1
passwd jimmy

userdel

delete a user

1
userdel jimmy

usermod

change user’s group and shell

1
usermod -g root -s bin/sh jimmy

Group operations

To add user jimmy to a group sudo. This will allow jimmy to run privilege commands.

1
gpasswd -a jimmy sudo

Check a user’s group using groups command

1
2
$ groups jimmy
jimmy : jimmy sudo

You can also use id username to check a user’s groups

To remove a user from a group

1
gpasswd -d jimmy sudo

Modify default shell

You can change the default login shell on the system by change /etc/default/useradd file.

1
SHELL=/bin/bash

Reference