01. User and Group Management

Linux is a multi-user system. This means that the platform can handle multiple user logins. As a system adminstrator, it's important to know how to manage all the users, assign them to groups and manage their permission levels.

In this series, we'll learn how to first create and manage users and groups, then look at all the important files that manage these settings in the background.

Three Types of Accounts

There are three types of accounts on any Unix platform, all of may be within a group account:

  • Root account - unrestricted to running any command.
  • System account - accounts that are used to maintain the operation of daemons, mail accounts and other system-related programs.
  • User accounts - used by ordinary users or groups or users.

Creating a user

Creating a user is simple. Simply use useradd with any of the below options to create a user. Make sure you are root when you perform these actions.

$ useradd -c "Sarah Jones" -d /home/sarah -G intro-unix-course sarah
Command field (store the user's full name).
Specify user's home directory.
Set a date when the account expires (YYYY-MM-DD).
Set the default shell.
Specify that you already have an encrypted password.
Don't specify a home directory.
Define all secondary groups that the user will belong to.
Set the user's primary group.
Define the number of days after a password expires that the account is permanently disabled.
0 for right after the password expires, -1 for don't use this feature at all.
Create a home directory for the user.
Specify a skeleton directory.
Change the login name of the user.
Lock the user's account.
Unlock the user's account.
Force the change of password after this many days have passed.

The Skeleton Directory

The skeleton directory holds all the default files to be included when a user is created. The system default skeleton folder is in /etc/skel.

$ mkdir skel
$ touch skel/example.txt
$ touch skel/example2.txt
$ useradd -c "Sarah Goodwill" -m -k /home/skel sarah 
$ ls /home/
sarah   ubuntu   user
$ ls /home sarah/
example.txt   example2.txt

If we forgot to include the -k option, we can easily create the directory and chown it to the user.

$ cd /home
$ mkdir sarah 
$ chown sarah:sarah 

Or we can use usermod with the -d option to assign sarah's new home directory. Note that the user can't be logged in.

$ usermod -d /home/sarah 

Modying existing users

To modify any existing users, use the usermod command with any of the above options.

# Set the user account to expire on the last day of 2016
$ usermod -e 2016-12-31 sarah 

Setting user password

To set a user's password, simply use the passwd command.

$ passwd sarah

Deleting users

To delete a user, use the userdel command. If Sarah spawned any processes, you can send the processes a kill signal to close them.

$ userdel sarah
userdel: user sarah is currently used by process 2381
$ kill 2381
$ userdel -r sarah

The -r options removes the user's home directory as well. The safe option is to not use this option, unless you are 100% sure.

Managing expiration time limits

The chage command is used to manage password expiration time limits. It sets the number of days between required password changes. First, make sure it's installed on your distro:

$ sudo apt-get install chage
$ sudo yum install chage
Set the account expiration date (YYYY-MM-DD).
Number of inactive days before locking the account.
Minimum number of days between password changes.
Maximum number of days that a password is valid.

Want to avoid becoming a code monkey?

The Clean Coder

Want to avoid becoming a code monkey? Try Good Practice

This book is packed with practical advice about everything from estimating and coding to refactoring and testing. It covers not only technique, but also attitude, as it shows how to approach software development with honor, self-respect, and pride; communicate and estimate faithfully; and understand that deep knowledge comes with a responsibility to act.

$ Check price
44.9944.99Amazon 4.5 logo(427+ reviews)

More Good Practice resources

Take your Linux skills to the next level!

System Admin Handbook

Take your Linux skills to the next level! Try Linux & UNIX

This book approaches system administration in a practical way and is an invaluable reference for both new administrators and experienced professionals. It details best practices for every facet of system administration, including storage management, network design and administration, email, web hosting, scripting, and much more.

$ Check price
74.9974.99Amazon 4.5 logo(142+ reviews)

More Linux & UNIX resources