Changing the Default Username and Password on Raspberry Pi

The default install of a Raspberry Pi will create a user with the username “pi” and the password “raspberry”. If you are only accessing this machine via it’s hardware interfaces (e.g. you have a keyboard plugged in) then this isn’t terrible but as soon as the machine is available to be logged in remotely (even if it’s just on your private network) you should change the username and password.

I’m going to assume that you want to perform this operation on a “headless” Pi and you’ll be shelling in to it using SSH. The instructions are basically the same for a remote machine just don’t shell into it.

Shell into the target machine and enter the command:

sudo passwd root

This will set or change the password for the root user. Generally you don’t want anyone directly logging in as root so we’ll disable this again later.

Now allow root to shell into the machine. It’s important this is disabled later. While it might be acceptable to allow root to login locally under some circumstances it’s never OK to allow remote root logins. From your “Pi” shell do the following:

  • Edit the file sshd_config: sudo nano /etc/ssh/sshd_config
  • Locate the commented out line: #PermitRootLogin prohibit-password
  • Uncomment the line by removing the leading #
  • Change the line to: PermitRootLogin yes
  • Save the file: ctrl-o
  • Exit the file: ctrl-x
  • Restart sshd service: /etc/init.d/ssh restart

Open a new terminal on your local machine and shell in to the target machine again but as root this time. If you can log in successfully then exit your “Pi” shell on the target machine, you can’t change the account name while it’s active.

In the root shell enter the following command to change the name of the “Pi” user to “foobar”:

usermod -l foobar pi

Move / create a home director for the renamed user:

usermod -m -d /home/foobar foobar

This is all that is strictly required. You might also want to change the password for the user but that is for another guide. For now open a new terminal window on your local machine and shell into the target machine with the new username.

At the regular user prompt check that you can view all the files you expect. If everything looks good exit the root shell. Undo the changes you made above to the sshd_config file, this is important! Check root can’t shell in.

Finally, block root logins completely but running the command:

sudo passwd -l root

You’re all done.