Saturday, June 01, 2019

Repairing Linux firewall

This is more a note to self, in case I mess up again.

I was playing around with the firewall on my Jetson Nano because I was testing out how to install NextCloud on it. However, I managed to thoroughly wreck my firewall settings, and in the end, I could not SSH in nor use VNC. So, I had to reset the firewall.

Here's how.

First, set all rules in iptables to ACCEPT.

sudo iptables -P INPUT ACCEPT
sudo iptables -P FORWARD ACCEPT
sudo iptables -P OUTPUT ACCEPT

Then, flush the nat and mangle tables, and all chains.
sudo iptables -t nat -F
sudo iptables -t mangle -F
sudo iptables -F
sudo iptables -X

Then I did a reset of ufw with:
sudo ufw reset

I also had problems with the ufw rules, so I did something really stupid:
sudo rm -rf /etc/ufw

In the end, I had to reinstall ufw, and copy the default rules files by:
sudo apt remove ufw
sudo apt install ufw
sudo cp -p /usr/share/ufw/before.rules /etc/ufw/
sudo cp -p /usr/share/ufw/before6.rules /etc/ufw/
sudo cp -p /usr/share/ufw/after.rules /etc/ufw/
sudo cp -p /usr/share/ufw/after6.rules /etc/ufw/
sudo ufw enable

Then, add back the ports I need, like SSH, by:
sudo ufw allow ssh

And it seems I managed to get it working again. Phew!

No comments: