Hi, this is Sandra Henry-Stocker, author of the “Unix as a Second Language” blog on NetworkWorld.
In this Linux tip, we’re going to look at the usermod command. It’s a command that allows you to make a lot of different changes to user accounts without having to edit configuration files like /etc/passwd and /etc/group.
To demonstrate how the command works, I’m going to change someone’s username. I’ll change Joe Doe’s username from “jdoe” to “gijoe”.
Here’s Joe’s account and his groups:
$ grep oe /etc/passwd; groups jdoe
jdoe:x:1017:1017:Joe Doe:/home/jdoe:/bin/bash
jdoe : jdoe adm
This command changes his username to “gijoe”:
New Current
| |
V V
$ sudo usermod -l gijoe jdoe
This command changes the username in the /etc/passwd file and updates the /etc/group file with the new username. To change the home directory to match the new username and change the user’s description fields, I’ll do this:
$ sudo usermod -d /home/gijoe -m gijoe
$ sudo usermod -c “GI Joe” gijoe
To verify that the changes were made:
$ grep gijoe /etc/passwd
gijoe:x:1017:1017:Joe Doe:/home/gijoe:/bin/bash
$ ls -ld ~gijoe
drwxr-xr-x 2 gijoe jdoe 4096 Nov 12 12:12 /home/gijoe
$ groups gijoe
gijoe : jdoe adm
You can’t change users’ groups with usermod unless you’re moving them to an existing group. Otherwise, you would use the groupmod command.
Keep in mind that there are many other types of changes that usermod can help you make — locking and unlocking accounts and more.
That’s your Linux tip for usermod.
previous post