As you know, any Raspberry Pi installation starts with a default username (pi) and the same password (raspberry).
In my other tutorials, I always recommend changing the password first.
Today, I’ll also address the way to change the default username.
The Linux command usermod can be used to change the default Raspberry Pi username (pi). This command is used to change all the parameters for a Linux account, including its name. But there are a few prerequisites to do this safely and efficiently.
In this article I’ll show you three methods to change the “pi” user with something more pertinent. The first method is by using the usermod command, the second is by changing a bunch of files manually, and the third is by simply creating a new user.
I tested all three methods while writing this tutorial, so you can find one that works best for you :).
Change the Pi Username with usermod
The “few prerequisites” mentioned in the introduction is that it’s not possible to change the username directly from the Pi session. To change the username, you have to be logged in with another user, and the Pi session should be closed.
The method I prefer is to use the root user, but you can also use another user if you prefer (check the last part of this tutorial to create one).
Disable the auto login
By default, Raspberry Pi OS Desktop will open a session with “pi” automatically on boot.
The first thing we need to do is to disable this default, or there will always be a process running for this user, and we’ll get the following error:
usermod: user pi is currently used by process 621
- Go to the main menu.
- Open the Raspberry Pi Configuration tool, under Preferences.
- In the first tab, click “Disabled” beside “Auto Login”:
If you are on Raspberry Pi OS Lite, you can skip this step, as there is no auto login for you :).
This step is not mandatory, but I prefer doing almost everything from my computer. And I want to explain you every possibles scenario, so you are not lost in this process :).
If you want to use SSH, you need to enable it in the same tool.
Go into interfaces and enable SSH.
We need another user with privileges to change the default username, and the most natural way to do this is to use root.
You first need to enable it by setting a password :
- You need to open a session with pi.
At this step you can use SSH, a terminal on Desktop or just open a session on the Lite version.
- Type the command:
- This will set a password for root, allowing us to log in with it.
If you want to change the default username with SSH, you also need to enable root access to SSH :
- Open the SSH configuration file:
sudo nano /etc/ssh/sshd_config
- Find the line:
- Replace it with:
- Save and exit.
- Restart the SSH service to apply changes:
sudo service ssh restart
For security reasons (as explained here), it isn’t a good idea to keep root access enabled. So, you might consider reversing this change once you create your new username.
Anyway, we are ready to change the pi username now.
Change the default username
- Log in with root.
You can use the method you prefer depending on your installed version (open a session or use SSH).
- In a terminal or SSH session, type the following command:
usermod -l <new_user> pi
usermod -l patrick pi
- Rename the home directory.
We can do this with:
usermod -m -d /home/<new_user> <new_user>
usermod -m -d /home/patrick patrick
Now try to log in with your new username. It should work – no need to reboot.
The password for this new user will be the same as for the pi user, so probably “raspberry” if you didn’t change it.
From there, you might want to revert previous changes, like enabling the auto-login and disable the SSH access for root.
You can also disable the root user completely with:
sudo passwd -l root
Change the default username manually
The previous method follows the best practices, but is a bit long with many steps and commands that might be hard to remember.
If you don’t care about using best practices, I have a quick a dirty method for you :).
The idea is to change all the users files, replacing “pi” with your new username.
You still need to be in root to do this safely:
- Open a terminal or SSH session and switch to root:
It can be inside a pi session, no problem with this.
- Copy and paste the following commands:
sed -i s/pi/<new_user>/g /etc/passwd
sed -i s/pi/<new_user>/g /etc/shadow
sed -i s/pi/<new_user>/g /etc/group
sed -i s/pi/<new_user>/g /etc/sudoers
sed -i s/pi/<new_user>/g /etc/gshadow
mv /home/pi /home/<new_user>
I highly recommend creating a backup before doing this the first time – at least for the edit files. If you have a lot of stuff installed, a full copy of your SD card is probably a good idea.
The sed command is useful to replace one word with another in a file. You can find it in the “expert” commands listed in this article.
Create a new user
Most of the time, the method I use on a fresh installation is this simple one:
- Log in with the “pi” user on first boot (automatic).
- Configure the basic options with it.
- As soon as you are ready, create a new user in command line.
- Add the sudo privileges to it.
- Login with the new username.
- Delete the pi username.
I find this the easier method when you have nothing on the default session.
The other methods might be more useful if you have many files or a configured program you want to keep. But at the beginning, creating a new user seems perfect.
If you are interested in this, I already have a topic on the subject here.
It explains in details all the steps listed above.
You can also join an active directory domain with your Raspberry Pi and log in with the domain users.