Ubuntu 14.04 LTS Server Setup
Linux, Server, Sysadmin, Ubuntu
This article is a cheatsheet/guidelines for the secure setup of Ubuntu 14.04 LTS server.
The content has been largely drawn from this Digital Ocean article.
Once a new server is created, you should follow these setup guidelines as soon as possible. Check the auth.log file for malicious access attempts:
~~~
sudo cat /var/log/auth.log | grep 'sshd.*Invalid'
~~~Malicious log in attempts begin almost immediately - presumably, would-be hackers are trying a range of IP addresses and knocking on doors, looking for low-hanging fruit. For this reason, the initial VPS setup should definitely include a strong root password.
Set FQDN/Hostname
To set the hostname to “archimedes”, edit /etc/hostname:
echo "archimedes" > /etc/hostname
hostname -F /etc/hostnameIf it exists, edit the file /etc/default/dhcpcd to comment out the SET_HOSTNAME directive.
Edit /etc/hosts
Edit /etc/hosts - amend the localhost entry, and add a new line referring to the FQDN.
This should be in the format: IP-Address-of-system hostname.domainname.TLD hostname
127.0.0.1 localhost.localdomain localhost
123.345.567.78 archimedes.your-main-domain.com archimedesAdd ipV6 if necessary, e.g.
1b00:1b00::f0f0:11aa:aa98:bdf4 archimedes.your-main-domain.com archimedesRestart the hostname service
/etc/init.d/hostname restartSet Timezone
Activate wizard, follow instructions:
dpkg-reconfigure tzdataCreate New User
Create a new user:
adduser daviduserAnswer the questions, add password, hit enter.
Give new user sudo privileges:
gpasswd -a daviduser sudoGenerate SSH Key Pair on Local Machine
You can re-use and existing one if it already exists
ssh-keygen -t rsaAdd a passphrase.
Copy Public SSH Key to Server
The easiest way to do this:
ssh-copy-id daviduser@123.45.56.78You will be prompted for the user’s password…and that’s it!
Reboot Server From Command Line
This is sometimes prompted for/required during the setup process:
sudo rebootHarden SSH Access
Backup config:
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bakRestrict root login so it is only via SSH key (NOT Password):
sudo nano /etc/ssh/sshd_configAmend the PermitRootLogin entry:
PermitRootLogin without-passwordCtrl+o to save, Ctrl+x to exit. Then reload ssh:
sudo reload sshTry and connect in a new terminal BEFORE CLOSING THE OPEN TERMINAL!!!
Test logging in to root via SSH - it will allow password entry, but the response shoudl be: “Permission denied, please try again.”
Firewall
Set up ‘Uncomplicated Firewall’: ufw.
Allow on the custom SSH port:
sudo ufw allow 4444/tcpAllow port 80, for internet:
sudo ufw allow 80/tcpTo allow https traffic, open port 443:
sudo ufw allow 443/tcpShow the exceptions you have added:
sudo ufw show addedEnable the firewall:
sudo ufw enableMore info on configuring ufw.
Swapfile
Probably not necessary but may prevent server crashing in the event of high traffic. Accessing data stored on disk (rather than memory) is slow.
Generally recommended that the size of the swapfile should be 2 x RAM.
Determine RAM:
# return memory in MB
free -mUse fallocate to allocate space to swap file:
sudo fallocate -l 4G /swapfilefallocate is used to manipulate the allocated disk space for a file, either to deallocate or preallocate it. For filesystems which support the fallocate system call, preallocation is done quickly by allocating blocks and marking them as uninitialized, requiring no IO to the data blocks. This is much faster than creating a file by filling it with zeros. fallocate man page
Give correct permissions to swapfile - restrict access so other users/processors can’t see it:
sudo chmod 600 /swapfileFormat the file for swap:
sudo mkswap /swapfileMake the file available for swap:
sudo swapon /swapfileMake it available for swap at boot by amending /etc/fstab:
sudo sh -c 'echo "/swapfile none swap sw 0 0" >> /etc/fstab'Resources
comments powered by Disqus