How to change a user login shell, UID or homedir with usermod

usermod is used for changing user info. You can change your login shell, userid, username, homedir, etc.

Watch Free Movies

I like this command because it’s syntax is similar to useradd’s:

$ usermod [options] username

1. Change the user login shell: usermod -s

$ grep anakin /etc/passwd
anakin:x:1006:1008:Anakin Skywalker:/home/anakin:/bin/bash
$ sudo usermod -s /bin/zsh anakin
$ grep anakin /etc/passwd
anakin:x:1006:1008:Anakin Skywalker:/home/anakin:/bin/zsh

The login shell can be changed also with the chsh command:

$ sudo chsh -s /bin/sh anakin
$ grep anakin /etc/passwd
$ anakin:x:1006:1008:Anakin Skywalker:/home/anakin:/bin/sh

2. Change the user id (UID): usermod – u

$ id xavier
uid=1014(xavier) gid=1016(xavier) groups=1016(xavier)
$ sudo usermod -u 1050 xavier
$ id xavier
uid=1050(xavier) gid=1016(xavier) groups=1016(xavier)

3. Change the user homedir: usermod -m -d

-d is for setting the homedir and -m is for creating the dir if it does not exist.

$ finger xavier | grep -i dir
Directory: /home/xmen Shell: /bin/bash
$ sudo usermod -m -d /home/profx xavier
$ finger xavier | grep -i dir
Directory: /home/profx Shell: /bin/bash

4. Add a user to a group with usermod:

How to add a user to a primary (existing) group: usermod -g.
$ groups xavier
storm : storm

So, storm’s primary group is storm. Now, I will change it to xmen:
$ sudo usermod -g xmen storm
$ groups storm
storm : xmen

Adding a user to secondary groups:
$ sudo usermod -G superhero,preetygirl storm
$ groups storm
storm : xmen superhero preetygirl

Scroll to Top