2014-05-27

If you are a linux system administrator one of the more important things to learn and to know is how to manage users and groups on linux systems.

This guide will describe how to add, delete and modify users and groups (manage users and groups) on CentOS 6 linux (same applies to other linux distributions) from command line (CLI).

The following guide was tested on a fresh fully updated CentOS 6.5 64bit minimal install.



Manage Users and Groups on Linux

Let’s start our Manage Users and Groups on Linux Guide!

1. Adding New Users

There are two available commands for adding a new user to our linux system. These commands are “useradd” and “adduser”. The difference is that “useradd” is a native binary, that comes with the system and “adduser” is a perl script which uses “useradd” binary. “adduser” is more user friendly than “useradd” (though on CentOS it seems completely the same) but in the end you can use both to achieve the same result. Let’s try “adduser” command first. Please note we are running all commands as user root or with sudo privileges!

Adduser

Run command “adduser username” to create a new user account as follows:

If you want to manually specify a home directory for the user run the command with “-d” parameter. Home directory will be created if it does not exist:

Set the password for the newly created user “geekpeek”. Without setting a password the newly created user will not be able to login via SSH (default SSH security restrictions):

Check to confirm home directory for user “geekpeek” was created:

That’s great. The newly created user can now connect to our linux system via SSH, but sometimes we want to force the user to change the password on first login. We can do this by issuing the following command:

We can now provide the user with the details about his account and remind him he is forced to change his password on first login.

Useradd

As in previous “adduser” example run command “useradd username” to create a new user account:

If you want to manually specify a home directory for the user run the command with “-d” parameter. Home directory will be created if it does not exist:

Set password for user to enable him to login via SSH:

Confirm users home directory was created:

..and force password change (if desired) on first login:

We can now provide the user with the details about his account and remind him he is forced to change his password on first login. So now we are one step closer learning how to manage users and groups on linux systems. Let’s continue…

NOTE: When adding new users a group with the same name is automatically created!

ADDUSER & USERADD HELP:

2. Adding New Groups

There are two commands available to add a new group in some distributions but not in CentOS! In CentOS we can only use command “groupadd” to add a new group to our linux system.

Run command “groupadd groupname” to create a new group:

We can check and confirm the group was successfully created by looking into /etc/group file:

Great, new group was successfully created with group ID 501. Let’s create another group called “admin” for following examples:

Great

GROUPADD HELP:

3. Modifying Users

There are many scenarios in which we are forced to modify an existing user. We will not go through all of the use cases but the syntax is always the same “usermod [options] username” – you can get all information by running “usermod –help”. In the following example we will re-configure user “geekpeek” and change his primary and secondary groups.

First use “id username” command to see existing user configuration:

As we see user “geekpeek” has user ID 500 and his primary group is “geekpeek” with group ID 500. This user is only a member of “geekpeek” group. Let’s add “geekpeek” user to previously created group “testing”:

As we can see “-G” parameter added user “geekpeek” to a supplementary group “testing”. Now this user is a member of two groups, primary “geekpeek” and secondary “testing”. Let’s try and change this users primary group with the following command:

Voila, the parameter “-g” changes the users primary group. We can also change users user ID:

Let’s  modify user “geekpeek” to the state it was before we started playing around with it:

NOTE: We can use numeric user or group ID’s or human friendly user and group names to change and modify users.

USERMOD HELP:

4. Modifying Groups

Modifying groups is pretty much the same or similar to modifying users. We can modify groups with the command “groupmod” and the syntax is “groupmod [options] groupame” – you can get all information by running “groupmod –help”. In the following example we will change the group name and group ID for previously created group “testing”.

Let’s see the current configuration for group “testing”:

Let’s change group name to “testgroup” with the following command:

..and check if we succeded:

We can see that “geekpeek” user secondary group name changed. What will happen when we change the group ID by issuing the following command:

Let’s see the current situation:

..again everything was changed successfully and no manual action is needed.

GROUPMOD HELP:

5. Removing Users

We can remove users from our linux system with the command “userdel” The correct syntax is “userdel [options] username”. To remove user home directory and mail spool add parameter “-r” as option:

Check that home directory and mail spol was removed:

Doing great! Just one more step in our manage users and groups on linux system guide!

USERDEL HELP:

6. Removing Groups

We remove groups with “groupdel” command. The syntax is “groupdel groupname” – no parameters are available here:

..and we are finished!

As we can see we can easily manage users and groups from linux command line. If you do not remember the parameter to use, you can always see help which is very direct and self explanatory. Hope this post was helpful in learning how to manage users and groups on CentOS 6.

The post Manage Users and Groups on Linux appeared first on GeekPeek.Net.

Show more