Verified and Tested 05/9/15

Introduction

With any server, the primary goal should always be security. Many users are victims of malicious infiltrations on their servers due to the lack of security boundaries established from the beginning. Let us begin on the right path by laying our foundation with security. This how-to will help you with your initial setup on Ubuntu 15.04 so that you can successfully secure your server while giving you peace of mind knowing your server is protected.

What Do You Need?

You need a Ubuntu 15.04 server configured with a static IP address. If you do not have a server already, you can visit our industry-leading VPS Hosting page and spin a new server up in under 30 seconds.

Server Preparation

To get started, log in to your Ubuntu 15.04 server via SSH or the VNC Console located here. Atlantic.Net Cloud servers are set up as minimal installations to avoid having unnecessary packages from being installed and never used. If some software packages that you’re used to using aren’t installed by default, feel free to install them as needed.

Let’s make sure that your server is fully up-to-date.

Update the Ubuntu 15.04 Root Password

By default, your Atlantic.Net servers are automatically set with secure passwords. However, we still recommend updating your password after creating your server and every 60-90 days after that to ensure it remains secure. A minimum of 8 characters, including lowercase, uppercase, and numbers, are recommended to increase the level of security.

Type the following command to activate your request and follow the on-screen instructions to update/confirm your root password:

passwd

Create a new user with sudo privileges

After successfully updating your password, it’s recommended that you create a new user with sudo/root permissions. Since the standard admin user for many Linux Operating Systems like Ubuntu 15.04 is “root,” we’re going to make a new admin user that will be used for day-to-day administration tasks. Creating a new user with root permissions will increase the security of the way your server is accessed.  Individuals looking to target your system will target the root user because it’s known to be the default admin account. By creating a new user with sudo/root permissions and disabling the root user, you are increasing security by using a user that isn’t already publicly known.

Type the following command to create your new user replacing “user1” with your username, and confirm.

adduser user1

Fill in the information that applies to the user and confirm the information:

