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 | root:x:0:0:root:/root:/bin/ash |
Format of /etc/passwd
- User name.
- Encrypted password. usually ‘x’ - for security reason, passwords are stored in /etc/shadow file( only users with root permission can visit)
- User ID number (UID)
- User’s primary group ID number (GID)
- Full name of the user (GECOS)
- User home directory.
- 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 | $ groups jimmy |
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