Basic Tutorial to Secure an Ubuntu VPS

Secure Ubuntu VPS
Secure Ubuntu VPS

Securing your Ubuntu VPS is essential for protecting data, ensuring stability, and preventing unauthorized access. Here’s a straightforward guide on some basic yet effective steps to secure an Ubuntu VPS.


1. Update Your System

Start by updating your system to ensure all software is up-to-date with the latest security patches.

sudo apt update && sudo apt upgrade -y

2. Create a New User and Disable Root Login

For security, avoid using the root account directly and create a new user with sudo privileges.

  1. Create a new user: sudo adduser yourusername
  2. Add the user to the sudo group: sudo usermod -aG sudo yourusername
  3. Switch to the new user: su - yourusername
  4. Disable root login by editing the SSH configuration file: sudo nano /etc/ssh/sshd_config Find the line:
   PermitRootLogin yes

Change it to:

   PermitRootLogin no
  1. Restart SSH to apply changes:
    sudo systemctl restart ssh

3. Enable Firewall (UFW)

Ubuntu’s Uncomplicated Firewall (UFW) provides a straightforward way to manage firewall settings.

  1. Allow SSH access: sudo ufw allow OpenSSH
  2. Enable the firewall: sudo ufw enable
  3. Check the status:
    sudo ufw status

Optionally, if you’re hosting a web server, allow HTTP and HTTPS traffic:

sudo ufw allow http
sudo ufw allow https

4. Change the Default SSH Port

Changing the SSH port can add an additional layer of security against automated attacks.

  1. Open the SSH configuration file: sudo nano /etc/ssh/sshd_config
  2. Find the line: #Port 22 Uncomment and change 22 to your desired port, e.g., 2222: Port 2222
  3. Restart SSH to apply changes: sudo systemctl restart ssh
  4. Don’t forget to allow the new SSH port through the firewall:
    bash sudo ufw allow 2222/tcp

5. Disable Password Authentication and Enable SSH Key Authentication

Using SSH keys instead of passwords enhances security.

  1. Generate an SSH key pair on your local machine: ssh-keygen -t rsa -b 4096
  2. Copy your public key to your VPS: ssh-copy-id -p 2222 yourusername@your_server_ip
  3. Disable password authentication for SSH: sudo nano /etc/ssh/sshd_config Find the line: PasswordAuthentication yes Change it to: PasswordAuthentication no
  4. Restart SSH:
    sudo systemctl restart ssh

6. Install Fail2ban

Fail2ban monitors login attempts and blocks IPs with repeated failures, protecting against brute-force attacks.

  1. Install Fail2ban: sudo apt install fail2ban -y
  2. Start and enable Fail2ban: sudo systemctl start fail2ban sudo systemctl enable fail2ban
  3. Configure Fail2ban by creating a local jail file: sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
  4. Modify settings as needed: sudo nano /etc/fail2ban/jail.local You can adjust the ban time, retry limits, and monitored services.
  5. Restart Fail2ban:
    sudo systemctl restart fail2ban

7. Install and Configure Automatic Updates

Automatic updates reduce the risk of security vulnerabilities by ensuring software remains current.

  1. Install the unattended-upgrades package: sudo apt install unattended-upgrades -y
  2. Enable automatic updates:
    sudo dpkg-reconfigure --priority=low unattended-upgrades

8. Regular Backups

Always keep regular backups to quickly recover in case of an attack or data loss. Many hosting providers, like Hosteons, offer backup solutions, making it easy to automate and restore from snapshots or backups.


Summary

By following these steps, you enhance the security of your Ubuntu VPS against common threats. Regular updates, secure login configurations, a robust firewall, and monitoring tools like Fail2ban all contribute to a safer and more reliable server environment. With these basics covered, your VPS will be better protected against potential attacks.

VPN Setup Guide – One click L2TP VPN Server or IPSEC VPN Server Setup

In this video you will learn how to setup your own private L2TP to IPSEC VPN Server using a KVM VPS or a Dedicated Server. I’ve used a Ubuntu 20 based installation but this same script or command even works on Debian, this is a fully automated setup for a VPN Server. I’ve used a Budget KVM VPS from https://hosteons.com/kvm_vps.php for this tutorial but this same script should work with any other providers’s VPS as long as it’s a KVM VPS and the OS is Debian or Ubuntu. This is a one click or one command setup to have your VPN Server ready almost instantly.

Command to install your L2TP VPN server is:

wget https://git.io/vpnsetup -O vpnsetup.sh && sudo sh vpnsetup.sh

NOTE: Please make sure to backup your data before running this script as hosteons will not be responsible for any data loss.

