How to Change the Username on Raspberry Pi

Ben
Ben
@benjislab

The default username for Raspberry Pi OS is "pi", which is common knowledge and can be a security risk. Changing the default username to something unique can enhance the security of your Raspberry Pi. This guide will walk you through the steps to change the username on your Raspberry Pi.

Prerequisites

Before starting, ensure you have the following:

  • Raspberry Pi (any model)
  • MicroSD card with Raspberry Pi OS installed
  • Stable internet connection
  • Access to the command line (via monitor and keyboard or SSH)

Step 1: Create a New User

First, create a new user with sudo privileges. This new user will be used to change the default "pi" user.

  1. Open a terminal on your Raspberry Pi or SSH into it.
  2. Run the following command to create a new user. Replace newuser with your desired username:
sudo adduser newuser
  1. Follow the prompts to set a password and fill in the user details.

  2. Add the new user to the sudo group:

sudo usermod -aG sudo newuser

Step 2: Switch to the New User

  1. Log out from the current session or open a new terminal.
  2. Log in with the new user:
su - newuser

Step 3: Change the Default Username

  1. Open a terminal as the new user.
  2. Stop any services that might be using the pi user:
sudo systemctl stop <service_name>

Replace <service_name> with the name of any services running under the pi user, such as cron or ssh.

  1. Change the username and home directory of the pi user. Replace newuser with your new username and pi with the current username you are changing:
sudo usermod -l newuser -m -d /home/newuser pi
  1. Change the display name (optional):
sudo chfn -f "New User Full Name" newuser

Step 4: Update Group Names (Optional)

If you want to change the group name associated with the old username, you can do so with the following command:

  1. Change the group name:
sudo groupmod -n newuser pi

Step 5: Verify the Changes

  1. Check the home directory:
ls /home

Ensure that the new home directory /home/newuser exists and contains the files from the old user.

  1. Check the username:
whoami

Ensure that it returns the new username.

  1. Restart any services that were stopped:
sudo systemctl start <service_name>

Step 6: Clean Up (Optional)

You can remove the old user's directory if it still exists and you're sure you no longer need it:

sudo rm -rf /home/pi

Conclusion

Changing the default username on your Raspberry Pi enhances its security and personalization. By following these steps, you can easily change the username from "pi" to something unique. This simple change helps protect your Raspberry Pi from unauthorized access and makes it better suited for your specific needs.