Changing the username on a Linux system can seem a bit tricky at first. After all, there are tons of application settings, system services, file permissions, and configuration files that rely on the username in order to function smoothly. Suddenly changing the name can have adverse and unexpected effects on your system, and will cause certain things to stop working if you do not manage to change the username using the proper method.
What’s more is that it is very common to have a home directory full of files and application settings. Changing this directory to match your new username can cause all of your settings to be reset, if the already installed applications do not know where to find the new location that stores all of the settings. Obviously, it is ideal to pick a username for your system that you can stick with indefinitely, but we are not completely out of luck if you find yourself in a situation where you absolutely must change the username.
In this tutorial, we will go over the step by step instructions to change a username on Ubuntu Linux, along with the user’s home directory, while making sure not to lose any existing application settings. The end result is that your system will not have any leftover evidence that the previous username ever existed, and everything should be smoothly transitioned over to the new username.
In this tutorial you will learn:
- How to change a username on Ubuntu Linux
- How to change a user’s home directory
- How to transfer all application settings to new directory via symbolic link
|Category||Requirements, Conventions or Software Version Used|
|Software||usermod, groupmod, ln|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
How to Change Ubuntu Username and Keep Application Settings
Before continuing, make sure you have a full system backup in case things go awry. Changing a Linux username and home directory is generally not recommended, as it can cause problems with configuration files that directly reference either the username or home directory, rather than an environment variable. Note that we were able to successfully change the username of the main user on our Ubuntu test system. However, a full backup is still recommended, and your success rate may vary depending on your current system configuration and what programs you have installed.
Follow along with the step by step instructions below to get your Ubuntu username and home directory changed. For our example, we will be changing username
- Firstly, it is important to know that you can’t change the username of the currently logged in user. With this in mind, make sure that you log in to some other user, or directly to the root user before proceeding. Even if logged into some other user, note that you will still need root (sudo) permissions. See our tutorial on creating a new user account if you need to first create a secondary user.
- We will use the usermod command and the
-loption to change our user account name from
$ sudo usermod -l newuser linuxconfig
- Since Linux always creates a new group with a matching name whenever a new user is created, we should also use the
groupmodcommand to change the group name:
$ sudo groupmod -n newuser linuxconfig
- Next, we will change the home directory associated with our renamed user. We will once again use the
usermodcommand to accomplish this, but with the
-doption. We will also add the
-moption to specify that we need to create the new directory in case it does not already exist:
$ sudo usermod -d /home/newuser -m newuser
Notice that we do not specify the path to our old home directory. The
usermodcommand already knows where to find the current home directory, and will change it to the new path accordingly.
What about my application settings?
All of your application settings (configuration files) will still be in the renamed home directory. As for other settings, things should transfer over smoothly thanks to environment variables.
- In case some files have hard coded your old home directory in their configuration, we can create a symbolic link from the old home directory to the new one. This will ensure that everything remains compatible.
$ sudo ln -s /home/newuser /home/linuxconfig
- Lastly, reboot your system for all of the changes to take effect:
In this tutorial, we saw how to change the username and home directory for a user on an Ubuntu Linux system. We also ensured that all application settings were not erased when we made the change, by changing the username and group name with the
usermodcommand, and then creating a symbolic link from the old home directory to the new one. This way, any applications or configuration files that pointed to the old home directory will still be able to seamlessly find the files that they rely on to operate.