How can I add a new user as sudoer using the command line?
Just add the user to the sudo
group:
sudo adduser <username> sudo
The change will take effect the next time the user logs in.
This works because /etc/sudoers
is pre-configured to grant permissions to all members of this group (You should not have to make any changes to this):
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
As long as you have access to a user that is in the same groups as your "original" user, you can delete the old one.
Realistically, there are also other groups your new user should be a member of. If you set the Account type of a user to Administrator in Users Settings, it will be placed in at least all of these groups:
adm sudo lpadmin sambashare
Because your system configuration may vary, I suggest taking a look at the output of groups <username>
to see what groups are normally in use.
I did
sudo usermod -a -G sudo <username>
as recommended here.
Open the sudoers file: sudo visudo
will open the /etc/sudoers
file in the editor defined in $EDITOR
(probably GNU nano - set the variable if it's not what you want, eg export EDITOR="nano"
and try sudo visudo
again).
Add the below line to the end of the file.
username ALL=(ALL) ALL # Change the user name before you issue the commands
Then perform WriteOut with Ctrl + O. The editor will ask you for the file name to write into. The default will be a temporary file that's used by visudo
to check for syntax errors before saving to the actual sudoers
file. Press Enter to accept it. Quit the nano editor with Ctrl + X.
Done!