iptables, the linux firewall, can do this.
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
Remember not to do this over a remote SSH session.
The first rule will flush the existing firewall rules, the second and third block incoming and outgoing traffic respectively. The fourth rule blocks FORWARDs but that's probably largely irrelevant for you. You can then add entries for traffic you wish to allow as below.
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
You haven't specified your linux flavour but you can execute these commands on the command line and then use iptables-save to save your configuration. Alternatively, you can edit
/etc/sysconfig/iptables (on a RHEL based distribution, likely elsewhere on others?) and save these commands (minus the iptables prefix, i.e.,
-A INPUT -p tcp --dport 22 -j ACCEPT) and then load them using iptables-restore.