root@JVHServer:~# adduser user1
Adding user `user1' ...
Adding new group `user1' (1000) ...
Adding new user `user1' (1000) with group `user1' ...
Creating home directory `/home/user1' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for user1
Enter the new value, or press ENTER for the default
 Full Name []: user1
 Room Number []:
 Work Phone []:
 Home Phone []:
 Other []:
Is the information correct? [Y/n] y

Create a password for that user by typing the following command to activate your request and following the on-screen instructions to update/confirm your “user1” password:

passwd user1

Once you have created a new user and created the password for that user, it is time to add the user to the sudo wheel group. In Ubuntu 15.04, once you add them to the sudo wheel group, they are automatically assigned sudo/root permissions. Run the following command to add the user to the sudo wheel group.

adduser user1 sudo

Finally, when you have created the user with sudo/root permissions,  you can exit your session and log back in with your “user1” to verify the changes made. Alternatively, you can run the following command and switch users from root to “user1,” which will ask you for that user’s password. It is important to remember that you will have to use sudo before running any command with the user.

su - user1

Configure SSH Access

Port 22 is the default port for remote connections via SSH in Linux systems. By changing the ssh port, you will increase the security of your server in preventing brute force attacks and unwanted users from reaching your server using the default port. For this tutorial, use Port 5022 as an example.

Open your SSH Configuration file, find the Port line, remove the # and change 22 number to your Custom port. Save and exit.

sudo nano /etc/ssh/sshd_config
# What ports, IPs and protocols we listen for
Port 5022

For your system to update the new settings from the SSH Configuration file, we must restart the sshd service.

sudo systemctl restart sshd.service

SSH has now been configured to use Port 5022, and if you attempt to login using Port 22, your login will fail. However, do not exit your session as we need to configure the custom port on the firewalls configuration part first, which we will configure in the upcoming steps.

Limit Root Access

Since we’ve created a new user with root permissions and made a custom ssh port, there’s no need to keep the actual root user available and vulnerable over SSH on your server. Let us restrict the root user’s access to the local server and grant permission to the new user over SSH only.

Open the SSH Configuration file, find the PermitRootLogin line, remove the # and change it from yes to no.

sudo nano /etc/ssh/sshd_config
#PermitRootLogin yes
PermitRootLogin no

For your system to update the new settings in the SSH Configuration file, we must restart the sshd service.

sudo systemctl restart sshd.service

Note: You will now have the root user disabled in making those changes, so you must log in to your server with the “user1” that you created. However, do not exit your session. We must configure the custom port on the firewall in the upcoming steps.

Create a Private SSH Key

Private/Public SSH Keys are great additional features that increase security in a server’s method. However, it takes a bit more effort to set up. The question is, Is your server worth the extra security? If you want to implement the following security features, you can continue with the next steps. Let us proceed and generate the SSH Key.

ssh-keygen

If you want to change the location where the SSH Key will be saved,  you can specify it here. However, the default location where it’s stored should be OK. Press enter when prompted with the following question, then enter a passphrase, unless you don’t want one.

Enter file in which to save the key (/home/user1/.ssh/id_rsa):
This is the default page when installing SSH Keys on a Ubuntu 15.04 server

This is the default page when installing SSH Keys on a Ubuntu 15.04 server

Configuring the SSH Key is crucial. We must copy the entire key string to a Word/ Notepad Document. The key can be viewed in the following location with the cat command.

cat ~/.ssh/id_rsa.pub

Copy the SSH key, beginning with ssh-rsa and ending with user1@yourserver, into a Word/ Notepad document to add it to the config file later on.

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDNcnc3pMOytM7xgwZHUX0Wi/O78hDvLUmQVtKn8Qk1ijBY82jVftKXKRhf8toNkgm9jaZmfapLa7ynzUG4jbFjyy8H+iEs1R8P7eu+e8/fmGwjorbMFuWdoi7h2CldoqKTdMEb/dMNxjNMzBbovl3XtZviQGm4/TMVO1hHxy85JR8zAtNFu7liaP7IonexNrOGhY8CjqRcBsceQLkl1MlX7cWaWAMqd6jQnAvggTLerI9P286AP8Sk4uColj7GKOljj8X6J/2pcjp9v2IvJOqwC/zLwUKZ6qTEV6SrfdbjopoCVvpXkVhmcbHX5Xv1gwynO+vTkpPFwVTjSnAai71L jose@JVHServer

Once the SSH Key is stored safely, the directory for the SSH Keys needs limited permissions that only the owner can read, write and execute the file.

sudo chmod 700 .ssh

Within the SSH directory, a file containing the SSH Key must be added, simply using your editor (in this case NANO) the following location:

nano .ssh/authorized_keys

Paste the SSH Key, then save and exit using the nano format.

Finally, we have to limit the permissions of the authorized_keys file that we just created so only the owner can read and write.

chmod 600 .ssh/authorized_keys

We could now verify that the key is working by closing the session and typing [email protected] or your server’s hostname in your SSH Console. Furthermore, you can click “here” to see our How To Generate and Use SSH Keys article for more information.

Basic Firewall Rules

By default, your Atlantic.Net’s Ubuntu 15.04 Server is not loaded with a firewall. However, depending on your preference, you may install any of the following: firewalld, iptables, etc. In this part, I will be using Firewalld, which uses the firewall-cmd tool to configure its rules. We must first install the Firewall service with the following:

sudo apt-get install firewalld

Next, we’re going to add a rule to allow the custom SSH Port 5022 that was created earlier to access the server publicly. At the same time, we will remove the previous default rule allowing SSH access on TCP/22.

sudo firewall-cmd --permanent --add-port=5022/tcp
sudo firewall-cmd --permanent --remove-service=ssh

If you have a web server, you may want to allow the following rules to access your sites over the internet.

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https

You can use this for similar rules related to any systems you want to be publicly accessible. This test server is set up to host a website and email services, so in addition to the updated SSH  and HTTP(S) rules, additional rules for secure POP3, IMAP, and SMTP have been added.

sudo firewall-cmd --permanent --add-service=pop3s
sudo firewall-cmd --permanent --add-service=imaps
sudo firewall-cmd --permanent --add-service=smtp

For Firewalld to accept those settings, you must restart the firewall.

sudo firewall-cmd --reload

Your settings will have been saved, and you are ready to proceed. To verify all the services/ports that are available, run the following:

sudo firewall-cmd --permanent --list-all
This is the default page after configuring the Firewalld rules on Ubuntu 15.04 server

This is the default page after configuring the Firewalld rules on Ubuntu 15.04 server

NTP Time Sync

The NTP (Network Time Protocol) is used to synchronize the time and date of computers over the network to remain accurate and up to date. Let us begin by installing the NTP(If it hasn’t been installed) and configuring the service to synchronize with their servers.

sudo apt-get install ntp

Once the NTP service is installed, we need to ensure that the service is ON.

sudo /etc/init.d/ntp start

Add Swap File

A Swap file is simply a small space created on a server hard drive to simulate Ram. If the server is running low on memory, it will look at the hard drive and ease the load tricking the system into thinking it has more memory. We will set up the swap file on the hard drive to increase the server’s performance just a little bit more.

Begin by checking your resources to make sure we can add the file. When you run the following command, you will see the percentage space on your Hard drive that is currently being used.

df -h

When creating a Swap file, you usually want to add half of your existing RAM up to 4GB(If you have 1GB of actual Ram, you add a 512MB file). In this part, I will be adding a 512MB swap file to the drive. The way that this is calculated is by 1024 x 512MB = 524288 block size.

sudo dd if=/dev/zero of=/swapfile bs=1024 count=524288

Now that we have added a swap file, an area needs to be created to proceed.

sudo mkswap /swapfile

With the Swap file created and the Swap file, area added we could go ahead and add permissions to the file so that only the owner can read and write.

sudo chown root:root /swapfile
sudo chmod 600 /swapfile

Now that the swap file has the appropriate permissions, we can go ahead and activate it.

sudo swapon /swapfile

You can verify your newly added Swap file with the following.

sudo swapon -s

To make the Swap file always active even after a reboot, we must configure it accordingly.

sudo nano /etc/fstab

Paste the following command at the bottom of the file, save your work, and exit.

/swapfile              swap   swap     defaults     0 0

Finally, verify if your swapfile is activated by typing the following command:

free -m
This is the default page after creating a Swap File on a Ubuntu 15.04 Server

This is the default page after creating a Swap File on a Ubuntu 15.04 Server

What Next?

With that, you now have a server with a strong security foundation, giving you peace of mind knowing that your server is protected. You may now proceed to build your platform according to your needs. Thank you for following along, and feel free to check back with us for further updates.

Atlantic.Net is dedicated to providing the very best in technical support for our customers along with offering full line-ups of VPS hosting solutions for any sized business.