Managing user in Linux
What are users in Linux ?
In Linux, a "user" is just someone who uses the computer. Each user has their own stuff, like files and settings, and they can only access what they're allowed to. It's a way to keep things organized and secure on the system.
To check current user
whoami
The "whoami" command in Linux is used to display the username of the currently logged-in user in the terminal .
To create a new user
The "adduser" command in Linux is used to create a new user account on the system including setting up their home directory and default settings.
sudo adduser [username]
But without setting up, We can do
sudo useradd [username]
The "useradd" command is used to create a new account on the system directly(without setup), allowing for customization of various parameters such as home directory, user ID, and group ID.
To print list of users
We can use the "cat" command to display the contents of the "/etc/passwd" file, which contains information about all user accounts.
cat /etc/passwd
This command will print list of username in the system, with one username per line.
To set password of username
"passwd" command is used to set or reset the password of a username in Linux.
sudo passwd [username]
After running this command, you'll be prompted to enter and confirm the new password for the specified user. Once you've done that, the password will be set for the user.
To modify a user
The "usermod" command in Linux is used to modify the user account proporties.
For help using this command
sudo usermod --help
This command will give you a list of options that you can use to modify the user account.
To switch user
The "su -" command in Linux is used to switch to another user account in a login shell environment.
sudo su - [username]
If you don't enter the username, It will be switched to root user account.
To logout user
The "logout" command in Linux is used to terminate a login session.
logout
To remove user
The "userdel" command in Linux is used to remove/delete a existing user account from the system.
sudo userdel [username]
To change shell of a user
To change the shell of a user in Linux, you can use the "usermod" command with the "-s" option, followed by the path to the new shell and the username.
sudo usermod -s [path/to/new/shell] [username]
For example if we want to change the shell of user "hulk" to "/bin/bash" ;
sudo usermod -s /bin/bash hulk
To change/rename username
To rename a username in Linux, you can use the "usermod" command with the "-l" option to change the login name, followed by the old username and the new username.
sudo usermod -l [new_name] [old_name]
To create group
A group in Linux is a collection of user accounts that share common permissions and access rights to files and resources on the system.
To create a new group in Linux, you can use the "groupadd" command followed by the desired group name.
sudo groupadd [groupname]
To print list of groups
To print a list of groups in Linux, you can use the "cat" command to display the contents of the "/etc/group" file, which contains information about all the groups on the system.
cat /etc/group
To add user to the group
To add a user to a group in Linux, you can use the "usermod" command with the "-aG" option, followed by the group name and the username.
sudo usermod -aG [groupname] [username]
To remove user from the group
To remove a user from a group in Linux, you can use the "gpasswd" command with the "-d" option, followed by the username and the group name.
sudo gpasswd -d [username] [groupname]
To delete a group
The "groupdel" command is used to delete/remove group in Linux.
sudo groupdel [groupname]
To make a user sudoer
A "sudoer" refers to a user who has been granted permission to execute commands with elevated privileges using the "sudo" command in Linux.
The "sudo visudo" command in Linux is used to edit the sudoers file, which determines who is allowed to use the "sudo" command and which commands they are allowed to execute with elevated privileges.
sudo visudo
The command will display a editable file where we can add the user name and make it sudoer.
In this picture , we have added a newuser to user privilege specification with all privilege.
In another way,
We can add the user to the sudo group or we can add the user in another group and add the group in admin group.
Adding user to sudo group
sudo usermod -aG sudo newuser
To add a existing group in admin group we can enter "sudo visudo" and simply edit the file same as the first method.
Subscribe to my newsletter
Read articles from sudip adhikari directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
sudip adhikari
sudip adhikari
I am an explorer of IT field.