In this tutorial we are going to work on the firewall configuration
of your machine. Some care must be taken when doing this, or you will
suddenly find you can no longer log in!
In all these cases the easiest way to do the experiment is to CREATE
an executable program in /root called "firewall".
You should make the contents of this something like:
#!/bin/bash
#
iptables -F INPUT
iptables -F OUTPUT
iptables -F FORWARD
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
#
# Accept ongoing connections
iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
#
# For your own safety, stop users logging in from other VMs
#
iptables -A INPUT -m conntrack --ctstate NEW -p tcp --dport 22 ! -s 10.0.0.0/16 -j ACCEPT
iptables -A INPUT -m conntrack --ctstate NEW -p tcp --dport 23 ! -s 10.0.0.0/16 -j ACCEPT
#
# Your changes go after here.
#
To execute this file, remember "chmod +x ./filewall" and then just "./firewall"
or "/root/firewall" to run it. Execute it once and they press the check
button to make sure everything is set up ok.
After executing this file you can use "iptables -L" to show you what
rules have been stored in the kernel. The provided rule uses a default
policy of ACCEPT, just in case you make a typo and lock yourself out.
Default ACCEPT is a bad idea here, and this is just to get you going.
Later on we look at the default policy of DROP.
NOTE: If at any time you mess up and can no longer connect via telnet
or ssh, then you can either reboot or use VNC to fix the problem. The
script is not reexecuted automatically when you reboot, and should still be there
on the next boot. VNC is much faster. If you use VNC, once logged in, you
can restore the default firewall by doing "systemctl restart iptables.service".