Software & AppsOperating SystemLinux

Managing User Accounts in Terminal: List, Add, Delete, Modify Users with Commands

Ubuntu 13

In the world of Linux, managing user accounts is a fundamental task of system administration. This article will guide you through the different commands you can use to list, add, delete, and modify users directly from the terminal.

Quick Answer

Managing user accounts in the terminal allows you to list, add, delete, and modify users using various commands. You can list users using commands like cut -d: -f1 /etc/passwd, add users with sudo adduser new_username, delete users with sudo userdel username, and modify users with commands like usermod -l new_username old_username. Additionally, you can add a user to the sudo group using adduser username sudo.

Listing Users

To list all users in the terminal, you can use several commands, each providing slightly different information.

1. cut -d: -f1 /etc/passwd

This command lists all local users on your system. The cut command is used to remove sections from each line of files. The -d: option specifies the delimiter, and -f1 tells the command to print the first field, which is the username.

2. cat /etc/passwd | grep -vE '(/bin/false|/sbin/nologin|/bin/sync)' | cut -d: -f1

This command lists all users excluding system accounts and guest accounts. grep -vE is used to filter out lines matching the specified patterns.

3. getent passwd

This command lists all users, including those from different user databases. getent stands for “get entries” and it retrieves and displays database entries from the system databases.

Adding Users

To add a new user, you can use the following commands:

1. sudo adduser new_username

This command will add a new user with the specified username. The adduser command creates a new user account using the values specified on the command line and the default values from the system.

2. sudo useradd new_username

This command can also be used to add a new user. The useradd command creates a new user or updates default new user information.

Deleting Users

To remove/delete a user, you can use the following commands:

1. sudo userdel username

This command will remove a user from the system. The userdel command modifies the system account files, deleting all entries that refer to the user name.

2. sudo deluser --remove-home username

This command will remove a user and their home directory. The --remove-home option instructs the deluser command to remove the user’s home directory and mail spool.

Modifying Users

To modify a user, you can use the following commands:

1. usermod -l new_username old_username

This command modifies the username of a user. The -l option instructs the usermod command to change the user’s login name.

2. sudo passwd username

This command changes the password for a user. The passwd command changes passwords for user accounts.

3. sudo chsh username

This command changes the shell for a user. The chsh command changes the user login shell.

4. sudo chfn username

This command changes the details for a user (e.g., real name). The chfn command changes user finger information.

Adding a User to the sudo Group

1. adduser username sudo

This command will add a user to the sudo group. Being part of the sudo group allows a user to execute commands with root privileges.

2. usermod -aG sudo username

This command can also be used to add a user to the sudo group. The -aG option appends the user to the supplementary group(s).

Remember, some commands may require root or sudo privileges to execute. Also, use caution when deleting users or their home directories. For more information on these commands, refer to their respective manual pages (man adduser, man useradd, man userdel, etc.) or search online for further documentation.

By understanding these commands, you can effectively manage user accounts on your Linux system directly from the terminal. With practice, these tasks will become second nature. Happy computing!

How can I list all users in the terminal?

To list all users in the terminal, you can use the command cut -d: -f1 /etc/passwd. This command will display the usernames of all local users on your system.

How can I add a new user?

To add a new user, you can use the command sudo adduser new_username. This command will create a new user account with the specified username.

How can I delete a user?

To delete a user, you can use the command sudo userdel username. This command will remove the specified user from the system.

How can I modify a user’s username?

To modify a user’s username, you can use the command usermod -l new_username old_username. This command will change the login name of the user to the new username specified.

How can I change a user’s password?

To change a user’s password, you can use the command sudo passwd username. This command will prompt you to enter and confirm the new password for the specified user.

How can I add a user to the `sudo` group?

To add a user to the sudo group, you can use the command adduser username sudo. This command will grant the user the ability to execute commands with root privileges.

How can I change a user’s shell?

To change a user’s shell, you can use the command sudo chsh username. This command will prompt you to enter the new shell for the specified user.

How can I change a user’s details (e.g., real name)?

To change a user’s details, such as the real name, you can use the command sudo chfn username. This command will prompt you to enter the new details for the specified user.

Are there any precautions I should take when deleting a user?

Yes, when deleting a user, use caution as it will permanently remove the user’s account and their home directory. Ensure you have a backup of any important files before deleting the user.

Where can I find more information about these commands?

For more information about these commands, you can refer to their respective manual pages by using the man command followed by the command name (e.g., man adduser, man userdel). You can also search online for further documentation and resources.

Leave a Comment

Your email address will not be published. Required fields are marked *