Fix macOS Catalina broken Mail Search

This post is not related to hostEONS Hosting Services, but a very simple fix for macOS Catalina broken mail search.

Since most of our techs are using MAC and as soon as we are upgraded to Catalina we were surprised to find out that SpotLight or mail search was not displaying results from mails.

So here is how we fixed it.

Quit Apple Mail

Go to System Preferences

Now Click on Spotlight

Then Click on Privacy Tab

Now click on “+” and add whole Macintosh HD, it will give you warning that you are about to disable Spotlight search, proceed with it.

Now restart your MAC

Now again to System Preferences -> Spotlight -> Privacy Tab now select Macintosh HD and click on minus “-” sign and close System Preferences

Now you start using Mail in your Mac and give it couple of hours, it will reindex your whole drive including mails and all mails will be searchable again.

Logging or Showing Real IP in cPanel/Apache when using CloudFlare

We recently switched to CloudFlare for our own website but even after a lot of research could not find proper documentation moreover mod_cloudflare has been discontinued and all documents are too old, so here is how we fixed it.

  1. First login to WHM
  2. Customise your Apache config and under Apache Modules select mod_remoteip (yes we will use mod_remoteip instead of mod_cloudflare)
  3. Once Apache is recompiled now you need to edit a file with name “370_mod_remoteip.conf” it may be 360 or 350 or something else but last part will be “mod_remoteip.conf” and file is available in /etc/apache2/conf.modules.d.
  4. Just add following to above file:

RemoteIPHeader X-Real-IP
RemoteIPTrustedProxy 173.245.48.0/20
RemoteIPTrustedProxy 103.21.244.0/22
RemoteIPTrustedProxy 103.22.200.0/22
RemoteIPTrustedProxy 103.31.4.0/22
RemoteIPTrustedProxy 141.101.64.0/18
RemoteIPTrustedProxy 108.162.192.0/18
RemoteIPTrustedProxy 190.93.240.0/20
RemoteIPTrustedProxy 188.114.96.0/20
RemoteIPTrustedProxy 197.234.240.0/22
RemoteIPTrustedProxy 198.41.128.0/17
RemoteIPTrustedProxy 162.158.0.0/15
RemoteIPTrustedProxy 104.16.0.0/12
RemoteIPTrustedProxy 172.64.0.0/13
RemoteIPTrustedProxy 131.0.72.0/22
RemoteIPTrustedProxy 2400:cb00::/32
RemoteIPTrustedProxy 2606:4700::/32
RemoteIPTrustedProxy 2803:f800::/32
RemoteIPTrustedProxy 2405:b500::/32
RemoteIPTrustedProxy 2405:8100::/32
RemoteIPTrustedProxy 2a06:98c0::/29
RemoteIPTrustedProxy 2c0f:f248::/32

 

5. Now go to WHM and under “Apache Configuration” click on “Include Editor” then under “Pre Main Include” edit the file and add:

<IfModule remoteip_module>
RemoteIPHeader X-Forwarded-For
</IfModule>
<IfModule log_config_module>
LogFormat “%{Referer}i -> %U” referer
LogFormat “%{User-agent}i” agent
LogFormat “%a %l %u %t \”%r\” %>s %b” common
LogFormat “%a %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”” combined
CustomLog logs/access_log combined
</IfModule>

 

Now just save and restart Apache, it should now start showing your real IP in apache logs etc…

 

 

 

Manually Migrate a KVM VPS to another node where both source and destination nodes are running SolusVM

If for some reason SolusVM automated migration is failing or if you just want make your hands dirty and wants to manually migrate a KVM VPS to another VPS Node, just follow these simple steps:

Run following commands on Source VPS Node:

Assuming your KVM ID for your VPS is “kvm5368”

# lvdisplay | grep kvm5368
LV Path /dev/vg_node10/kvm5368_img
LV Name kvm5368_img

Now lets shutdown the Source VPS:

# virsh shutdown kvm5368

Now once it’s shutdown lets create an image of the VPS Disk:

#dd if=/dev/vg_node10/kvm5368_img | gzip | dd of=/root/kvm5368_backup.gz bs=4096

Now lets create a Logical Volume on the destination node, so run following commands on the destination node:

# lvcreate -n kvm5368_img –size 30G /dev/vg_node

Now transfer the VPS image file created earlier from source to destination node:

# scp -C /root/kvm454_backup.gz root@destinationvpsnode:/root/

Once it’s transferred then run following command on destination vps node to restore the VPS Image:

dd if=/root/kvm5368_backup.gz | gzip -d | dd of=/dev/vg_node/kvm5368_img bs=4096

