How to Modify an Existing User in Linux

How to Modify an Existing User in Linux

Hey there, welcome to ProgramGuru.org! 👋 Today, we’re going to learn how to modify an existing user in Linux using the usermod command. If you’ve ever needed to change a username, move the user’s home directory, or update their group—this lesson is for you!

Let’s say we already have a user on our system, and now we want to change some of their details. We’ll walk through the most common modifications step by step.

🔹 1. Change a User’s Username

Let’s change the username from john to johnny:

sudo usermod -l johnny john
username 'john' is now changed to 'johnny'

Note: This doesn’t rename the home directory. It just changes the login name.

🔹 2. Rename the User’s Home Directory

If you’ve changed the username, it’s a good idea to rename the home directory too. Here’s how:

sudo usermod -d /home/johnny -m johnny
home directory is now /home/johnny and all files are moved

-d specifies the new home directory and -m moves the files.

🔹 3. Change a User’s Primary Group

Let’s assign the user to a different primary group, say developers:

sudo usermod -g developers johnny
user 'johnny' is now part of the primary group 'developers'

🔹 4. Add the User to Additional Groups

Want to give the user access to other groups like sudo or docker?

sudo usermod -aG sudo,docker johnny
user 'johnny' has been added to groups: sudo, docker

Important: Always use the -a (append) flag when adding to groups. Without it, the user will be removed from all other groups!

🔹 5. Lock or Unlock a User Account

If you need to temporarily disable a user without deleting them:

sudo usermod -L johnny   # Lock
sudo usermod -U johnny   # Unlock
account 'johnny' is now locked/unlocked

✅ Summary

Here's what we learned today:

  • How to change usernames
  • How to move and rename home directories
  • How to update primary and secondary groups
  • How to lock or unlock accounts

And that’s it! 🎉 You’ve now mastered the basics of modifying users in Linux. Keep practicing and you’ll feel like a pro in no time. Don’t forget to check out the next topic in our Linux course on user permissions.

Thanks for learning with ProgramGuru.org!