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
If you are lost in all these new words and abbreviations, request my free Raspberry Pi glossary here (PDF format)!
Prerequisites
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.
Sale: 10% off today.
Take it to the next level.
I'm here to help you get started on Raspberry Pi.
Learn all the skills you need in the correct order.
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 :).
Enable SSH
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.
Enable root
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:
sudo passwd
- 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:
#PermitRootLogin prohibit-password
- Replace it with:
PermitRootLogin yes
- 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
For example:usermod -l patrick pi
- Rename the home directory.
We can do this with:usermod -m -d /home/<new_user> <new_user>
For example:usermod -m -d /home/patrick patrick
Tests
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.
Sale: 10% off today.
Download the eBook.
Uncover the secrets of the Raspberry Pi in a 30 days challenge.
Learn useful Linux skills and practice multiples projects.
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:
sudo su
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>
reboot
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.
Warning: As explained by Alex in the comments, it’s not a good idea to use these commands if you use other usernames containing “pi” in their name (spi, gpio). Adjust the filter depending on the existing users, or do it manually to be completely safe.
Reminder: Remember that all my Patreon supporters get access to this website without ads, early access to my videos and much more. You can become part of this community for as little as $3 per month & get all the benefits immediately.
Note about USB devices
Grab your free PDF file with all the commands you need to know on Raspberry Pi!
Just a quick note here, submitted by Ingo.
If you follow the procedure to rename pi to another user name, you may have an issue when pluggin usb drives.
Sale: 10% off today.
Get the eBook.
Do more with your Raspberry Pi, learn the useful concepts and take the shortcuts.
You miss half of the fun of using a Raspberry Pi if you don’t know anything about Python.
By default, Raspberry Pi OS will automatically mount USB drives to /media/<username>/<devicename>.
But in your case, the folder /media/<username> won’t exist.
To fix this, you can either create it or rename it.
In a command line, something like:sudo mkdir /media/<username>
should do the trick.
More details here about USB devices management on Raspberry Pi.
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.
- Reboot.
- 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.
nice one – thx
but I lost the purpose of changing the username
if for security then on the 1st place: disable root login and forbid ssh as root and 2nd: disable ssh password login – just use the key for login
Yes, you are right.
The goal in this article was just to explain how to do it, not why 🙂
The sed variant is a bad idea as there are /etc/group entries named “spi” and “gpio”, both which would be mangled with the new user name: s<new_user>
What’s also missing is /etc/sudoers.d/010_pi-nopasswd which enables passwordless sudo for the “pi” user. There’s probably more.
Good comment about the sed command, didn’t think of that :/
And yes, for sudo, I could add it in the linked article.
Thanks!