Managing User Accounts in Linux

In the Linux system administration, effective user management is critical. This article delves into the intricacies of managing user accounts in a Linux environment, covering essential tasks from user creation to access control. By the end, you’ll be well-equipped to handle user-related tasks confidently.
Lets get on with it by following these easy steps;
Create a User
Let’s start by creating a new user. The useradd
command is your go-to tool for this task. To add a user named "newuser," use the following command:
useradd newuser
After execution, you can verify the changes by running:
cat /etc/passwd | grep newuser
This command displays essential information about the user, including the username, user ID, group ID, and home directory.
Set an Expiry Date for the User
In some scenarios, you might want to set an expiry date for a user account. To accomplish this, we can use the chage
command. For instance, to set an expiry date for "newuser" two weeks from the current date, use:
chage -E $(date -d '+2 weeks' +%Y-%m-%d) newuser
To view the changes you’ve made, employ the chage
command with the -l
option:
chage -l newuser
The -l
option stands for "list" and provides detailed password expiry information for the specified user.
Prompt the User to Change Their Password on Login
Forcing users to change their passwords upon their next login is a common security practice. Achieve this by using the chage
command with the -d
option set to 0:
sudo chage -d 0 username
Again, you can confirm the changes using the chage -l
command:
chage -l newuser
This ensures that the user will be prompted to change their password when they log in.
Attach User to a Group
Group management is an integral part of user administration. To add a user to a group, you first need to create the group. Let’s create a group named “altschool”:
sudo groupadd altschool
With the “altschool” group in place, you can add the user “newuser” to it using the usermod
command:
usermod -aG altschool newuser
To confirm the user’s group memberships, you can use the groups
command:
groups newuser
Allow Group to Run Only the “cat” Command on /etc/
Precise control over user privileges is a key aspect of Linux user management. To allow the “altschool” group to run only the “cat” command on files within the /etc/
directory, you can edit the sudoers file using the visudo
command:
sudo visudo
This command opens the sudoers file with root privileges, enabling you to add specific permissions for the “altschool” group:
%altschool ALL=(ALL) /bin/cat /etc/*
Alternatively, you can echo this line into the sudoers file without opening it:
echo "%altschool ALL=(ALL) /bin/cat /etc/*" >> /etc/sudoers
To view the result, use the cat
command to inspect the sudoers file:
cat /etc/sudoers | grep altschool
With these configurations, the “altschool” group now possesses permission to use the “cat” command with superuser privileges (ALL=(ALL)
) specifically for files within the /etc/
directory.
Create a User Without a Home Directory
In certain scenarios, you might need to create a user without a home directory. The -M
option in the useradd
command allows you to do just that:
sudo useradd -M anotheruser
To verify the change, inspect the /etc/passwd
file:
cat /etc/passwd | grep anotheruser
It’s worth noting that, even though you used the - no-create-home
flag for a new user like "granger," some systems might still create a home directory depending on system configuration or user management settings. To ensure no default home directory is created, you can specify a non-existent directory using the -d
flag:
useradd -M -d /nonexistent anotheruser
Exploring User Information
To delve into the contents of the /etc/passwd
file with superuser (root) privileges, employ:
sudo cat /etc/passwd
Here, you’ll find entries for various system and service accounts, as well as users like “renge” and “granger.” You’ll notice that “granger” has a home directory set to “/nonexistent,” indicating that although the - no-create-home
option was used during user creation, the system still assigned a directory for the user.
Managing user accounts in Linux is a fundamental skill for any system administrator. By understanding the intricacies of user creation, access control, and group management, you can maintain a secure and organized system environment. These techniques help you tailor user privileges to meet your system's unique requirements and security standards.
Subscribe to my newsletter
Read articles from sibyl obodoekwe directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
