Adding Linux Users

Every user who has access to a Linux system needs a login and a password. Each user must belong to a primary group and for security or access purposes can belong to several secondary groups.

In order to create new logins, modify or delete users, you must already be logged in as root.  The root login is the highest level and only certain individuals should have access to the root account.

useradd – Adding a new user

Options:

* -d home directory
* -s starting program (shell)
* -p password
* -g (primary group assigned to the users)
* -G (Other groups the user belongs to)
* -m (Create the user’s home directory

Example: To add a new user with

* a primary group of users
* a second group mgmt
* starting shell /bin/bash
* password of xxxx
* home directory of manoj
* create home directory
* a login name of manoj

useradd -gusers -Gmgmt -s/bin/shell -pxxxx -d/home/manoj -m manoj

usermod – Modifying existing user

Options:

* -d home directory
* -s starting program (shell)
* -p password
* -g (primary group assigned to the users)
* -G (Other groups the user belongs to)

Example: To add the group ‘others’ to the user manoj

usermod -Gothers manoj

userdel – Deleting a user

Options:

* -r (remove home directory)

Example: To remove the user ‘manoj’ and his home directory

userdel -r manoj

passwd – User’s Password

Options:

* user’s name (Only required if you are root and want to change another user’s password)

Example: To change the password for the account you are currently logged in as…

passwd
Enter existing password
Enter new password
Enter new password again (to validate)

Example: To change the password for the user ‘manoj’ (only you are logged in as root)…

passwd manoj
Enter existing password (can be either manoj’s password or root’s password)
Enter new password
Enter new password again (to validate)

Where user and group information stored

User names and primary groups are stored in /etc/passwd. This file can be directly edited using the ‘vi’ editor, although this is not recommended. Format of the file is…

* User (name normally all lower case)
* Password (encrypted – only contains the letter ‘x’)
* User ID (a unique number of each user)
* Primary Group ID
* Comment (Normally the person’s full name)
* Home directory (normally /home/<user name>
* Default shell (normally /bin/bash)

Each field is separated by a colon.

Passwords for each user are stored in /etc/shadow. This file should only be changed using the passwd command.

Group information is stored in /etc/group. This file can be directly edited using the ‘vi’ editor. Format of the file is…

* Group name
* Group password (hardly ever used)
* Group ID
* User names (separated by commas)

Each field is separated by a colon.

Default files

When a new user is created, the default files and directories that are created are stored in /etc/skel.

This directory can be modified to fit your needs. Modifications only effect new users and does not change anything for existing users.

su – Switch User

To switch to another user, use the su command. This is most commonly used to switch to the root account.

Example: To switch to root account…
su
Enter root’s passwd

Example: To switch to the user ‘manoj’…
su manoj
Enter manoj’s or root’s passwd

To return to original user, enter exit

How can I add a user to a group under Linux operating system?

You can use the useradd or usermod commands to add a user to a group. The useradd command creates a new user or update default new user information. The usermod command modifies a user account i.e. it is useful to add user to existing group. There are two types of group. First is primary user group and other is secondary group. All user account related information is stored in/etc/passwd/etc/shadow and /etc/group files to store user information.

useradd Example – Add A New User To Secondary Group

You need to the useradd command to add new users to existing group (or create a new group and then add user). If group does not exist, create it. The syntax is as follows:
useradd -G {group-name} username
In this example, create a new user called vivek and add it to group called developers. First login as a root user (make sure group developers exists), enter:
# grep developers /etc/group
Output:

developers:x:1124:

If you do not see any output then you need to add group developers using groupadd command:
# groupadd developers
Next, add a user called vivek to group developers:
# useradd -G developers vivek
Setup password for user vivek:
# passwd vivek
Ensure that user added properly to group developers:
# id vivekOutput:

uid=1122(vivek) gid=1125(vivek) groups=1125(vivek),1124(developers)

Please note that capital G (-G) option add user to a list of supplementary groups. Each group is separated from the next by a comma, with no intervening whitespace. For example, add user jerry to groups admins, ftp, www, and developers, enter:
# useradd -G admins,ftp,www,developers jerry
useradd example – Add a new user to primary group

To add a user tony to group developers use following command:
# useradd -g developers tony
# id tony
Sample outputs:

uid=1123(tony) gid=1124(developers) groups=1124(developers)

Please note that small -g option add user to initial login group (primary group). The group name must exist. A group number must refer to an already existing group.
usermod example – Add a existing user to existing group

Add existing user tony to ftp supplementary/secondary group with usermod command using -a option ~ i.e. add the user to the supplemental group(s). Use only with -G option :
# usermod -a -G ftp tony
Change existing user tony primary group to www:
# usermod -g www tony

Advertisements

About zephyr9000

Happy sailer
This entry was posted in Linux Command and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s