Linux user management - Create, Remove, modify an user account

User Management is one of the major responsibilities of a System Administrator. The user can either be an account associated with an application or a human being. This post shows how a user account can be created, removed and modified via CLI.


useradd command

To create a new user in Linux, we use the useradd command. The syntax for useradd command is
useradd [options]username

Important options


-d set home directory of the new              account
-e set account expire date
-g set primary group
-G set the secondary groups
-s login shell for new account
-u set UID

 

examples

To create a new user account named foo with default settings
useradd foo

To create a new user account named foo with primary group as bar
useradd -g bar foo


To create a new user foo, with an additional secondary group admin, type
useradd -G admin foo


To create a user foo with login shell as 'nologin'
useradd -s /sbin/nologin foo




userdel command


To remove a user in Linux, we use the userdel command. The syntax for useradd command is
userdel [options]username

 

examples

To remove a user account foo, type
userdel foo
The above command removes user named foo but the user home directory and mail spool will exist. To remove a user account along with home directory and mail spool, type
userdel -r foo




usermod command


To modify an existing user use the usermod command.The syntax for useradd command is
usermod [options]username


Important options

-a               append new groups to the list of existing secondary groups
-d modify home directory of a user account
-e modify account expire date
-g modify primary group
-G modify the secondary groups
-L lock user account
-m Move the content of the user's home directory to the new location
-s login shell for new account
-u set UID
-U Unlock user account


examples


To modify and move the contents of user foo's home directory from /home/foo to /home/bar, type
usermod -d /home/bar/ -m foo

To forcefully change the primary group of user foo to admin, type
usermod -g admin foo

To modify user foo with secondary group as bar,  type

usermod -G bar foo

To append a new group named calypso, with out removing the existing secondary groups, type
usermod -G calypso -a foo

To temporarily lock a user foo, from logging in
usermod -L foo

There are alternate methods of creating, removing and modifying user accounts, which will be covered in later post. For more options and details about the commands see the respective man pages.

Comments

Popular posts from this blog

Understanding awk command with examples

Understanding sed command with example -Part 1

How to install a Software in Linux