Create sudo user on Linux
Before provisioning any Linux server, usually some kind of admin user is needed to perform the initial setup (i.e. with Ansible)
To create a sudo user:
- Connect to VPS
ssh [email protected]_ip_address
- Create a new user:
adduser username --force-badname
(--force-badname
flag will raise an error if your name contains forbidden or problematic characters) - Add new user to the
admin
group:adduser username admin
-
Modify
sudoers
file (withvisudo
, to prevent saving incorrect file) so users ofsudo
andadmin
groups can login without password:# Members of the admin group may gain root privileges %admin ALL=(ALL) NOPASSWD:ALL # Allow members of group sudo to execute any command %sudo ALL=(ALL:ALL) NOPASSWD:ALL
-
Add your SSH key to authorized keys:
sudo su - username
mkdir ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 644 ~/.ssh/authorized_keys
vim ~/.ssh/authorized_keys
- Logout and login as a new user to test the setup