Red Hat Linux 7 Unleashed

Red Hat Linux 7 Unleashed

By William Ball

Managing Users and Groups from the Command Line

Although linuxconf gives you the option of having a GUI admin tool for managing users and groups, you can also manage users and groups from the command line. This is something Linux is pretty famous for.

Adding Users

The useradd command is a command-line utility that you can use to create a new user or to update an existing user. To add new user Barney Rubble to the system, simply su to root and type:


   [root@cartoons]# useradd -c "Barney Rubble" -d /home/brubble brubble

The -c option is the full name of the person. The -d option is the home directory that you would want to create for this new user. Lastly, you specify the userid of your new user. Now you can assign a password to the user ID:

[root@cartoons]# passwd brubble
Changing password for user brubble
New UNIX password:

You could have set the password by including the –p option in the useradd command, but it requires the encrypted password. The two-step process is a little easier if you are manually creating a new user.

For a more detailed listing of available options for these commands, you can view the man pages for useradd.

Changing User Passwords

The user root can use the passwd command to change the password for any user. If you would like to change the password for the current user logged in, yourself, type passwd . For obvious reasons, the system will prompt you for your current password before it assigns you a new one.

Removing Users

You can remove a user with the userdel command:

[root@cartoons]# userdel brubble

You can use an -r option if you would like to remove the user's home directory also:

[root@cartoons]# userdel -r brubble

Be careful! This will delete all of the user's data. You may want to back up this data before you remove it.

For a more detailed listing of available options for these commands, you can view the man pages for userdel.

Managing Groups from the Command Line

Not only can you manage users from the command line, but Red Hat Linux also provides a couple of utilities for managing groups.

To create a new group you can use the groupadd command:

[root@cartoons]# groupadd newgroup

You can then add a user to the group with the usermod command:

[root@cartoons]# usermod -G newgroup brubble

To delete the group use the groupdel command:

[root@cartoons]# groupdel newgroup

All group commands modify the /etc/group file. This is where all group information is stored. If you are brave you can edit the file directly, although it's probably not the best idea to do so because you are more likely to introduce errors in the file than when you use the command-line utilities.

For the format of the /etc/group file, each line contains four segments and is delimited by colons:


   group name:password:group ID:users

If nothing is to be entered into a field, that field is left blank. However, a colon will still delimit the field from the other fields. Table 21.1 contains a short description of each of the fields in the /etc/group file.

Table 21.1. /etc/group File Fields

Segment Description
group name A unique identifier for the group
password Usually the password is blank, *, or x. By default it is x.
group ID The unique number that identifies a group to the operating system
users A list of all user IDs that belong to that group

When adding groups to this file, just follow the format of the existing fields. Add a unique group, assign it a password if necessary, give it a unique group ID, and then list the users associated with that group. The users are separated with commas. If you do not correctly format the line or if the data is incorrect in some other way, the users might not be able to use that group ID.

If the system were using a shadow password system, the password field would be moved to /etc/shadow.group, and an x would be assigned to the field in the /etc/group file.

When finished editing the /etc/group file, double-check its permissions. It should be owned by root, and its group should be root or sys (a group ID of 0). The permissions should be read and write for owner and read for everyone else (644 in hex).

The list of groups does not need to be in a particular order. The order of the list of users in each group is also irrelevant. Red Hat Linux will search the entire file until it comes to the line that it is looking for.

Although users can be in several groups, Linux only allows them to be active in a single group at a given time. The starting group, commonly called the primary group, is the group associated with the user in the /etc/passwd file. If a user wants to switch to another group (and he or she is in the group according to /etc/group), the user must issue the newgrp command to switch.

All these commands have man pages where you can read more about them.

Share ThisShare This

Informit Network