User Management in Linux

Add User
Note:- We can add user Two type
1) By creating user this command it will ask all information as well as it set password for user at the same time and It create home directory for that user.
#sudo adduser %user_name%
Ex.
sudo adduser a
Adding user `a' ...
Adding new group `a' (1001) ...
Adding new user `a' (1001) with group `a' ...
Creating home directory `/home/a' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for a
Enter the new value, or press ENTER for the default
 Full Name []:
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n]
2) Using this command it just create user no home directory, no password.
#sudo useradd %user_name%
Ex.
sudo useradd b

Set or Change Password of any user
#sudo passwd %user_name%
Ex.
#sudo passwd c
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

Give Sudo Permission to any User
Note:- give sudo permission to user by two type
1) By editing sudoers file (for this we can use two type)
- #sudo nano /etc/sudoers
GNU nano 2.5.3                         File: /etc/sudoers

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL
c    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d

- Direct edit sudoers file
#sudo visudo %user_name%

Ex.
#sudo visudo c

2) Directly add user to any or sudo group
#sudo usermod -aG %group_name% %user_name%
Ex.
sudo usermod -aG sudo c

Add User with root permission
If you are really want to create superuser (copy of root but with other password and home directory) and not a sudo user, use UID=0 and GID=0 for new user:
#sudo useradd -ou 0 -g 0 %User_name%
Ex. sudo useradd -ou 0 -g 0 satish
-o allows you to create non-unique UID (root UID=0)
-u $UID sets $UID
-g $GID sets $GID

From <https://superuser.com/questions/196848/how-do-i-create-an-administrator-user-on-ubuntu>

To remove user from any group
Note:- we have two type for doing this task
1) sudo gpasswd -d %User_Name% %Group_Name%
Ex.
sudo gpasswd -d b root

2) sudo deluser %User_Name% %Group_Name%
Ex.
sudo deluser b sudo

To Delete User
- To just delete user without it home directory
#sudo userdel %username%
Ex.
#sudo userdel b
- If you want to remove all of the files of the user then use -r
#sudo userdel -r %username%
To see user group detail
#groups %username%

Ex.
#groups rm
rm : rm adm cdrom sudo dip plugdev lpadmin sambashare

Change Home Directory of Any User
# sudo usermod -d <new-directory-location> <username>
Ex. sudo usermod -d /vns-branch-data/test test

Add User with No Home Directory
# sudo useradd <user-name>

Add User with different group (Primary Group)
# sudo useradd –g <group-name> <user-name>

Add User with different group (Secondary Group)
# sudo useradd –G <group-name> <user-name>

Add User with No Login Permission
# sudo useradd --system --shell /usr/sbin/nologin <user-name>
Ex. sudo useradd --system --shell /usr/sbin/nologin test

Lock User Account with passwd
sudo passwd -l <username>
Eg.
sudo passwd -l test1

Unlock User Account with passwd
sudo passwd -u <username>
Eg.
sudo passwd -u test1

Comments