How to Add, Modify and Remove User in Linux?

Consumer administration in any working system is without doubt one of the fundamental routine duties of a system administrator.

For a Linux based mostly working system, this normally includes creating consumer accounts, modifying current accounts reminiscent of altering their house listing, default shell, locking/unlocking a number of accounts, and deleting consumer accounts.

Earlier than we look at the instructions and processes to carry out these duties, let’s take a quick take a look at how consumer accounts will be categorized in Linux. Additionally observe that given instructions, until explicitly specified, will work in commonest Linux distributions.

Consumer Varieties

Root consumer

The root consumer is the working system administrator with all permissions to carry out operations. Normally alone root can set up/take away or replace fundamental system applications and libraries. It’s the solely consumer account with system-wide privileges.

So the basis consumer is essentially the most highly effective consumer of the system.

Particular consumer

These are the customers with out logins. They do not have all of the privileges of the root consumer. Relying on the account, they carry out totally different specialised roles.

These are routinely created through the set up of an utility. bin, sync, lp, mail, operator, squid are some examples of particular customers.

Widespread customers

Common customers solely have full permissions of their work listing, normally their house listing. They don’t have any rights to handle the system or set up the software program. They can’t carry out these duties with out particular privileges by way of sudo.

Add consumer

Debian/Ubuntu

On a Debian or Ubuntu based mostly system there are a selection of choices so as to add customers from CLI. The primary command is adduserwhat a Perl script is and makes use of useradd command within the backend which we are going to see the usage of later.

Since including a consumer is a privileged activity, you need to make use of sudo as prefix and username as argument. Different particulars will be supplied if requested. Aside from the username and password, the opposite info is optionally available. We will confirm that the consumer was created through the use of id command.

$ sudo adduser johndoe
Including consumer `johndoe' ...
Including new group `johndoe' (1003) ...
Including new consumer `johndoe' (1003) with group `johndoe' ...
Creating house listing `/house/johndoe' ...
Copying information from `/and so on/skel' ...
New password:
Retype new password:
passwd: password up to date efficiently
Altering the consumer info for johndoe
Enter the brand new worth, or press ENTER for the default
        Full Identify []: John Doe
        Room Quantity []:
        Work Telephone []:
        House Telephone []:
        Different []:
Is the data right? [Y/n] Y
$
$ id johndoe
uid=1003(johndoe) gid=1003(johndoe) teams=1003(johndoe)
$

CentOS/RHEL/Fedora (together with Debian/Ubuntu)

The next command, useradd works on RHEL-based OS distributions and works equally nicely on Ubuntu/Debian hosts. The best syntax (with out further choices) to create a brand new consumer is:

$ sudo useradd <username>

Instance:

$ sudo useradd janedoe

The useradd command helps a number of choices that may be specified throughout consumer creation, the commonest are consumer ID (UID), group ID (GID), default shell and residential listing, and so on. An instance of that is given beneath:

$ sudo useradd -s /bin/sh -d /information/newhome -c "Jane Doe" -u 1005 janedoe

You’ll be able to confirm the newly created consumer utilizing id command:

$ id janedoe
uid=1005(janedoe) gid=1005(janedoe) teams=1005(janedoe)
$

Change consumer

Typically one wants to vary properties of current customers based mostly on organizational necessities, consumer requests or system migrations. Most of those properties are simple to switch, however we have to be certain that this impacts the consumer setting and entry to information owned or accessed by the consumer.

Default shell

The default shell is the CLI shell that’s created when a consumer begins a brand new CLI session, both regionally or over SSH. Most fashionable programs have a default consumer Bash although it might range based mostly on the Linux distribution or the consumer setting. To alter a consumer’s default shell, use:

$ sudo usermod -s <shell> <username>

Instance:

$ getent passwd janedoe
janedoe:x:1005:1005::/information/newhome:/bin/sh
$ sudo usermod -s /bin/bash janedoe
$ getent passwd janedoe
janedoe:x:1005:1005::/information/newhome:/bin/bash
$

As you’ll be able to see within the output above, the shell has modified from /bin/sh Disagreeable /bin/bash for consumer janedoe.

House folder

Just like the default shell, a consumer’s house listing will be modified to a unique location utilizing:

$ sudo usermod -d <new_directory_path> <username>

Within the instance beneath, the consumer’s house listing janedoe has modified to /information/janedoe:

$ getent passwd janedoe
janedoe:x:1005:1005::/information/newhome:/bin/bash
$ sudo usermod -d /information/janedoe janedoe
$ getent passwd janedoe
janedoe:x:1005:1005::/information/janedoe:/bin/bash
$

Earlier than you make the swap, be sure that the brand new folder has the right possession and permissions. In any other case, the consumer could expertise issues logging in or working within the new house listing.

consumer title

You’ll be able to change the consumer ID of an current consumer utilizing:

