A new server is like an open land area where you will build the empire. As we all know, to make a building last long, we need a strong foundation. Here in this guide, we will do the initial server setup with Ubuntu, which will add a strong foundation for our server. Whether you are working on a personal server or a server for a big firm, initial server configuration is the most important thing, as it will add a layer of security and make the server reliable.
When you think of a new server, remember that it’s just a PC with an Operating System (Ubuntu in our case). So, it’s our duty to install the required applications and configure them, as these applications will allow or deny access.
In this tutorial, we will do the following
Prerequisites
- A new Ubuntu server
- Root access via SSH
Step 1 – Login to the server with SSH
The first step is to login to our Ubuntu server using SSH. You can use CMD (for Windows) or terminal (for linux) or you can install PuTTY.
With root’s password
Some hosting providers use key-based login, and some use the root’s password. If your hosting provider has provided root’s password, then you can just open CMD or terminal and enter the below command.
# ssh root@your_server_ip
Note:- Make sure to change "your_user_ip" with your server's IP
With SSH key
If your hosting provider has provided you the SSH key then the login steps are given below
- Open Putty
- On the left menu, click on “SSH”, then click on “Auth” then click on “Credentials”
- Add the ssh key under “Private key file for authentication”
- Click on “Session” which is the first option given on the left side
- Enter your server IP in “Host Name (or IP address) field
- Enter “22” in “Port” field
- Click “Open”
- Just enter your root’s password and hit “Enter” key
With this, you have successfully connected to your server.
Step 2 – Create Sudo User
Always remember, root user is the God of the server as it has access to everything. It has high privileges and can perform any actions without any restrictions. Since we have to do a lot of work on the server, it would be very dangerous to work with the “root” user. A sudo user is the best way to manage a server, as they have privileges to manage things. Follow the steps below to create a sudo user
Execute the below command to create a new user first
# adduser username
Now let’s assign “sudo” privileges to the user we have created
# usermod -aG sudo username
Note: make sure to change "username" to the username you want to create for ex: ashish
Step 3 – Installing and configuring UFW firewall
Although there are many firewalls available with Ubuntu’s “Ufw” works best, and hence we will install and configure the same. Follow the steps below
# apt update -y
# apt upgrade -y
# apt install ufw -y
This will install UFW firewall on your server. Now let’s configure it. Execute the commands given below, which will allow only the OpenSSH, 80, and 443 ports, which are the most common protocols needed for our server to work.
# ufw allow OpenSSH
# ufw allow 80,443
# ufw enable
# ufw status
With this, we have configured our firewall successfully
Step 4 – Change SSH port
By default, SSH service works on port 22, and this is known to everyone. So, it’s an easy door for the hacker to try accessing port 22 of your server. Hence, to add another layer of security, let’s change the port to something else. Since most of the services use ports from 1 to 1024, for the safest way, let’s use 2020. You can use any port between 10.24 and 65535. Follow the steps below to change the SSH port
Open SSH configuration file
# nano /etc/ssh/sshd_config
Find the line
port 22
Comment the line by adding a # in front of it and add the new line below it. It should look like this
#port 22
port 2020
Save the file by pressing “ctrl+x”, “y”, hit “Enter.”
Make sure to update the firewall as well using the command given below.
# ufw allow 2020
# ufw reload
Note: change port as per your need. You can choose any of the port between 1024 to 65535
Now let’s restart the SSH service so the changes will take effect
# sudo systemctl restart sshd
Now, try connecting to your server using the new port.
Conclusion
With this, we have completed our initial server setup with Ubuntu. Now, the server is ready with the rest of the configuration.