How To Change The Username On Raspberry Pi

1. Log in to the Raspberry Pi using the current username and password.

2. Open the terminal window and type in the command “sudo passwd root”.

3. Enter the current password for the user.

4. Enter the new username.

5. Enter the new password twice.

6. Type in the command “sudo usermod -l newusername oldusername”.

7. Type in the command “sudo usermod -d /home/newusername -m newusername”.

8. Type in the command “sudo groupmod -n newusername oldusername”.

9. Type in the command “sudo chown -R newusername:newusername /home/newusername”.

10. Reboot the Raspberry Pi.

11. Log in to the Raspberry Pi using the new username and password.

On a fresh installation of Raspberry Pi OS, you now have the choice of the username (before that it was always “pi”). But if you want to change it later on, there are a few steps to know to not break anything. Today, I’ll address how you can safely change a username.

The Linux command usermod can be used to change a username on Raspberry Pi. This command is used to change all the parameters for a Linux account, including its name. But there are a few prerequisites to be aware of to ensure you do this safely and efficiently.

In this article, I’ll show you three methods to change the user name. 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 use the one that works best for you.

If you’re looking to quickly progress on Raspberry Pi, you can check out my e-book here. It’s a 30-day challenge where you learn one new thing every day until you become a Raspberry Pi expert. The first third of the book teaches you the basics, but the following chapters include projects you can try on your own.

Change the Pi Username with usermod

Prerequisites

The most important “prerequisite” to have in mind is that it’s not possible to change the username directly from the same session. To change the username, you have to be logged in with another user, and the user session should be closed.

The method I prefer is to use the root user (the administrator account), 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 the default user 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 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 probably skip this step, as there is no auto login by default.

Enable SSH

This step is not mandatory, but I prefer doing almost everything from my computer. And I want to explain to you every possible scenario, so you are not lost in the process.

If you want to use SSH, you need to enable it in the same tool.
Go into interfaces and enable SSH.

Don’t know what I’m talking about? Check this guide first: Use SSH To Remote Control Your Raspberry Pi: A complete guide

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 the default user.
    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 are new to this, I have more details in this article: How to Easily Log In as Root on Raspberry Pi OS.

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 username now.

Are you a bit lost in the Linux command line? Check this article first for the most important commands to remember, and a free downloadable cheat sheet so you can have the commands at your fingertips.

Change the default username

We’ll now connect to the Raspberry Pi via SSH (or just via the terminal if you are using RPI OS Lite):

  • Log in with root.
    You can use the method you prefer depending on your installed version (open a session or use SSH).
  • Then ype the following command:
    usermod -l <new_user> <current_username>
    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 without needing to reboot.
The password for this new user will be the same as for the old user.

From there, you might want to revert previous changes, like enabling the auto-login and disabling 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 and dirty method for you.

The idea is to change all the user’s files, replacing the old username 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
    Check the previous part if you need more guidance, I explained every step.
    In this case, it can be inside the main user session, no problem with this.
  • Copy and paste the following commands:
    sed -i s/<old_user>/<new_user>/g /etc/passwd
    sed -i s/<old_user>/<new_user>/g /etc/shadow
    sed -i s/<old_user>/<new_user>/g /etc/group
    sed -i s/<old_user>/<new_user>/g /etc/sudoers
    sed -i s/<old_user>/<new_user>/g /etc/gshadow
    mv /home/<old_user> /home/<new_user>
    reboot

I highly recommend creating a backup before doing this the first time – at least for the edited 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 the old username in their name (ex: replacing “pi” with “pat” will also replace “gpio” with “gpato”…).
Adjust the filter depending on the existing users, or do it manually to be completely safe.

Note about USB devices

Just a quick note here, submitted by Ingo.
If you follow this procedure to rename the default user, you may have an issue when plugging in USB drives.

By default, Raspberry Pi OS will automatically mount USB drives to /media/<username>/<devicename>.
But in this 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 device management on Raspberry Pi.

Want to chat with other Raspberry Pi enthusiasts? Join the community, share your current projects and ask for help directly in the forums.

Create a new user

Another option is simply to create a new user, copy the data you need to the new session, and delete the old one after that.

In summary, you can follow these steps:

  • Log in with the default user (automatic).
  • Create a new user in the command line.
  • Add the sudo privileges to it.
  • Reboot.
  • Login with the new username.
  • Optional: copy your personal files to the new home folder.
  • Remove the old user.

I find this the easier method when you have nothing important 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 in 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 detail all the steps listed above.

You can also join an active directory domain with your Raspberry Pi and log in with the domain users.

Additional Resources

Not sure where to start?
Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.
Watch the Raspberry Pi Bootcamp course now.

Master your Raspberry Pi in 30 days
Don’t want the basic stuff only? If you are looking for the best tips to become an expert on Raspberry Pi, this book is for you. Learn useful Linux skills and practice multiple projects with step-by-step guides.
Download the e-book.

VIP Community
If you just want to hang out with me and other Raspberry Pi fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.

Need help building something with Python?
Create, understand, and improve any Python script for your Raspberry Pi.
Learn the essentials step-by-step without losing time understanding useless concepts.
Get the e-book now.

You can also find all my recommendations for tools and hardware on this page.

How To Change The Username On Raspberry Pi

Changing the username on your Raspberry Pi is a simple process that can be done in a few steps. This guide will walk you through the process of changing the username on your Raspberry Pi.

Step 1: Log in to your Raspberry Pi

The first step is to log in to your Raspberry Pi. You can do this by connecting to your Raspberry Pi via SSH or by connecting a monitor, keyboard, and mouse to your Raspberry Pi.

Step 2: Open the Terminal

Once you are logged in to your Raspberry Pi, open the terminal. You can do this by clicking on the terminal icon in the top left corner of the screen.

Step 3: Change the Username

Once the terminal is open, type the following command to change the username:

sudo usermod -l new_username old_username

Replace new_username with the new username you want to use and old_username with the current username.

Step 4: Log Out and Log Back In

Once you have changed the username, log out of your Raspberry Pi and log back in with the new username. You should now be able to access your Raspberry Pi with the new username.

Conclusion

Changing the username on your Raspberry Pi is a simple process that can be done in a few steps. This guide has walked you through the process of changing the username on your Raspberry Pi.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?