Linux User Accounts - Notes By ShariqSP
Managing Linux User Accounts
What are Linux User Accounts?
In Linux, user accounts are used to manage access to system resources and provide a way to differentiate between various users. Each user account has its own set of permissions and configurations, allowing for secure and organized system management. User accounts can be categorized into system accounts and regular user accounts. System accounts are used by system services and applications, while regular user accounts are used by individuals.
What are User Groups?
User groups are collections of user accounts that are used to manage permissions and access to resources collectively. Instead of assigning permissions to individual users, you can assign them to a group. This way, any user who is a member of the group automatically inherits the permissions assigned to that group. This is particularly useful for managing file access and administrative privileges.
Each user can be a member of one or more groups, and each group can have multiple users. Groups simplify the management of user permissions and make it easier to apply changes across multiple users at once.
Creating a New User Account
To create a new user account, you use the useradd
command. This command allows you to add a new user with default settings or specify additional options. Here’s how you can create a new user account:
sudo useradd -m username
sudo passwd username
In these examples:
sudo useradd -m username
creates a new user namedusername
and creates a home directory for the user.sudo passwd username
sets or changes the password for the userusername
.
Setting or Changing a User Password
To set or change a user's password, you use the passwd
command. This command allows you to set a new password for an existing user account. Here’s how you can change the password:
sudo passwd username
In this command:
sudo passwd username
prompts you to enter a new password for the userusername
. You will need to enter the password twice to confirm.
Switching to Another User Account
To switch to another user account, you can use the su
or sudo
command. The su
command is used to switch users, while sudo
allows you to execute commands as another user.
su - username
sudo -u username command
In these examples:
su - username
switches the current session to the userusername
. You will be prompted for the user's password.sudo -u username command
executes a specific command as the userusername
. This requires the current user to have the appropriatesudo
permissions.
Deleting a User Account
To delete a user account, you use the userdel
command. This command allows you to remove a user from the system. You can also choose to remove the user's home directory and mail spool.
sudo userdel username
sudo userdel -r username
In these examples:
sudo userdel username
deletes the user accountusername
but leaves the user's home directory and files intact.sudo userdel -r username
deletes the user account and also removes the user’s home directory and mail spool.
Creating a New User Group
To create a new user group, you use the groupadd
command. This command creates a new group in the system:
sudo groupadd groupname
In this command:
sudo groupadd groupname
creates a new group namedgroupname
.
Deleting a User Group
To delete a user group, you use the groupdel
command. This command removes the specified group from the system:
sudo groupdel groupname
In this command:
sudo groupdel groupname
deletes the group namedgroupname
. Note that you cannot delete a group that still has users in it; you must first remove the users or reassign them to another group.
Adding a User to a User Group
To add a user to a user group, you use the usermod
command with the -aG
option. This command adds the specified user to the specified group:
sudo usermod -aG groupname username
In this command:
sudo usermod -aG groupname username
adds the userusername
to the groupgroupname
. The-a
option appends the user to the group without removing them from other groups, and-G
specifies the group.
Conclusion
Managing user accounts and user groups in Linux is essential for maintaining a secure and organized system. By understanding how to create, modify, and delete user accounts and groups, as well as how to manage group memberships, you can effectively control access to system resources and ensure proper security and organization.