Default server setup tasks
Following up on the previous blog post Protect your server here are some more typical tasks I set up on a fresh system.
Firewall
In the earlier post I showed the output of the iptables-save
command of my typical iptables firewall
configuration (this can be easily loaded with iptables-restore
). However, it might be easier to
understand looking at the iptables
commands itself:
#!/bin/bash
iptables -F
iptables -A INPUT -i lo -p all -j ACCEPT
iptables -A OUTPUT -o lo -p all -j ACCEPT
iptables -A OUTPUT -o eth0 -p all -j ACCEPT
iptables -A INPUT -i eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -I INPUT -p tcp --dport 22 -i eth0 -m state --state NEW -m recent --set
iptables -I INPUT -p tcp --dport 22 -i eth0 -m state --state NEW -m recent --update --seconds 300 \
--hitcount 2 -j LOG --log-prefix "Possible SSH attack! " --log-level 7
iptables -I INPUT -p tcp --dport 22 -i eth0 -m state --state NEW -m recent --update --seconds 300 \
--hitcount 2 -j DROP
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -P INPUT DROP
This script sets up the firewall in the following way:
- Accept all input and output on the local loopback interface lo
- Allow all output on the network card eth0
- Allow all input from already established connections
- Track the connection attempts on port 22 (SSH) (needs recent module)
- Block connections on port 22 for 5 minutes for IP addresses which try more than twice to establish a connection within 5 minutes
- Allow all other connections on port 22 (the order is important, has to come after the ‘block’ rule!)
- If none of the rules matched, drop connection
Talk to me
…using ‘mini smtp’ and ‘mutt’.
If you want to be up-to-date about what’s going on your server, you should let your server send you an email occassionaly. For example to tell you the outcome of the daily backup, etc.
This requires to setup the local mail system. In my opinion the easiest way is to use msmtp together with a gmail account and mutt.
apt-get install msmtp mutt
Add your gmail account settings to the /etc/msmtprc config file:
# Set default values for all following accounts.
defaults
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile ~/.msmtp.log
# A gmail account
account gmail
host smtp.gmail.com
port 587
from xxx@googlemail.com
user xxx@googlemail.com
password xxx
# Set a default account
account default : gmail
Send a test eMail from the command line:
echo "This is just a test..." | mutt -s "Just a test" "xxx@example.com"
Get notified when someone logs in
As you can now send eMails from the command line, you can make use of this for example for getting notified when someone logs in via SSH.
Create a script, e. g. /usr/local/sbin/login_notify.sh:
#!/bin/sh
EMAIL_TO="xxx@example.com"
SUBJECT="SSH Login Notification"
TIME=`date +"%d-%m-%Y %T"`
MESSAGE="
A user signed into your server through SSH.
-------------------------------------------
Username: ${PAM_USER}
IP Address: ${PAM_RHOST}
Time: ${TIME}"
if [ ${PAM_TYPE} = "open_session" ]; then
echo "${MESSAGE}" | mutt -s "${SUBJECT}" "${EMAIL_TO}"
fi
exit 0
Then add a hook into the pam service /etc/pam.d/sshd which calls your notification script on each SSH login:
...
session required pam_exec.so /usr/local/sbin/login_notify.sh
Backups with rsnapshot
rsnaphot uses rsync
and hard links in a very clever way
to create easy accessible backups without wasting much disk space.
On my home NAS I have two hard disks, one holding the data which is shared via NFS, the other is the backup drive. I perform 7 daily backups followed by a weekly backup. That way I always have an incremental backup of the last 7 days, plus snapshots of every week. For example at the moment my backup drive looks like that:
drwxr-xr-x 3 root root 4.0K Aug 24 04:20 daily.0
drwxr-xr-x 3 root root 4.0K Aug 23 04:10 daily.1
drwxr-xr-x 3 root root 4.0K Aug 22 03:50 daily.2
drwxr-xr-x 3 root root 4.0K Aug 21 03:57 daily.3
drwxr-xr-x 3 root root 4.0K Aug 20 03:51 daily.4
drwxr-xr-x 3 root root 4.0K Aug 19 03:53 daily.5
drwxr-xr-x 3 root root 4.0K Aug 18 04:09 daily.6
drwxr-xr-x 3 root root 4.0K Aug 14 03:36 weekly.0
drwxr-xr-x 3 root root 4.0K Aug 7 03:36 weekly.1
...
Where each directory represents a “snapshot” of the particular time.
After installing rsnapshot
edit/add these values of the config file /etc/rsnapshot.conf
# All snapshots will be stored under this root directory.
# (E.g. that's the mount point of my backup hard disk)
snapshot_root /backup
# Comment out the backup levels and create
retain daily 7
retain weekly 52
# Specify the log file
logfile /var/log/rsnapshot.log
# The directories to backup and excludes
# (E.g. that's the mount point of my NFS share hard disk and I
# exclude all 'tmp' directories)
backup /nfs/ all/
exclude tmp/
I use a bash scripts which launch rsnapshot and send my an email with the result, so that I notice soon when something’s going wrong. There’s only one thing which is worse than having no backups, it’s wrongly assuming you have working backups ;-)
#!/bin/bash
mount /backup
/usr/bin/rsnapshot daily
umount /backup
tail -14 /var/log/rsnapshot.log | mutt -s "rsnapshot backup daily" xxx@example.com
(the ‘weekly’ one is basically the same; just make sure you don’t run them at the same time)