How to Change the Username in Ubuntu?
Tags: ubuntu
Published: 2021-12-01 16:31:07

---
title: How to Change the Username in Ubuntu?
published: true
date: 2021-11-25 16:57:02 UTC
tags: Ubuntu
canonical_url: https://stackallflow.com/ubuntu/how-to-change-the-username-in-ubuntu/
---

<header>
<p><span style="font-size: 1.2em; color: initial;">keyrings</span><span style="font-size: 1.2em; color: initial;">permissions</span><span style="font-size: 1.2em; color: initial;">user-profile</span></p>
</header>

Some time ago, when I installed Ubuntu, I chose a rather stupid username for my account that I do not want to use anymore.

How do I change this (including the name of my home directory, and the name in the terminal) without losing settings for applications?  
How do I keep permissions and my keys for various authentification (e.g. email, SSH, GPG and more)?  
What settings could possibly get lost if I changed my username?

#### Accepted Answer

Unix-like operating systems decouple the user name from the user identity, so you may safely change the name without affecting the ID. All permissions, files, etc are tied to your identity (uid), not your username.

To manage every aspect of the user database, you use the `usermod` tool.

To change username (it is probably best to do this without being logged in):

```
sudo usermod -l newUsername oldUsername

```

This however, doesn’t rename the home folder.

To change home-folder, use

```
sudo usermod -d /home/newHomeDir -m newUsername

```

after you changed the username.

For instance, you could logout, drop to a console (<kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>F1</kbd>), and `sudo su -` to become true root (as opposed to `sudo -s`, where $HOME is still /home/yourname.) Maybe you also have to kill some still running processes from this user first. To do so, enter `ps -u username`, look for the matching PID and kill them by `kill PID-number`.

Update: as arrange mentioned, some files may reference your old home directory. You can either keep a symlink for backward compatibility, e g `ln -s /home/newname /home/oldname` or you can change the file contents with `sed -i.bak 's/*oldname*/*newname*/g' *list of files*` It creates a backup for each file with a .bak extension.

Some additional information for not so experienced users like me:  
As I only have ONE user account (administrator), it would not let me change the username (“you are already logged in” was the response in TTY1 (<kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>F1</kbd>). To get around this:

1. Login with your old credentials and add a new user, e.g. “temporary” in TTY1:
```
sudo adduser temporary

```

set the password.

2. Allow the temporary user to run sudo by adding the user to sudo group:
```
sudo adduser temporary sudo

```
3. Log out with the command `exit`.
4. Return to tty1: Login with the ‘temporary’ user account and password. Change your username and folder as mentioned above. `exit` (until you get the login prompt)
5. Go back to TTY7 (<kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>F7</kbd>) to login on the GUI/normal desktop screen and see if this works.
6. Delete temporary user and folder:
```
sudo deluser temporary
sudo rm -r /home/temporary

```

The post [How to Change the Username in Ubuntu?](https://stackallflow.com/ubuntu/how-to-change-the-username-in-ubuntu/) appeared first on [Stack All Flow](https://stackallflow.com).