$ sudo usermod -u <new_uid> <username>

Instance:

$ getent passwd janedoe
janedoe:x:1005:1005::/information/janedoe:/bin/bash
$ sudo usermod -u 1010 janedoe
$ getent passwd janedoe
janedoe:x:1010:1005::/information/janedoe:/bin/bash
$

Once more, altering the UID modifications the way in which the Linux file system assigns possession and permission to a file or listing. Make it possible for the consumer’s house listing and its contents and all different information anyplace within the system that have been initially owned by the consumer (with previous UID) have been modified to mapped UID. Failure to take action could trigger issues within the CLI session and consumer entry to information.

Default group

The default group is normally the consumer’s default group ID, which is created throughout consumer creation until a unique GID is specified. Linux permits you to change a consumer’s default group that makes use of usermod command too. Right here is the syntax you need to use:

$ sudo usermod -g <new_gid or group_name> <username>

Here is an instance:

$ getent passwd janedoe
janedoe:x:1010:1005::/information/janedoe:/bin/bash
$ sudo usermod -g 1001 janedoe
$ getent passwd janedoe
janedoe:x:1010:1001::/information/janedoe:/bin/bash
$

Once more, be sure that the brand new group ID is about to the consumer’s house listing, contents, and another information or directories which can be acceptable to correctly migrate their ownerships.

Add/take away teams

Along with the default group, a consumer in Linux will be a part of secondary teams. We will all the time add or take away further teams {that a} consumer belongs to usermod command.

$ sudo usermod -a -G <group_id or group_name> <username>

Instance:

$ id janedoe
uid=1005(janedoe) gid=1005(janedoe) teams=1005(janedoe)
$ sudo usermod -a -G docker janedoe
$ id janedoe
uid=1005(janedoe) gid=1005(janedoe) teams=1005(janedoe),1001(docker)
$

In the identical means you employ to take away a consumer from one of many secondary teams gpasswd command as proven beneath:

$ sudo gpasswd -d <username> <groupname>

Instance:

$ id janedoe
uid=1005(janedoe) gid=1005(janedoe) teams=1005(janedoe),1001(docker)
$ sudo gpasswd -d janedoe docker
Eradicating consumer janedoe from group docker
$ id janedoe
uid=1005(janedoe) gid=1005(janedoe) teams=1005(janedoe)
$

GECOS How

GECOS discipline /and so on/passwd accommodates consumer info or feedback. We could change this info for an current consumer if:

$ sudo usermod -c <remark> <username>

Instance:

$ getent passwd janedoe
janedoe:x:1005:1005::/information/janedoe:/bin/bash
$ sudo usermod -c "Jane Doe - System Admin" janedoe
$ getent passwd janedoe
janedoe:x:1005:1005:Jane Doe - System Admin:/information/janedoe:/bin/bash
$

Observe that in case your remark or consumer information accommodates areas, you have to enclose that discipline in quotes, as within the instance above.

Login title

The consumer’s login title can be modified with usermod utilizing command -l flag:

$ sudo usermod -l <new_username> <old_username>

Instance:

$ id janedoe
uid=1005(janedoe) gid=1005(janedoe) teams=1005(janedoe)
$ sudo usermod -l jane_doe janedoe
$ id jane_doe
uid=1005(jane_doe) gid=1005(janedoe) teams=1005(janedoe)
$

Keep in mind to replace consumer credentials in keeping with the brand new title wherever it’s used. Even in instructions like idthe brand new username have to be specified.

Delete consumer

A consumer will be faraway from Linux utilizing userdel command.

$ sudo userdel <username>

Instance:

$ id janedoe
uid=1005(janedoe) gid=1005(janedoe) teams=1005(janedoe)
$ sudo userdel janedoe
$ id janedoe
id: ‘janedoe’: no such consumer
$

To delete a consumer together with their house listing and mail spool, add -r flag too.

$ sudo userdel -r <username>

Particularly for Ubuntu based mostly programs you may as well use consumer deluser command to delete a consumer:

$ sudo deluser <username>

Equally, use to delete the house listing and mail spool as nicely:

$ sudo deluser --remove-home <username>

For detailed info and different supported choices, see the principle web page of assorted instructions utilizing:

$ man adduser
$ man useradd
$ man usermod
$ man deluser
$ man userdel

Conclusion

This text confirmed totally different points of consumer administration in a Linux system. It explains, amongst different issues, totally different classes of customers and how one can add and take away them. It additionally covers numerous choices that assist change the parameters of an current consumer. Whereas it doesn’t cowl all of the capabilities supported by the assorted instructions, it does cowl lots of the widespread administrative duties {that a} system administrator could encounter of their day-to-day work.

You might also be curious about studying: Easy methods to Delete Information and Folders in Linux?

Rate this post
Leave a Comment