How I Setup an Ubuntu Server

2024-06-07

I have setup a lot of servers over the years. Here is how I setup an Ubuntu server. Quick and simple 🤘

When you first create a new Ubuntu server, you should perform some important configuration steps as part of the initial setup. These steps will increase the security and usability of your server and will give you a solid foundation for subsequent actions.

# Login as root

ssh root@your_server_ip

# Create new User

adduser sammy

# Add your user to the sudo group

usermod -aG sudo sammy

# Setup Firewall

ufw allow OpenSSH
ufw allow https
ufw enable

If you have setup the firewall accordingly the output of ufw status should look like the following.

ufw status

# output
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
443                        ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
443 (v6)                   ALLOW       Anywhere (v6)

# Add SSH Key to .ssh/authorized_keys

In order to access the server as non-root user you need to add your public ssh key to the .ssh/authorized_keys of the user you created above.

# Prevent Login with Password / Force SSH Key Pair Authentication

Open the file /etc/ssh/sshd_config as root and set the following values.

PasswordAuthentication no
PubkeyAuthentication yes

After you changed the ssh configuration you need to restart the SSH service.

# Prevent Login as root

Open the file /etc/ssh/sshd_config as root and set the following values.

PermitRootLogin no

After you changed the ssh configuration you need to restart the SSH service.

# Restart SSH Service

sudo systemctl restart sshd

# Don’t ask for Password when using sudo

Open the file /etc/sudoers with root with sudo visudo and add the following to the end of the file.

YOUR_USER_NAME ALL=(ALL) NOPASSWD: ALL

# Sources