Tutorial: Initial server setup with Ubuntu 22.04

We will show you how to properly configure a new server with Ubuntu 22.04 and thus create a good basis for later installations.

When creating or setting up an Ubuntu 22.04 server for the first time, there are some important configuration steps to perform. This way, the security and usability of the server can be increased and a solid foundation for later activities can be created.

Step 1️⃣ – Logging in as root

 

For this step you need the public IP address of your server and the password or private key for the root user account (if you have installed an SSH key for authentication).

If you are not currently connected to your server, log in as the root user with the following command: ssh root@your_server_ip .
Simply replace the placeholder “your_server_ip” with the public IP address of your server.

Attention: Due to the increased privileges, we do not recommend using the root account on a regular basis.

If a host authenticity warning appears, accept it. If your server uses password authentication, enter your root password to log in. If you are using an SSH key that is protected by a passphrase, you may need to enter the passphrase the first time you use the key per session. If you are logging in to the server for the first time using a passphrase, you may also need to change the root password. Follow the instructions to change the password when you receive a prompt to do so.

Step 2️⃣ – Creating a new user

 

Once you have logged in as root, you can create a new user account with limited privileges for everyday use. Use the following command to do this: adduser yourname
Simply replace the placeholder “yourname” with the desired user name.

You will then be asked a few questions, including the need to enter a password for the new account. All other information that is asked here is optional and can be skipped as needed.

Step 3️⃣ – Granting of administrative authorizations

 

To avoid always having to log in when necessary, you can set up so-called superuser or root privileges for your newly created normal user account. This allows your normal user to execute commands with administrative privileges by placing the word sudo in front of the command.
Simply add your new user to the sudo system group with the usermod -aG sudo yourname command. Simply replace the placeholder “yourname” with the username you chose in step 2. Members of the sudo group are authorized to use the sudo command by default on Ubuntu 22.04.

Note: Until you verify that you can log in with your new user and use sudo (in step 5), we recommend that you remain logged in as root.

Step 4️⃣ – Setting up a firewall

 

Ubuntu 22.04 servers can use the UFW firewall to ensure that only connections to specific services are allowed. You can use this application to set up a simple firewall.

Applications can register their profiles with UFW during installation. These profiles allow UFW to manage these applications by name. OpenSSH, the service that allows you to connect to your server, has a profile registered with UFW. You can view the list of installed UFW profiles with the ufw app list command.

Use the ufw allow OpenSSH command to ensure that the firewall allows SSH connections, which will allow you to log in to your server next time.

Now enable the firewall with the following command: ufw enable

Geben Sie Y ein und drücken Sie ENTER, um fortzufahren. Use the ufw status command to check if SSH connections are still allowed.

Caution: The firewall now blocks all connections except SSH. If you install and configure additional services, you must adjust the firewall settings to allow the new traffic on your server.

Step 5️⃣ – Enable external access for your regular user

 

Now that you have a regular user for daily use, you need to ensure that you can log in to this account directly via SSH. The configuration of SSH access for your new user depends on whether your server’s root account uses a password or SSH key for authentication.

If you have logged in to your root account with a password, password authentication is enabled for SSH. You can apply SSH to your new user account by opening a new terminal session and using SSH with your new username: ssh yourname@your_server_ip. Simply replace the placeholder “yourname” with the username you chose in step 2, and replace the placeholder “your_server_ip” with the public IP address of your server.

After you enter your normal user’s password, you are logged in. Remember, if you need to run a command with administrator privileges, type sudo before it, for example: sudo command_to_run

The first time you use sudo in each session (and periodically thereafter), you are prompted to enter your regular user’s password. To increase the security of your server, we recommend setting up SSH keys instead of password authentication.

If you have logged in to your root account with SSH keys, password authentication for SSH is disabled. To log in as a normal user with an SSH key, you must paste a copy of your local public key into the ~/.ssh/authorized_keys file of your new user. You can copy the file and directory structure from your root account.

The easiest way to copy the files with the correct owners and permissions is to use the rsync command. This command copies the root user’s .ssh directory, retains the permissions, and changes the file owners-all in a single command: rsync --archive --chown= yourname: yourname ~/.ssh /home/ yourname. Simply replace the placeholder “yourname” with the username you chose in step 2.

Now open a new terminal session on your local machine and use SSH with your new username: ssh yourname@your_server_ip. Replace the “yourname” placeholder again with the user name you chose in step 2, and replace the “your_server_ip” placeholder with the public IP address of your server.

After you enter your normal user’s password, you are logged in. Remember, if you need to run a command with administrator privileges, type sudo before it, for example: sudo command_to_run

The first time you use sudo in each session (and periodically thereafter), you will be prompted to enter your regular user’s password.

 

Next steps for Tutorial: Initial server setup with Ubuntu 22.04

Now you have created a solid foundation for your server. You can now install any software you need on your server. Learn more.