9 Useful Tips For Linux Server Security

Any serious systems can’t ignore server security, especially in public Cloud. No doubt there’re tons of tips and tutorials available on the Internet. Let’s focus on fundamental and general best practices first.
A List Of Security Improvements I Enforce After OS Provisioning.

9 Useful Tips For Linux Server Security

Here we use Ubuntu 16.04 for instance.

1.1 1. Keep Kernel Up-To-Date.

Certainly no blind update for prod envs. But for newly installed servers, it’s usually harmless and can guarantee a higher level of security.

One common suggestion is disabling unused services. But I choose to trust my distros provider. Generally speaking, I believe they might make right choices to have what installed and enabled by default.

apt-get -y update

1.2 2. Reset Root password And Install Basic Packages

We need that to access web console of VMs. This happens when ssh doesn’t work. e.g. problematic iptables rules block you, OS runs into kernel panic, or machine reboot mysteriously stucks.

echo "root:$root_pwd" | chpasswd

  • Basic packages I use heavily
apt-get install -y wget curl

# vim for quick editing
apt-get install -y vim

# tmux is useful to persist sessions
apt-get install -y tmux

# lsof for checking tcp ports
apt-get install -y lsof

  • Install docker daemon, if necessary
wget -qO- https://get.docker.com/ | sh

  • Change hostname
echo "my-hostname" > /etc/hostname
hostname -F /etc/hostname

1.3 3. Generate SSH Key Pair.

Never never share the same ssh key pair across servers!

exec ssh-agent bash

# General new key pair

# Load key pair

1.4 4. Hardening SSHD.

Only allow ssh by keyfile, thus hackers can’t easily break-in by guessing your password. Use another ssh listening port other than 22, which can avoid annoying ssh login attempts.

  • Inject my ssh key
mkdir -p /root/.ssh/
echo "ssh-rsa AAA... denny@dennyzhang.com" \
   >> ~/.ssh/authorized_keys

  • Disable ssh by password
# Disable ssh by password
sed -i 's/^#PasswordAuthentication yes/PasswordAuthentication no/g' \
sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' \
grep PasswordAuthentication /etc/ssh/sshd_config

# Use another ssh port
sed -i "s/^Port 22/Port $sshd_port/g" /etc/ssh/sshd_config
grep "^Port " /etc/ssh/sshd_config

# Restart sshd to take effect
service ssh restart

1.5 5. Restrict Malicious Access By Firewall.

This might be the most important security improvement you shall do.

# Have a clean start with iptables
iptables -F; iptables -X
echo 'y' | ufw reset
echo 'y' | ufw enable
ufw default deny incoming
ufw default deny forward

# Allow traffic of safe ports
ufw allow 22/tcp
ufw allow 80,443/tcp

# Allow traffic from certain port
ufw allow 2702/tcp

# Allow traffic from given ethernet nic
ufw allow in on docker0

# Allow traffic from trusted ip
ufw allow from

1.6 6. Add Timestamp To Command History.

It allows us to review what commands has been issued, and when.

echo export HISTTIMEFORMAT=\"%h %d %H:%M:%S \" >> /root/.bashrc

1.7 7. Pay Close Attention to var/log.

Use logwatch to automate the check and analysis. It’s a userful parsing perl script that analyzes and generates daily reports on your system’s log activity. Major log files:

  • /var/log/kern.log
  • /var/log/syslog
  • /var/log/ufw.log
  • /var/log/auth.log
  • /var/log/dpkg.log
  • /var/log/aptitude
  • /var/log/boot.log
  • /var/log/cron.log
  • /var/log/mailog
apt-get install -y logwatch

# Full check. Takes several minutes
logwatch --range ALL

# Only check log of Today
logwatch --range Today

# Check log for last week
logwatch --range "between -7 days and -1 days"

1.8 8. Run 3rd Security Check Tools.

Not everyone can or will be a security expert. Better try reliable and versatile tools. lynis is quite handy and straight-forward. Just a single bash file.

apt-get install -y lynis

# Run lynis to check security issues
lynis -c

1.9 9. Proper Backup Unrecoverable Data.

Always has plan B. As the last resort, make it’s feasible to do a quick system restore in new servers.

Special thanks to this reddit discussion.

More Reading: Detect Suspicious Linux Processes


PRs Welcome

Blog URL: https://www.dennyzhang.com/linux_security

3 Responses to 9 Useful Tips For Linux Server Security

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.