Once it’s restored run following command on your SolusVM Master:

/scripts/vm-migrate <vserverid> <newnodeid>

Now you should be able to boot the new migrated VM from SolusVM

Getting Errors while booting a OpenVZ 7 VPS Container running on Ploop File System ?

Getting errors like:

2019-08-09T08:24:10-0400 : Error in e2fsck (fsutils.c:471): e2fsck failed (exit code 4)

2019-08-09T08:24:10-0400 vzctl : CT 392 : Failed to mount image /vz/private/392/root.hdd: Error in e2fsck (fsutils.c:471): e2fsck failed (exit code 4)
[41]

#vzctl start 392

Starting container…
Opening delta /vz/private/392/root.hdd/root.hdd
Adding delta dev=/dev/ploop61204p1 img=/vz/private/392/root.hdd/root.hdd (rw)
/dev/ploop61204p1p1: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.
(i.e., without -a or -p options)
Error in e2fsck (fsutils.c:471): e2fsck failed (exit code 4)
Failed to mount image: Error in e2fsck (fsutils.c:471): e2fsck failed (exit code 4)

It can happen due to a file system crash or VPS Node crash etc…, don’t worry follow following tutorial to fix.

Solution:

1)Stop the container.

#vzctl stop 392

#vzlist 392

2)Mount the ploop image.

#ploop mount /vz/private/392/root.hdd/DiskDescriptor.xml

3)Perform fdisk -l

#fdisk -l /dev/ploop61204p1

4)Perform a file system check for the partition(note p1 at the end):

#e2fsck /dev/ploop61204p1p1

5)Unmount the ploop image

#ploop umount -d /dev/ploop61204p1

Unmounting device /dev/ploop61204p1

6)Start the container

#vzctl start 392

Beware! Always Ask your VPS Provider About CPU Clock Speed and Drives being used

You may find a a lot of VPS Providers popping up these days and some of them even offering like 2 CPU Cores even with a 512 MB VPS that too very cheap, but always ask the following few basic questions before ordering:

  1. CPU Clock Speed
  2. Kind of Drives Used
  3. Are they using RAID 10
  4. Do they Provide Native IPv6 and is it a /64 if you don’t get a /64 then it’ useless as /64 per customer is the minimum standard for IPv6 because all RBL now block a /64 and not individual IPv6 IP so if another user from same /64 is an abuser even you will be blocked/banned so it’s best to get a separate /64
  5. Is your provider offering unmetered data transfer ?
  6. Are they offering DDOS protection ?
  7. Ask for a test IP and check latency, just ask for Looking Glass if they have any, something like https://lg.hosteons.com

Hosteons.com offers CPU Clock Speed between 2.5Ghz – 3Ghz

Hosteons uses SSD Drives with RAID 10 for best Disk I/O Performance

Hosteons provides Free /64 IPv6 with all VPS

Hosteons VPS comes with unmetered data transfer

Hosteons offers Free DDOS Protection with all VPS Packages

Hosteons has very good routing globally, it can be verified from https://lg.hosteons.com it has test IP as well as other network utilities to check latency and routing.

How to enable iptables in CentOS 7

Just use the following commands to disable firewalld and enable the good old iptables in any CentOS 7 based VPS or server:

[root@test2 ~]# systemctl stop firewalld

[root@test2 ~]# systemctl mask firewalld
Created symlink from /etc/systemd/system/firewalld.service to /dev/null.

[root@test2 ~]# yum install -y iptables iptables-services
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.centos.org
* extras: mirror.centos.org
* updates: mirror.centos.org
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
Package iptables-1.4.21-24.1.el7_5.x86_64 already installed and latest version
Resolving Dependencies
–> Running transaction check
—> Package iptables-services.x86_64 0:1.4.21-24.1.el7_5 will be installed
–> Finished Dependency Resolution

Dependencies Resolved

=========================================================================================================================================
Package Arch Version Repository Size
=========================================================================================================================================
Installing:
iptables-services x86_64 1.4.21-24.1.el7_5 updates 51 k

Transaction Summary
=========================================================================================================================================
Install 1 Package

Total download size: 51 k
Installed size: 25 k
Downloading packages:
iptables-services-1.4.21-24.1.el7_5.x86_64.rpm | 51 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : iptables-services-1.4.21-24.1.el7_5.x86_64 1/1
Verifying : iptables-services-1.4.21-24.1.el7_5.x86_64 1/1

Installed:
iptables-services.x86_64 0:1.4.21-24.1.el7_5

Complete!
[root@test2 ~]#

[root@test2 ~]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@test2 ~]#
[root@test2 ~]# systemctl start iptables