Linux is a multi-user operating system, and user management is a critical aspect of system administration. It involves creating, modifying, and removing users, as well as managing groups, permissions, and access. In this article, we’ll go over several key Linux user management operations, providing examples along the way.
1. Creating a Group
In Linux, groups are used to organize users. You can create a new group with the groupadd
command. Here’s how you can create a group named TEST_GROUP
:
sudo groupadd TEST_GROUP
ShellScriptThis command creates a new group called TEST
on the system._
GROUP
2. Creating a Login User
To create a user who can log into the system, use the useradd
command. By default, this creates a login user with the username and a home directory.
sudo useradd -m TEST_USER
ShellScript-m
: This option ensures that the home directory is created for the user (/home/TEST_USER
).
You can also specify additional details like the shell, primary group, and more. For example:
sudo useradd -m -s /bin/bash -g TEST_GROUP TEST_USER
ShellScriptThis command creates a login user TEST_USER
, assigns them to the TEST_GROUP
, and sets their default shell to /bin/bash
.
3. Creating a Non-login User
Non-login users are typically used for system services, where no login is required. You can create such a user with the useradd
command and the -M
option:
sudo useradd -M -s /sbin/nologin TEST_USER_NOLOGIN
ShellScriptThis creates the user TEST_USER_NOLOGIN
, but it does not create a home directory, and the user cannot log in to the system.
4. Modifying a User’s Group
Sometimes, you need to modify a user’s group membership. To change a user’s primary group, use the usermod
command with the -g
option:
sudo usermod -g TEST_GROUP TEST_USER
ShellScriptThis changes the primary group of TEST_USER
to TEST_GROUP
.
To add a user to additional secondary groups, use the -aG
option:
sudo usermod -aG ADDITIONAL_TEST_GROUP TEST_USER
ShellScriptThis adds TEST_USER
to ADDITIONAL_TEST_GROUP
without affecting their membership in TEST_GROUP
.
5. Modifying a User’s Home Directory
If you want to change the home directory for a user, use the usermod
command with the -d
option:
sudo usermod -d /home/NEW-DIRECTORY TEST_USER
ShellScriptThis changes the home directory of TEST_USER
to /home/NEW-DIRECTORY
. If you want to move the user’s existing home directory content, you can add the -m
option:
sudo usermod -d /home/NEW-DIRECTORY -m TEST_USER
ShellScriptThis moves the content from the old home directory to the new one.
6. Changing a User’s Password
To change a user’s password, use the passwd
command:
sudo passwd TEST_USER
ShellScriptYou will be prompted to enter a new password for TEST_USER
.
7. Deleting a User
To delete a user, use the userdel
command. You can delete the user without removing their home directory:
sudo userdel TEST_USER
ShellScriptTo delete the user and their home directory, use the -r
option:
sudo userdel -r TEST_USER
8. Granting Root Privileges to a User
To grant root privileges to a user, you typically add them to the sudo
or wheel
group (depending on the distribution). This can be done using the usermod
command:
sudo usermod -aG sudo TEST_USER
ShellScriptThis adds TEST_USER
to the sudo
group, granting them the ability to execute commands with root privileges using sudo
.
If you are using a system where the wheel
group is used for sudo privileges, you can instead run:
sudo usermod -aG wheel TEST_USER
ShellScript9. Other Related Operations
- Listing All Users: To see a list of all users, you can check the
/etc/passwd
file:
cat /etc/passwd
ShellScript- List a Specific User with the User Name:
cat /etc/passwd | grep TEST_USER
ShellScript- Alternatively, the
id
command displays the user’s UID, GID, and group memberships:
id TEST_USER
ShellScriptViewing Group Membership: To see which groups a user belongs to, use the groups
command:
groups TEST_USER
ShellScript- Locking and Unlocking User Accounts: If you want to lock or unlock a user’s account (i.e., prevent or allow login), use the
passwd
command with the-l
and-u
options:
sudo passwd -l TEST_USER # Lock account
sudo passwd -u TEST_USER # Unlock account
ShellScript10. Viewing a List of All Users
To view a list of all users on the system, you can use the awk
command to extract the usernames from the /etc/passwd
file. The /etc/passwd
file contains user account information, and each line represents a user. The username is the first field, separated by a colon (:
).
Here’s the command to list all usernames:
awk -F: '{print $1}' /etc/passwd
ShellScript-F:
: This tellsawk
to use the colon (:
) as the field separator.{print $1}
: This prints the first field of each line, which is the username.
This command will output a list of all users on the system.
Conclusion
Linux user management is essential for controlling access to the system. By using commands like useradd
, usermod
, and userdel
, system administrators can efficiently manage users and groups. Additionally, by assigning users to specific groups and granting them appropriate permissions, you can ensure the security and proper functioning of the system.
Understanding these commands and how to implement them is fundamental for any Linux administrator. Whether you are managing login users, non-login users, or user privileges, these operations form the core of Linux user management.
Here are some notable Linux-based brands and distributions:
- Ubuntu: A popular and user-friendly distribution, ideal for both beginners and experienced users. Ubuntu is widely used in personal computers, servers, and cloud environments.
- Red Hat Enterprise Linux (RHEL): A commercial distribution targeted at enterprises. RHEL is known for its stability and security, often used in servers and data centers.
- CentOS: A free and open-source distribution that is a downstream derivative of RHEL. CentOS is widely used for server environments.
- Debian: A versatile, community-driven distribution known for its stability. Debian serves as the base for many other distributions, including Ubuntu.
- Fedora: Sponsored by Red Hat, Fedora is a cutting-edge distribution that often serves as a testing ground for new technologies before they are integrated into RHEL.
- SUSE Linux Enterprise: A commercial distribution similar to RHEL, primarily used for enterprise environments, with a focus on scalability and security.
- Arch Linux: A minimalist and highly customizable distribution, popular with advanced users who prefer to build their systems from the ground up.
- Linux Mint: Based on Ubuntu, Linux Mint is a user-friendly distribution designed for users transitioning from Windows.
- Kali Linux: A Debian-based distribution focused on security and penetration testing. It includes numerous tools for ethical hacking.
- Manjaro: An Arch-based distribution designed to be more user-friendly, providing a rolling release model and an easy-to-use interface.
- Zorin OS: A distribution designed to resemble Windows, making it a good choice for users transitioning from Microsoft operating systems.
- Elementary OS: Known for its aesthetic design, Elementary OS is based on Ubuntu and focuses on providing a user-friendly experience with a simple and clean interface.
- Slackware: One of the oldest Linux distributions, known for its simplicity and minimalism. It is often used by advanced users who prefer a traditional, hands-on approach to configuration.
- Puppy Linux: A lightweight distribution designed to run on older hardware, Puppy Linux is extremely fast and efficient with a small footprint.
These brands and distributions represent a wide range of use cases, from user-friendly desktops to powerful enterprise solutions and specialized security tools.
Leave a Reply