Skip to content

Instantly share code, notes, and snippets.

@rohan-molloy
Last active April 18, 2020 02:53
Show Gist options
  • Save rohan-molloy/effc571c9854d7d6aa38b2443da1e08e to your computer and use it in GitHub Desktop.
Save rohan-molloy/effc571c9854d7d6aa38b2443da1e08e to your computer and use it in GitHub Desktop.

Self Hosted Email using a VPS

Step 1: Register a VM

For this example, I'm using Amazon lightsail with Debian.
Ensure port 25 is open in the firewall section
Thus tutorial assumes there is a standard user named 'admin'

Step 2: Set the hostname of the instance

hostname ec2-xxx-xxx-xxx-xxx.ap-southeast-2.compute.amazonaws.com
echo 'ec2-xxx-xxx-xxx-xxx.ap-southeast-2.compute.amazonaws.com' > /etc/hostname
vi /etc/hosts

Step 3: Set firewall rules

Here we restrict SSH to our IP address/range, allow SMTP

iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -s <your_ip_range> -p tcp -m tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 25 -j ACCEPT
iptables -P INPUT DROP
apt-get install iptables-persistent

Step 4: Install Postfix

apt-get install postfix mutt

Step 5: Configure Postfix

Run dpkg-reconfigure postfix.
Select "Internet site"
Set internet mail name to the hostname set in step 2.
Set the root and postmaster recipient to "admin"
Set other destinations to receive mail to as the hostname and localhost.
Leave all other options as the default.

Step 6: You have mail!

su admin
mutt 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment