Introduction
In this tutorial, we will have a look at a few important tasks to perform in the server for setting up the server and basic server hardening. These steps will increase the security of your server as well as usability. We will perform a series of tasks such as creating a new sudo user, updating packages, setting timezone and securing SSH servers, etc.
Prerequisites
- Cloud VPS or Dedicated Server with Debian 10 installed.
Log in via SSH
When your server is created Snel sends you an email with the default username, password, and server IP address. For first time login, you need to use those credentials to log in to your server.
If you’re not familiar with how to connect please have a look at our “How to connect to your server with SSH” article.
Change Logged in User Password
Upon the first login, it is very important to change the password of the current user. Use the following command for the same.
passwdIt will ask you to provide your existing password unless you are logged in as the root user.
Create a New Sudo User
If you are logged in as root user, it is recommended to create a sudo user. If you are logged in as sudo user with username in format client_xxxxxx_x, which Snel already created for you, it is still a best practice to create a new sudo user.
A Sudo user is a user having superuser privileges. In simple terms, this user can perform administrative commands and tasks as the root user.
To create a new user, run the following command. You can replace the example username happysnel with anything you like.
sudo adduser happysnelNote: You can omit using sudo command if you are logged in as root user. Provide the required information such as your name and password and a user will be created for you.
client_xxxxx_x@vps:~$ sudo adduser happysnelAdding user `happysnel' ...Adding new group `happysnel' (1001) ...Adding new user `happysnel' (1001) with group `happysnel' ...Creating home directory `/home/happysnel' ...Copying files from `/etc/skel' ...New password: Retype new password: passwd: password updated successfullyChanging the user information for happysnelEnter the new value, or press ENTER for the default Full Name []: Happy Snel Room Number []: Work Phone []: Home Phone []: Other []: Is the information correct? [Y/n] yAdd your newly created user to the sudo group. Users in the sudo group are sudo user in Debian 10.
sudo usermod -aG sudo happysnelLogging in as the Newly Created User
Exit from the current terminal session by running the logout command and log in again using ssh as the new user.
ssh [email protected]192.168.0.1 is an example IP address.
Disable Root Login via SSH
Find the current setting for root login via SSH by running the following command.
sudo cat /etc/ssh/sshd_config | grep PermitRootLoginYou might see the following output.
[happysnel@vps ~]$ sudo cat /etc/ssh/sshd_config | grep PermitRootLoginPermitRootLogin without-password# the setting of "PermitRootLogin without-password".As in the above output, it is set to without-password. It means that password authentication is disabled, however, public-key authentication is enabled. Which is fine in most cases. Make sure, it should not be commented out or should not be set to yes.
To completely disable root login, edit the file by running the following command.
sudo nano /etc/ssh/sshd_configAnd change the line to the following.
PermitRootLogin noSave the file and restart the SSH server by running the following command.
sudo systemctl restart sshdNow, if you will try to login as the root user, it will not let you in.
Update Your Server
It is important to install the latest security patches and updates to your server. Run the following command to update the local package lists.
sudo apt-get updateNow, update the packages.
sudo apt-get -y upgradeNote: If you get any prompts saying an updated package or file is available, but the installed version is modified. Choose keep the local version currently installed option.
Setting timezone
You may want your server in the same timezone as you. Run the following command to get a list of available timezones.
timedatectl list-timezonesThe list of available timezones is also available here.
Once you have identified your timezone, set it using the following command.
sudo timedatectl set-timezone Europe/AmsterdamYou can confirm the timezone by running the following command.
timedatectlSet Hostname
Check your existing hostname by running the following command.
hostnamectlYou should see a similar output.
happysnel@vps:~$ hostnamectl Static hostname: vps.snelexample.site Icon name: computer-vm Chassis: vm Machine ID: cfc70e29ed8440108dfa33dd59160dc9 Boot ID: 3ccca50362244cf7a001d1c45d41223f Virtualization: kvm Operating System: Debian GNU/Linux 10 (buster) Kernel: Linux 4.19.0-5-amd64 Architecture: x86-64To set a hostname, run the following command.
sudo hostnamectl set-hostname host.snelexample.siteReplace host.snelexample.site with your actual hostname. Preferably, it should be an FQDN(Fully Qualified Domain Name). But, if you are not sure if you want to add an FQDN, a label to identify the server also works.
To resolve the hostname in your local server, you will need to add it to /etc/hosts file. Edit the hosts file by running the following command.
sudo /etc/hostsAppend your hostname at the end of the line that starts with 127.0.0.1. For example.
127.0.0.1 localhost host.snelexample.siteConfigure a Firewall
Debian 10 does not come with a default firewall installed. You can install UFW(Uncomplicated Firewall) by running the following command.
sudo apt-get -y install ufwFirst, deny access to all incoming traffic by running the command.
sudo ufw default deny incomingAlso, allow access to all outgoing traffic by running the command.
sudo ufw default allow outgoingNow, that our default policy is created, let’s allow default SSH port 22 through the firewall. Run the command.
sudo ufw allow 22You can also run :
sudo ufw allow sshAs UFW knows that the default port for SSH is 22.
Now, that all our rules are in place, start the UFW firewall by running.
sudo ufw enableIt will ask you for the confirmation, as enabling UFW may disrupt the existing SSH connection. You can go ahead and press y as we already opened port 22 through the firewall. You will get a similar output.
happysnel@host:~$ sudo ufw enableCommand may disrupt existing ssh connections. Proceed with operation (y|n)? yFirewall is active and enabled on system startupYou can view the status of the firewall by running the command.
sudo ufw statusYou should see a similar output.
happysnel@host:~$ sudo ufw statusStatus: active To Action From-- ------ ----22 ALLOW Anywhere 22/tcp ALLOW Anywhere 22 (v6) ALLOW Anywhere (v6) 22/tcp (v6) ALLOW Anywhere (v6)SSH Port Change (Optional)
Malicious bots on the internet continuously target the default SSH port 22. You can change it to any other port so that your server is not a victim of continues attacks of bots on port 22. To change the SSH port, open the SSH configuration file again by running the following command.
sudo nano /etc/ssh/sshd_configFind the line which says
#Port 22Uncomment it and change it to any port you like between 1024 to 65535.
Eg.
Port 2200Save the file and exit from the editor.
Open port 2200 from the firewall by running the command.
sudo ufw allow 2200Since, we no longer need port 22 open, block it using the command.
sudo ufw delete allow 22sudo ufw delete allow sshNow, restart the SSH server by running the following command.
sudo systemctl restart sshdAlso, reload the UFW firewall rules by running the command.
sudo ufw reloadNow, if you will try to login from another terminal without specifying a port, it will not let you in. Modify the SSH command to log in to include the port number.
ssh -p 2200 [email protected]192.168.0.1 is an example IP address.
Reboot
Now that we have gone through updating the packages and configuring the server. Give the server a reboot so that if there are any pending changes, it will be applied.
sudo rebootConclusion
In this tutorial, we have learned how to set up a sudo user on newly created Debian 10 instances. We configured hostname, time zone and updated the packages. We also secured our SSH server from basic break-ins.
Spot a mistake in this article? Email us at support@snel.com
This article was last checked on 17 April 2023. · 24,410 views

