It happens. For numerous reasons, someone in your company might legally change their name. That can become an administrative nightmare if you don’t know what to do. Fortunately, if you’re a Linux administrator you do.
What if you’re new to the game of Linux and need to change a username? It’s not that challenging. I’m going to walk you through the process of making such a change. I’ll demonstrate on Ubuntu Server, but the process should be the same across most Linux distributions.
The only assumption this will make is that you have access to a user with sudo privileges (and a username that must be changed). With that said, let’s make some magic.
SEE: Securing Linux policy (Tech Pro Research)
Creating a test user
For the purpose of learning, we’re going to create a test user account. Open a terminal (or log into your Linux server) and issue the command:
sudo adduser testaccount
The above command will create the new user, a group, a home directory, copy the necessary files from /etc/skel, prompt you to type a password for the user, and answer a few optional questions (Figure A).
Altering the user account
Let’s change the testaccount username to haversham. To do this, issue the command:
sudo usermod -l haversham testaccount
At this point, the username has changed. However, the home directory associated with the username is still testaccount. To change that, we issue the command:
sudo usermod -d /home/haversham -m haversham
If you issue the command ls /home/ you should see the home directory now reflects the new username (Figure B).
Finally, the users’ group name must be changed from testaccount to haversham. To do this, issue the command:
sudo groupmod -n haversham testaccount
Testing the account
Before you log out, you should test the account by SSHing into the server with the new username. Upon successful login, you should find yourself in /home/haversham. You can further test this by creating a test file (to ensure the user has write privileges in their home directory). Issue the command touch test. If you receive no error (Figure C), all is well, and you can hand over the renamed account to the user.
Removing the test account
There’s no reason to leave that test account in place. To remove it you need to delete both the user and the associated group. This can be done with the following two commands:
sudo deluser -r haversham sudo delgroup haversham
Good to go
Your user account was successfully changed. Now, when a user requests that their username be altered, you have the tools to make it happen.