1. MagicESP
  2. Ubuntu Server 22.04
  3. sudo without password

sudo without password

Some ubuntu server comes with only root account. I recommend create a ubuntu account for everyday use and use sudo to do root jobs. We do not even need to set and remember any password for the ubuntu account, because we normally login this account using the SSH keys file.

create {{ username.value }} account

sudo adduser --gecos "" --disabled-password {{ username.value }}

sudo without password prompt (using command line)

echo "{{ username.value }} ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/90-cloud-init-users
sudo chmod 0440 /etc/sudoers.d/90-cloud-init-users

sudo without password prompt (using editor)

sudo {{ texteditor.value }} /etc/sudoers.d/90-cloud-init-users

add the following lines

# User rules for {{ username.value }}
{{ username.value }} ALL=(ALL) NOPASSWD:ALL
sudo chmod 0440 /etc/sudoers.d/90-cloud-init-users

how to switch to another user

sudo -u {{ anotheruser.value }} -i

run as another user

sudo -u {{ anotheruser.value }} {{ anotheruser_command.value }}