Bootstrap FreeKB - Linux Commands - usermod (modifying a users account)
Linux Commands - usermod (modifying a users account)

Updated:   |  Linux Commands articles

The usermod command can be used to modify a users account in Linux. The chage command can be used to view a users account.


Change username

The usermod command with the -l or --login option can be used to change a username. In this example, the username is changed from user1 to user2.

[root@server1 ~]# usermod -l user2 user1

 

The first field in the /etc/passwd file will show the new username (user2 in this example). The home directory is not changed from /home/user1 to /home/user2.

[root@server1 ~]# cat /etc/passwd
user2:x:1002:1002::/home/user1:
. . .

 

The first field in the /etc/shadow file will show the new username (user2 in this example).

[root@server1 ~]# cat /etc/shadow
user2:!:17014:0:99999:7::18350:
. . .

 


Change password

The passwd command can be used to create a new, encrypted password. The usermod command with the -p or --password option can be used to create a cleartext password.

[root@server1 ~]# usermod -p MyPassword user1

 

The second field in the /etc/passwd file will have an "x", because passwords are not stored in the /etc/passwd file.

[root@server1 ~]# cat /etc/passwd | grep user1
user1:x:1002:1002::/home/user1:

 

The second file in /etc/shadow will show either the encrypted or cleartext password.

[root@server1 ~]# cat /etc/shadow
user1:MyPassword:1002:1002::/home/user1:

 


Change UID

The usermod -u command can be used to change the UID (1234 in this example).

[root@server1 ~]# usermod -u 1234 user1

 

The third field in the /etc/passwd file will show the new UID (123456 in this example).

[root@server1 ~]# cat /etc/passwd
user1:x:1234:1002::/home/user1:
. . .

 


Change GID

The usermod -g command can be used to change the GID (5678 in this example).

[root@server1 ~]# usermod -g 5678 user1

 

The fourth field in the /etc/passwd file will show the new GID (5678 in this example).

[root@server1 ~]# cat /etc/passwd
user1:x:1001:5678::/home/user1:
. . .

 


Add a comment

The usermod -c command can be used to add a comment (Sample comment in this example).

[root@server1 ~]# usermod -c 'Sample Comment' user1

 

The fifth field in the /etc/passwd file will show the new comment (Sample comment in this example).

[root@server1 ~]# cat /etc/passwd
user1:x:1001:1002:Sample comment:/home/user1:
. . .

 


Change home directory

The usermod -d command can be used change the users home directory. In this example, the home directory is changed from /home/user1 to /home/user2.

[root@server1 ~]# usermod -d /home/user2 /home/user1

 

The sixth field in the /etc/passwd file will show the new home directory (/home/user2 in this example).

[root@server1 ~]# cat /etc/passwd
user1:x:1001:1002::/home/user2:
. . .

 


Change default shell

The usermod -s command can be used change the users default shell (/bin/bash in this example).

[root@server1 ~]# usermod -s /bin/bash user1

 

The seventh field in the /etc/passwd file will show the new default shell (/bin/bash in this example).

[root@server1 ~]# cat /etc/passwd
user1:x:1001:1002::/home/user1:/bin/bash
. . .

 

The default shell can be set to /sbin/nologin (Red Hat) or /bin/false (Debian) to prevent a user from being able to access the system.

[root@server1 ~]# cat /etc/passwd
user1:x:1001:1002::/home/user1:/sbin/nologin
. . .

 


Change primary group

The usermod -G command can be used change the users primary group (group1 in this example).

[root@server1 ~]# usermod -G group1 user1

 

The id command displays the users groups.

[root@server1 ~]# id user1
uid=1002(user1) gid=1002(root) groups=1003(group1)

 


Change additional groups

The usermod -aG command can be used to make the user a member of additional groups (group2 in this example).

[root@server1 ~]# usermod -aG group2 user1

 

The id command displays the users groups.

[root@server1 ~]# usermod -aG user1,root user1

[root@server1 ~]# id user1
uid=1002(user1) gid=1002(root) groups=1003(group1),1004(group2)

 


Lock user account

The usermod -L command can be used to lock a users account, so that the user account cannot be used.

[root@server1 ~]# usermod -L user1

 

The second file in /etc/shadow will begin with an exclamation point, which means the account is locked.

[root@server1 ~]# cat /etc/shadow
user1:!$6HV7FH3HVNFH7X8DJ1MDHC8SA7J1M388E8SJSJDMD71J37A:1002:1002::/home/user1:
. . .

 

When user1 attempts to sign in, an error will appear.

[root@server1 ~]# su - user1
su: Authentication Failure

 


Unlock user account

The usermod -U command can be used to unlock a user account.

[root@server1 ~]# usermod -U user1

 

The second file in /etc/shadow willl no longer contain the excla




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 058aa4 in the box below so that we can be sure you are a human.