Linux usermod Command Tutorial With Examples

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-usermod-command-tutorial-examples/
PINTEREST
PINTEREST
INSTAGRAM

Linux provides usermod command by default for the most of the distributions. This command is used to user modification. After user creation the user related information, values and policies can be changed with usermod command. In this tutorial we will look various features of usermod command with examples.

Syntax

Syntax of the usermod command is like below.

Help

Usermod Help
Usermod Help

Add Comment or Information

User information is stored in a file named passwd . This file is located as /etc/passwd . In the ancient days of Linux and Linux this file is also used to store users password. But storing user information and user credential created security problems and passwords are stored in a separate file named shadown in the same directory etc .

There is a columns used to store information about the user. This information can be anything like First name, surname, the birthday etc.

Add Comment or Information
Add Comment or Information

We have set the comment for the user ismail as İsmail Baydan . We can check the modification with grep command by filtering line contains the user name ismail.

Change Home Directory

Human users generally have home directory for storing data, file, media etc for their personal or corporate usage. This home directory is generally reside in /home in Linux root file system.

For example user ismail will have the /home/ismail directory as his own home directory for the most the Linux systems. This value can be changed after creation for some reasons like we need to set and application folder for an application user like apache .

LEARN MORE  Bash Shell Export

In this example we will change the home directory of user nick . We will set /mnt as nick’s home directory like below.

Change Home Directory
Change Home Directory

As we can see from grep command output column number 6 is /mnt which is nick’s new home directory.

Set User Account Expiry Date

Some users are generally created in a hurry for temporary usage and than they forgotten and resides in the system as an active account. This is an security vulnerability and should be avoided with practical solution. One of the action to take to prevent this security issue is setting account expiry date for required accounts. Expire date will be specified in YYYY-MM-DD format as 2017-02-28 in this example for the user nick

Set User Account Expiry Date
Set User Account Expiry Date

And we will check the account expire date with the following command for the user nick.

Show User’s Groups

As like in the Windows operating systems in Linux operating systems user accounts are associated with groups. There are two type of user group. First type is primary group and each user account have single primary user group. Other type of group is secondary groups where each user may have more than one group. Primary groups of existing users can be changed with usermod command easily.

Show User's Groups
Show User’s Groups

Change User Primary Group

Previously we have printed the user group. Now we will change the user primary group more detailed information about primary group can be found previous example.

LEARN MORE  How Prompt for Input in Bash

In this example we will add user nick to the root group.

Change User Primary Group
Change User Primary Group

Adding Group To Existing User

There is two type of supplementary or secondary group addition. First we will specify the supplementary groups for the user and previous supplementary group memberships of the user will be removed if old groups do not presented. Which simply means the user will have only specified groups and all old groups will be reset.

Adding Group To Existing User
Adding Group To Existing User

And the second way we will add new supplementary groups to the user but old groups will be preserved. For his operations we will use extra option -a like below.

Adding Group To Existing User
Adding Group To Existing User

Change User Login Name

User accounts have login names to authentication themselves. In Linux operations systems users are identified by their user ID’s. User names are used an easy way to remember and use. So A user account user ID can not be changed after creation but the user name can be changed easily. In this example we will change user name nick to the nick1 with option -l .

Change User Login Name
Change User Login Name

Lock User Account

Some times we may need to lock some users for various reasons like security, holiday, test etc. User locking means the account will be inactive up to unlock operations and will be no user login for this period. In this example we will lock user nick1 with the -L option.

LEARN MORE  Nmap Host Discovery with Examples

As we can see from screenshot in the shadow file a ! is added which simply symbolize the account is locked.

Unlock User Account

Now in the previous example we have locked the account and we want to unlock the account named nick1. Simply provide -U options to the usermod command like below.

Change User Shell

In Linux user accounts are associated with shells. Shell is the application which interface with user and operating system. There are different type of shells used in Linux community. But the most popular shell is bash and generally used for most Linux distributions. This default shell for the user account can be changed with -s options.

In this example we will change the shell of user account nick1 with sh shell.

Change User Shell
Change User Shell

Change User ID

User id of and account can be changed too. This change operation is done with -u option like below. In this example we will change the user nick1 user ID to the 1234 .

Change User ID
Change User ID

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-usermod-command-tutorial-examples/
PINTEREST
PINTEREST
INSTAGRAM