I can’t access the root account anymore: user is not in the sudoers file

rootsudosystem-recovery

Accidentally removed my gnome desktop and am stuck in tty1. Trying to reinstall the desktop and keep getting the error "user is not in the sudoers file". Having trouble adding my user, new to debian and am not sure how to correctly open/edit sudoers from tty1 (or really how to edit files from cmd line for that matter if someone could please lend some specifics)!

Best Answer

To edit the sudoers file, you first need to be root. My guess, from this question, is that you have not enabled root access for your installation, correct?

If that's the case, you need to change that:

  1. From the bootloader prompt, navigate to the regular entry you boot from.
  2. Instead of pressing Enter press e to first edit the kernel command line parameters
  3. In the editor that opens, use the arrow keys to find the line that starts with linux, go to its end and append init=/bin/sh to it. This will replace your init system temporarily by your system's shell and since init is started by root, this will be a root shell.
  4. Press CTRL+X to boot the modified entry
  5. Once you have a root shell, you usually need to remount the root filesystem read/write instead of read-only. Enter the command mount -o remount,rw / to do that.
  6. You can enable root access by entering a password for root: use the passwd command for this.
  7. Usually you edit files in text mode using a text mode editor like vim or emacs, neither of which Debian ships by default. What Debian does ship by default is a lightweight version of vim called vim.tiny.
  8. In the particular case of the sudoers file, you should edit it using visudo because it does syntax checking for you before writing the file. If you follow this procedure for enabling the root account, you shouldn't need to add your regular user to the sudoers file.
  9. With enabled root access use reboot to boot normally (the edits in step 3 are temporary and vanish on reboot). When faced with tty1, you can then log in as root (with the password you created in step 6) and reinstall your desktop.

Note

As noted by @jthill in the comments below, you may opt out of enabling root access altogether and grant your regular user sudo (this is arguably more secure). In that case, instead of using passwd to give your root account a password, you should use:

usermod -aG regular_user_name sudo

This is likely to only work on Debian (and its derivatives) as it has the entry:

%sudo ALL = (ALL) ALL

which basically grants unfettered sudo access to all members of the sudo group. The usermod command above is meant to add your regular user to the sudo group. The presence of the above line is not guaranteed for other distros. Some distros use a group called wheel instead of sudo for that purpose, for example.

Related Question