Debian – How to add the only user on a Debian system back into the sudo group

debiangroupnot-root-usersudousers

I'm using Debian on a headless server that I'm connecting to via ssh.

When I installed Debian, I did not create a root account, so the first user created was in the sudo group. Then I added that user to another group, but in my ignorance I used:

sudo usermod -G NewGroup UserName

instead of:

sudo usermod -aG NewGroup UserName

Without the 'a' (append), this removed the user from every group (including sudo) other than the default UserName group.

I've learned my lesson now, but is there any way to regain superuser access to this installation?

This is a test environment with nothing to backup or recover, so I can simply reformat if all is lost, but I thought I'd ask anyway.

Best Answer

Assuming you have physical access to the server, you can either create a live medium which has a headless boot routine including an ssh-server to be started and then access the server via these ssh-credentials or - the simpler approach - in case you have a monitor and a keyboard available, plug them in and simply boot into the system.

Then mount the original hard drive and edit the /etc/group file accordingly (i.e.: sudo:x:<integer_number>:<username>).

As a hint for the future: IMHO having an active root account (i.e. password is not locked) is not necessarily a security risk - especially if you restrict ssh-access for root.

Related Question