Linux Usermod
Linux Usermod
User data is stored in the /etc/passwd file. Each line represents a different user on the
system.
Each line in /etc/passwd is in the format:
username:password:uid:gid:comment:home:shell
The password field should be "x", indicating that the user's encrypted password data is
stored in the more secure /etc/shadow file.
The uid and gid fields are the user ID and main group ID for the user. These are unique
numbers that correspond to the user name and group name, respectively.
The comment field is a text field the system won't process. It's usually used to store a user's
name or the name of the application that account is associated with.
The home field lists the home directory of the user, like /home/username.
The shell is the command run when the user starts a login session. An account created for a
human would use an interactive shell like /bin/bash. A non-interactive user (like an
bin/false or
account created to run a service) should use a shell that does nothing, like /
/sbin/nologin, to prevent anyone from logging in as that user.
Managing Users
These commands create, delete, or modify user accounts.
Command Description
useradd Add a user to the system.
userdel Delete a user from the system, optionally deleting their home directory as
well.
usermod Change a user's information, add it to secondary groups, or perform tasks
like moving its home directory or locking the account.
passwd Change or expire a user's password.
chsh sermod).
Change the shell for a user (also possible via u
Command Examples
Create a new user and its home directory
The -m option to useradd will tell the system to create a home directory in the default
system location. The -c option sets the user's comment field - if there's going to be a space
in the comment, put quotes around it.
useradd -m -c 'Full Name' username
LinuxTrainingAcademy.com
Add a user to a group
Using the -G option to add a user to a group via usermod will replace its current secondary
groups. The -a option tells the system to append the group to the user's secondary group
list instead.
usermod -a -G groupname username
User Switching
These commands switch the active shell to another user or identify the current user.
Command Description
su Switch to another user account. With no argument, switches to root.
sudo Execute a command with superuser privileges.
visudo udo. This command checks the changes made
Edit the configuration file for s
to make sure they are valid before saving.
whoami Display the username of the current account.
id Display the numeric ID or group information for a user.
Command Examples
Switch to another user in a login shell
Using a dash as an option to su will switch to a shell with environment variables set as if
the user had logged in. Useful if the account is configured to work with a specific
application.
su - username
LinuxTrainingAcademy.com
Use sudo with a pipe
To use a pipe with sudo, run su with the -c option, then put the command sequence you
want to run in quotes.
sudo su -c 'ls | sort > /root/files.txt'
Managing Groups
These commands create or delete groups or modify their data.
Command Description
groupadd Add a new group to the system.
groupdel Remove a group from the system.
groupmod Change information about a group (usually its name or numeric ID).
groups Display the groups a user belongs to.
Command Example
Change a group name
Change a group name with the -n option of the groupmod command.
groupmod -n newname oldname
LinuxTrainingAcademy.com