USEFUL USER AND GROUP LINUX COMMANDS FOR SYSADMIN
User & Group related Linux commands
In this tutorial, We will have look at multiple User and Group Linux commands. These commands basically help a newbie to get hands-on experience on user and group Linux commands.
with these commands, you can add, remove, delete users and groups.
To run certain commands you must have root or SUDO level rights on the Linux machine.
For the best practice as security reasons, we must disable the root login on the Linux system and create the user with the SUDO access.
It is very simple commands if you know how to use commands to add users and groups in the Linux system.
We are going to understand the working of below Linux commands,
- Add a user to Linux (useradd)
- Delete a user in Linux (userdel & rmuser)
- Add a new group to Linux (groupadd)
- Add a user to a group in Linux
- Change a password
- Modify a user in a Linux group (usermod & groupmod)
Add a User to Linux
Useradd command will help you to create a new user on the system.
In Linux, useradd command is a basic-level utility that helps to create a user account on Linux systems.
adduser command is symlink of useradd command.
Syntax of Useradd command
$ useradd [options] username
$ useradd linuxgrow
To activate the user in Linux, we need to set password for the created user.
$ passwd linuxgrow
Changing password for user linuxgrow. New UNIX password: Retype new UNIX password: passwd: all authentication tokens updated successfully.
After user creation, entry of the newly added user is created in the “/etc/passwd” file.
Delete a user in Linux (userdel & rmuser)
There are multiple options to delete users from the Linux system.
rmuser is used to delete the user. You need root access to run rmuser command,
$ rmuser linuxgrow
userdel command to delete user
$ userdel linuxgrow
Delete user with default user directory
$ userdel -d /home/linuxgrow linuxgrow
Remove user with all the user files
$ userdel --remove-all-files linuxgrow
Add a new group to Linux (groupadd)
Groupadd command allows you to add or create a group on the system.
To run this command you must have root or SUDO access on the system.
if you try to run it through normal user it will give you permission denied error.
groupadd command adds the entry to the “/etc/group” file. You will get information about all the groups on a system from “/etc/group” file
$ groupadd admin
Add a user to a group in Linux
We can add a user to the group using useradd command with -g parameter.
Run the following command,
$ useradd -c "Linuxgrow" -g admin -m linuxgrow
Now use “id” command to check group of linuxgrow user,
uid=1005(linuxgrow) gid=1006(admin) groups=1006(admin)
Modify a user in a Linux group (usermod & groupmod)
usermod command used to modify or change the settings of existing or already created a user account.
The only root user is able to execute this command. You can find more options for a usermod command using “usermod –help” on the terminal.
When we used usermod command below files gets changed,
- /etc/passwd – User account information.
- /etc/shadow – Secure account information.
- /etc/group – Group account information.
- /etc/gshadow – Secure group account information.
- /etc/login.defs – Shadow password suite configuration.
Change User default directory
$ usermod -d /var/www/ linuxgrow $ grep -E --color '/var/www/' /etc/passwd linuxgrow:500:500:This is Linuxgrow:/var/www:/bin/sh
Set user Account Expiry Date
You can confirm the expiry date with “chage” command.
$ usermod -e 2021-01-01 linuxgrow $ chage -l linuxgrow Last password change : Nov 02, 2019 Password expires : never Password inactive : never Account expires : Jan 01, 2020 Minimum number of days between password change : 0 Maximum number of days between password change : 99999 Number of days of warning before password expires : 7
Change User default group
To change the primary group with the usermod command, we need to use -g parameter.
$ usermod -g newadmin linuxgrow $ id linuxgrow uid=501(linuxgrow) gid=502(newadmin) groups=502(newadmin)
groupmod command used to modify or change the settings of existing groups.
Here we are using groupmod command to add existing users to the group.
This command works similarly to usermod command.
$ groupmod -A Username Groupname
ID command will provide you the no. of active users and their ids.
Also, you will get the user’s login and group information with the help of this command.
uid=0(root) gid=0(root) groups=0(root)
$ id linuxgrow
uid=501(linuxgrow) gid=502(newadmin) groups=502(newadmin)
This is a very important command which will give you information about the last login users’ details on the system.
prashant pts/10 :0 Wed Nov 2 13:40 still logged in
prashant pts/8 :0 Wed Nov 2 13:13 - 19:23 (06:10)
prashant pts/4 :0 Wed Nov 2 13:04 - 18:32 (05:27)
prashant pts/2 :0 Wed Nov 2 12:54 - 13:40 (00:45)
prashant pts/8 :0 Tue Nov 1 17:14 - 17:17 (00:03)
prashant pts/8 :0 Tue Nov 1 15:57 - 16:04 (00:07)
prashant pts/4 :0 Tue Nov 1 12:36 - 19:35 (06:59)
prashant pts/2 :0 Tue Nov 1 12:08 - 19:35 (07:27)
wtmp begins Tue Nov 1 12:08:05 2016
Who command will shows, who is currently logged on the system?
linuxgrow tty8 2020-08-03 12:20 (:0)
linuxgrow pts/10 2020-08-03 13:40 (:0)
This command will provide you the name of currently login user,
I hope you like the article if you find any difficulties using user and group Linux commands then please do comment queries or problem via the comment section, till then stay tuned to Linuxgrow.com for more such valuable articles.