Linux User Management
Creating Users in Linux
Linux allows system administrators to create and manage users with various commands. Below are the key commands used for user management.
1. useradd - Creating a New User
The useradd command is used to create a new user account in Linux.
Basic Syntax:
sudo useradd <username>Explanation:
sudo– Runs the command with administrator privileges.useradd– The command used to add a new user.<username>– Replace with the actual username you want to create.
Example:
sudo useradd yatharthThis will create a new user named yatharth.
2. Setting a Password with passwd
After creating a user, you need to set a password using the passwd command.
Syntax:
sudo passwd <username>Example:
sudo passwd yatharthAfter running this command, the system will prompt you to enter and confirm the new password.
3. id - Display User ID and Group ID
The id command shows the user ID (UID), group ID (GID), and other associated group IDs.
Example:
id yatharth4. whoami - Display the Current Logged-in User
whoami5. groups - Show Groups of a User
groups yatharth6. usermod - Modifying User Accounts
The usermod command allows you to modify user properties like home directory, shell, and group.
Linux User Management
Creating Users in Linux
Linux allows system administrators to create and manage users with various commands. Below are the key commands used for user management.
1. useradd - Creating a New User
The useradd command is used to create a new user account in Linux.
Basic Syntax:
sudo useradd <username>Explanation:
sudo– Runs the command with administrator privileges.useradd– The command used to add a new user.<username>– Replace with the actual username you want to create.
Example:
sudo useradd yatharthThis will create a new user named yatharth.
2. Setting a Password with passwd
After creating a user, you need to set a password using the passwd command.
Syntax:
sudo passwd <username>Example:
sudo passwd yatharthAfter running this command, the system will prompt you to enter and confirm the new password.
3. id - Display User ID and Group ID
The id command shows the user ID (UID), group ID (GID), and other associated group IDs.
Example:
id yatharth4. whoami - Display the Current Logged-in User
whoami5. groups - Show Groups of a User
groups yatharth6. usermod - Modifying User Accounts
The usermod command allows you to modify user properties like home directory, shell, and group.
Change Home Directory:
sudo usermod -d /home/customhome yatharthThis sets /home/customhome as the home directory for yatharth.
Change Username:
sudo usermod -l newyatharth yatharthThis renames yatharth to newyatharth.
Add User to a Group:
sudo usermod -aG sudo yatharthThis adds yatharth to the sudo group (granting administrative privileges).
Assigning Default Directories and User Permissions
1. Default Home Directory
By default, new users get a home directory under /home/<username>. To specify a custom home directory:
sudo useradd -m -d /custom/home yatharthThe -m flag ensures that the directory is created if it doesn’t exist.
2. Setting User Permissions
Linux assigns default file and directory permissions using the chmod and chown commands.
Change File Ownership:
sudo chown yatharth:yatharth /home/yatharthThis assigns ownership of /home/yatharth to the user yatharth.
Change File Permissions:
sudo chmod 700 /home/yatharthThis ensures only yatharth has access to their home directory.
Managing Groups in Linux
1. Show All Groups
getent groupOR
cat /etc/group2. Show Groups of a Specific User
groups yatharthOR
id yatharth3. Create a New Group
sudo groupadd developers4. Create a Group with a Specific GID
sudo groupadd -g 1050 testers5. Add a User to a Group
sudo usermod -aG developers yatharth6. Remove a User from a Group
sudo gpasswd -d yatharth developers7. Delete a Group
sudo groupdel testers8. Change a User’s Primary Group
sudo usermod -g developers yatharthDeleting Users in Linux
To remove users, use the userdel command.
1. Basic User Deletion
sudo userdel yatharthThis deletes the user yatharth but keeps their home directory and files.
2. Delete User and Home Directory
sudo userdel -r yatharthThe -r flag removes the home directory and all user-related files.
3. Remove a User from a Group
sudo gpasswd -d yatharth sudoThis removes yatharth from the sudo group.
Changing Ownership in Linux
Ownership of files and directories can be changed using chown.
1. Change File Ownership
sudo chown yatharth /var/www/html/index.htmlThis makes yatharth the owner of index.html.
2. Change Group Ownership
sudo chown :developers /var/www/html/index.htmlThis changes the group ownership to developers.
3. Change Both Owner and Group
sudo chown yatharth:developers /var/www/html/index.htmlThis assigns yatharth as the owner and developers as the group.
4. View Ownership and Permissions
ls -l /var/www/html/index.htmlThis displays file ownership and permission details.
Change Home Directory:
sudo usermod -d /home/customhome yatharthThis sets /home/customhome as the home directory for yatharth.
Change Username:
sudo usermod -l newyatharth yatharthThis renames yatharth to newyatharth.
Add User to a Group:
sudo usermod -aG sudo yatharthThis adds yatharth to the sudo group (granting administrative privileges).
Assigning Default Directories and User Permissions
1. Default Home Directory
By default, new users get a home directory under /home/<username>. To specify a custom home directory:
sudo useradd -m -d /custom/home yatharthThe -m flag ensures that the directory is created if it doesn’t exist.
2. Setting User Permissions
Linux assigns default file and directory permissions using the chmod and chown commands.
Change File Ownership:
sudo chown yatharth:yatharth /home/yatharthThis assigns ownership of /home/yatharth to the user yatharth.
Change File Permissions:
sudo chmod 700 /home/yatharthThis ensures only yatharth has access to their home directory.
Deleting Users in Linux
To remove users, use the userdel command.
1. Basic User Deletion
sudo userdel yatharthThis deletes the user yatharth but keeps their home directory and files.
2. Delete User and Home Directory
sudo userdel -r yatharthThe -r flag removes the home directory and all user-related files.
3. Remove a User from a Group
sudo gpasswd -d yatharth sudoThis removes yatharth from the sudo group.
Changing Ownership in Linux
Ownership of files and directories can be changed using chown.
1. Change File Ownership
sudo chown yatharth /var/www/html/index.htmlThis makes yatharth the owner of index.html.
2. Change Group Ownership
sudo chown :developers /var/www/html/index.htmlThis changes the group ownership to developers.
3. Change Both Owner and Group
sudo chown yatharth:developers /var/www/html/index.htmlThis assigns yatharth as the owner and developers as the group.
4. View Ownership and Permissions
ls -l /var/www/html/index.htmlThis displays file ownership and permission details.