change the default user on raspberry pi

How To Change The Username On Raspberry Pi

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

Get My Commands Cheat Sheet!
Grab your free PDF file with all the commands you need to know on Raspberry Pi!
  • 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.

Get My Commands Cheat Sheet!
Grab your free PDF file with all the commands you need to know on Raspberry Pi!

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.

Get My Commands Cheat Sheet!
Grab your free PDF file with all the commands you need to know on Raspberry Pi!

If you are looking for exclusive tutorials, I post a new course each month, available for premium members only. Join the community to get access to all of them right now!

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.

Get My Commands Cheat Sheet!
Grab your free PDF file with all the commands you need to know on Raspberry Pi!

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.


This tutorial doesn't work anymore? Report the issue here, so that I can update it!

Similar Posts

4 Comments

  1. 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

    1. Yes, you are right.
      The goal in this article was just to explain how to do it, not why ­čÖé

  2. 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.

    1. Good comment about the sed command, didn’t think of that :/
      And yes, for sudo, I could add it in the linked article.
      Thanks!

Comments are closed.