Search code examples
linuxraspberry-pisudo

Removed Sudo access from only user by changing UID and GUID


I mistakenly changed my UID and GUID of the active (and only) user on my raspberry pi server by changing the UID and GUID directly in the /etc/passwd file. I am connecting only with ssh as it is a headless server.

I had changed the UID and GUID from 1000 to 0 after misunderstanding some instructions and just generally being in a rush. I have no other users and cannot ssh into the server as root.

Now, when I try to use sudo

$ sudo ...
sudo: you do not exist in the passwd database

and if I try whoami

$ whoami
whoami: cannot find name for user ID 1000

If I read the /etc/passwd file then I can still see my user with the new UID and GUID

$ cat /etc/passwd | grep <user>
<user>:x:0:0:,,,:/home/<user>:/bin/bash

I don't have any privileged access and can only act as the user, I don't want to logout so as not to lose the session and lose being able to login again


Solution

  • It's probably borked and you'll need physical access to recover it - pop out the SD card, mount it, and undo your UID/GID changes

    mkdir tmp
    sudo mount /dev/sdXZ tmp  # disc X and partition Z are dependent on system
    # edit tmp/etc/password   # be cautious this is not your host system!