How to Change the Username on Raspberry Pi
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.
- Open a terminal on your Raspberry Pi or SSH into it.
- Run the following command to create a new user. Replace
newuser
with your desired username:
sudo adduser newuser
-
Follow the prompts to set a password and fill in the user details.
-
Add the new user to the
sudo
group:
sudo usermod -aG sudo newuser
Step 2: Switch to the New User
- Log out from the current session or open a new terminal.
- Log in with the new user:
su - newuser
Step 3: Change the Default Username
- Open a terminal as the new user.
- 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
.
- Change the username and home directory of the
pi
user. Replacenewuser
with your new username andpi
with the current username you are changing:
sudo usermod -l newuser -m -d /home/newuser pi
- 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:
- Change the group name:
sudo groupmod -n newuser pi
Step 5: Verify the Changes
- Check the home directory:
ls /home
Ensure that the new home directory /home/newuser
exists and contains the files from the old user.
- Check the username:
whoami
Ensure that it returns the new username.
- 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.