Open SourceHow to Manage Linux Users From the Terminal

How to Manage Linux Users From the Terminal

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Linux is a multi-user Operating System (OS). This means that multiple users can use the same Linux system at the same time. As a system administrator or sysadmin, you need to be able to control who has access to the system at all times. Follow through this guide to see how you can manage users on your Linux machine.

Creating Users in Linux from the Terminal

To add a new user to Linux via the terminal, use the useradd command. The syntax is below:

useradd [options] username 

The useradd command requires administrative privileges in order to run it. Therefore, you must prefix it with sudo.

Usually, you will need for your user to have a home directory. Adding the -m option will enable sys admins to achieve this. The user’s home directory will be located at /home/username. Here is how that looks in code:

$ sudo useradd -m userY # home  directory at /home/userY 

The user we created above cannot access the system as of yet since they do not have a password. You will need to use the passwd tool to manage your users’ passwords.

To create a password for userY, run the command below:

$ passwd userY

You will be asked to create and confirm the new user’s password. The output on your terminal should look similar to the one below:

New password: 
Retype new password: 
passwd: password updated successfully

The same passwd command can be used to change a user’s current password. Simply run the same command (as the previous one) with sudo privileges to do so, as shown below:

 

$ sudo passwd userY # changing a user's password 

To login on your terminal as the new user, simply type the su command, followed by the username:

su userY
Password: 
$ 

When you need to exit from this user’s shell, you can use the exit command.

Linux Terminal Window
Example of Linux Terminal Window (courtesy Linux.com)

See Who Is Accessing a Linux System Through the Terminal

You may want to know which user is currently using the system. In this case, use the who or w commands. The difference between the two commands is that w gives you more detailed information than who. To use the who command, simply input:

$ who 

To know how many users are on your Linux system, you have to open the /etc/passwd file. Each line on this file represents the information of a user separated by colons. All users are allowed to read from this file.

Ideally, you should be able to add a user by adding an entry to the /etc/passwd file. This may not be possible, though, as detailed below.

By default, Linux systems encrypt a user’s password field and store its value in the /etc/shadow file. Only the root user has access to this file. For this reason, you’ll see the value x where a user’s password is supposed to be in the /etc/passwd file.
Therefore, you can only use the passwd tool to create a user if your system implements encryption.

Generally, it is not advisable to directly modify the /etc/passwd file. Doing so may create security vulnerabilities. For example, deleting a user entry from the file doesn’t mean that the user’s access has been revoked. A malicious actor can therefore exploit this vulnerability and gain access to the system.

Managing User Access in the Linux Terminal

Linux allows you to modify, revoke or limit user access. You can use the usermod command to modify all the information about a user. It takes in the same options as the useradd command.

For example, to change a user’s name you input:

$ sudo usermod userY userZ

As a system administrator, you may want to revoke access to a certain account. To lock an account, use the passwd tool with the -l option. Use the -u option to unlock it, as shown below:

$ passwd -l userZ
$ passwd -u userZ 

Managing Passwords in Linux Terminal

You may need to define an expiry date for a user’s password. The chage command enables you to do this. The -e option allows system admins to specify an expiry date for a user’s password in the mm/dd/yyyy format. On expiry, the user will be prompted to enter a new password. Here is the syntax:

$ chage -e 08/25/2025 userZ

You can also specify the maximum number of days a user’s password should be active using the -M option.

$ chage -M 6 userZ  # this user's password will expire in 6 days

Removing Users in the Linux Terminal

To remove a user account, use the userdel command. If you include the -r option, the user’s home directory will also be deleted.

$ userdel -r userY

Managing Linux Users in the Terminal

This guide has shown you how you can use the command line to manage Linux users. Recall that it’s advisable for you to use the passwd tool to create new users, even though your Linux system may not implement password encryption. This way, you protect your system from unintended vulnerabilities.

Read: How to Manage Linux Groups from the Command Line.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories