Skip to content

Instantly share code, notes, and snippets.

@JeremyIglehart
Last active February 8, 2023 18:36
Show Gist options
  • Save JeremyIglehart/84251d8b6405eaa640d6546b2a1ae8bc to your computer and use it in GitHub Desktop.
Save JeremyIglehart/84251d8b6405eaa640d6546b2a1ae8bc to your computer and use it in GitHub Desktop.
How to create a new user and remove the `pi` user on a Raspberry Pi

How to create a new user and remove the pi user on a Raspberry Pi

Note: This has been tested in Rasbian Buster released on 2019-06-20

Important: replace jeremy below with whatever username you'd like as your new user.

  1. sudo adduser jeremy
    • You will be asked to enter a password
    • When filling out basic information about the user, you may leave it blank
    Adding user `jeremy' ...
    Adding new group `jeremy' (1001) ...
    Adding new user `jeremy' (1001) with group `jeremy' ...
    jeremy ALL=(ALL) NOPASSWD: ALL
    Creating home directory `/home/jeremy' ...
    Copying files from `/etc/skel' ...
    New password:
    Retype new password:
    passwd: password updated successfully
    Changing the user information for jeremy
    Enter the new value, or press ENTER for the default
      Full Name []: Jeremy Iglehart
      Room Number []:
      Work Phone []:
      Home Phone []:
      Other []:
    Is the information correct? [Y/n] Y
  2. sudo adduser jeremy sudo
    Adding user `jeremy' to group `sudo' ...
    Adding user jeremy to group sudo
    Done.
  3. sudo cp /etc/sudoers.d/010_pi-nopasswd /etc/sudoers.d/010_jeremy-nopasswd
    • If you don't like typing in the sudo password all the time, copy this file.
  4. sudo chmod u+w /etc/sudoers.d/010_jeremy-nopasswd
    • Add write permissions (so that you can change it)
  5. sudo vi /etc/sudoers.d/010_jeremy-nopasswd
    • Change "pi" to "jeremy"
  6. sudo chmod u-w /etc/sudoers.d/010_jeremy-nopasswd
    • remove write permissions (should be 0440 or -r--r-----)
  7. sudo reboot
    • To allow all settings to go into effect. You may get away with something else here - but rebooting does the trick. Also it ensures that the pi user is fully logged out and has nothing left running.
  8. Login as your new user
  9. sudo deluser -remove-home pi
    Looking for files to backup/remove ...
    Removing user `pi' ...
    Warning: group `pi' has no more members.
    Done.
    If you try to do this before rebooting you will see something like this:
    Looking for files to backup/remove ...
    Removing files ...
    sh: 0: getcwd() failed: No such file or directory
    Removing user `pi' ...
    Warning: group `pi' has no more members.
    userdel: user pi is currently used by process 702
    /usr/sbin/deluser: `/usr/sbin/userdel pi' returned error code 8. Exiting.
    
  10. sudo rm -vf /etc/sudoers.d/010_pi-nopasswd
    removed '/etc/sudoers.d/010_pi-nopasswd'
    

Done.

also note, on a fresh system where you haven't set any locale you will see this a lot:

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = (unset),
	LC_CTYPE = "en_US.UTF-8",
	LANG = "en_GB.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_GB.UTF-8").

Credit: https://raspi.tv/2012/how-to-create-a-new-user-on-raspberry-pi So, I first tried a few things outlined in this article above and found out quickly that the way Raspberry Pi is setting up the pi user is different than in this article, hence writing this new gist. After some trial and error I ended up settling on the method I describe here in this gist. I'd like to give credit to what got me started on this path.

@badbonez
Copy link

badbonez commented Feb 29, 2020

I followed all these steps, and created a new user but when I get to Step 8: I rebooted and logged in as my new user, tried to delete the pi user and got the error:

"userdel: user pi is currently used by process 543"

What happens is that the Raspberry Noobs version logs in the pi user automatically. I had to go to the Preferences>Raspberry Pi Configuration menu and untick the box "Login as Pi," reboot, login with the new user then I was able to delete pi user. In case someone else has this issue.

@mokrunka
Copy link

mokrunka commented Jul 6, 2020

I followed all these steps, and created a new user but when I get to Step 8: I rebooted and logged in as my new user, tried to delete the pi user and got the error:

"userdel: user pi is currently used by process 543"

What happens is that the Raspberry Noobs version logs in the pi user automatically. I had to go to the Preferences>Raspberry Pi Configuration menu and untick the box "Login as Pi," reboot, login with the new user then I was able to delete pi user. In case someone else has this issue.

This was really helpful. I had the exact same issue, and doing your idea fixed it.

@koma-xrd
Copy link

koma-xrd commented Feb 2, 2021

if you are connected via ssh you can simply sudo raspi-config > system options > boot / auto login and choose the option

@MartyLake
Copy link

Thanks @mkoetzing for saving my day !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment