The day I removed myself from sudoers group… and how I fixed it [Ubuntu]

I was following an online course and the instructor was talking about how to add the current user to the docker group, so you wouldn’t need to execute docker with sudo, and he did this:

sudo usermod -G docker myusername

Children, don’t try this at home.

I copied the command without thinking and BAM! I got removed from the sudoers list. This was good because I instantly learned two things:

1. Don’t copy+paste stuff without understanding it.

2. Think carefully before executing any command with sudo.

After reading a lot of Stack Exchange posts, this is how I fixed it on my Ubuntu 18.04.

Restart the computer and hold SHIFT during boot, you’ll get the GRUB menu.

Select Ubuntu***(recovery mode).

On the next menu, choose the root mode shell. Be careful with this, since it will execute any command as root. Use it to add yourself back to sudoers:

adduser myusername sudo

At this part, I got a message saying that the user didn’t exist. I checked that the username was correct by running:

ls /home

And since I didn’t know any better way to solve it, I recreated the user, setup its password and added it to the group.

useradd myusername -m -s /bin/bash
passwd myusername
adduser myusername sudo

And then to restart the computer:

shutdown -r now

When the Ubuntu GUI was displayed and I tried to login, I got stuck on a loop. This happened because when I did the useradd with the existing username (that Ubuntu was saying it didn’t exist), it changed the ownership of my user’s home to root.

To fix this, I restarted the computer, went back to recovery mode and on the root shell I changed back the directory ownership to my user:

chown myusername:myusername -R /home/myusername

After this just restart the computer again and be happy that you didn’t have to format your computer because of an internet tutorial!