What are the Unix shell commands for handling user administration?

Here’s a quick cheat-sheet for Unix user administration from the command line terminal. Make sure you are logged in as “root” user for these, or enter “sudo -i” for root user access.


useradd - Add 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 super
  • create home directory
  • a login name of super

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


usermod - Modify an 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 super

usermod -Gothers super


userdel - Delete a user

Options:

  • -r (remove home directory)

Example: To remove the user ’super’ and his home directory

userdel -r super


passwd - (Re)set user’s password

Options:

  • username (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 ’super’ user while logged in as ‘root’:

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


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 ’super’:
su super
Enter super’s or root’s passwd

To return to original user, enter exit


Where is user and group information stored on Linux?

Usernames and primary groups are stored in /etc/passwd. This file can be directly edited using the “nano” editor, although this is not recommended. The format for the file is:

  • Username (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/<username>
  • 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 “nano” editor. The format for the file is:

  • Group name
  • Group password (hardly ever used)
  • Group ID
  • Usernames (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.

Leave a Comment

Name: (Required)

E-mail: (Required)

Website:

Comment: