In this article, we will see how to solve "-bash: /etc/iptables/iptables.rules: No such file or directory"
error. In many cases, if you don't have the required packages installed then you always end up in error like this. Here also same thing happened with me when I was trying to use iptables command to create a chain rule to redirect all the SSH traffic to sshguard but when I tried to save the rule then I end up in getting "-bash: /etc/iptables/iptables.rules: No such file or directory"
error. While this error might come due to some other reason but in my case it was due to missing of a package.
[Solved]: -bash: /etc/iptables/iptables.rules: No such file or directory
Also Read: How to Define Audit File System Rules in RHEL/CentOS/Rocky Linux
When I was trying to run iptables-save > /etc/iptables/iptables.rules
command to save the iptables rules then it was always giving below error. So I further checked this error and found that there is no directory called iptables
present inside /etc
path. More about iptables-save command.
root@cyberithub:~# iptables-save > /etc/iptables/iptables.rules
-bash: /etc/iptables/iptables.rules: No such file or directory
This is simply because iptables-persistent package was missing in my system. So to deal with the above error I had to install iptables-persistent
package using apt install iptables-persistent
command as shown below.
root@cyberithub:~# apt install iptables-persistent
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
ipset libipset13 libnftables1 python3-decorator python3-firewall python3-nftables python3-selinux python3-slip python3-slip-dbus
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
netfilter-persistent
The following NEW packages will be installed:
iptables-persistent netfilter-persistent
0 upgraded, 2 newly installed, 0 to remove and 213 not upgraded.
Need to get 13.8 kB of archives.
After this operation, 89.1 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Preparing to unpack .../iptables-persistent_1.0.14ubuntu1_all.deb ...
Unpacking iptables-persistent (1.0.14ubuntu1) ...
Setting up netfilter-persistent (1.0.14ubuntu1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/netfilter-persistent.service → /lib/systemd/system/netfilter-persistent.service.
Setting up iptables-persistent (1.0.14ubuntu1) ...
update-alternatives: using /lib/systemd/system/netfilter-persistent.service to provide /lib/systemd/system/iptables.service (iptables.service) in auto mode
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for systemd (245.4-4ubuntu3.15) ...
After successful installation, if you try to list all the files installed to your system from package iptables-persistent
then you will see a directory called /etc/iptables
root@cyberithub:~# dpkg -L iptables-persistent /. /etc /etc/iptables /etc/systemd /etc/systemd/system /etc/systemd/system/netfilter-persistent.service.d /etc/systemd/system/netfilter-persistent.service.d/iptables.conf /usr /usr/share /usr/share/doc /usr/share/doc/iptables-persistent /usr/share/doc/iptables-persistent/copyright /usr/share/netfilter-persistent /usr/share/netfilter-persistent/plugins.d /usr/share/netfilter-persistent/plugins.d/15-ip4tables /usr/share/netfilter-persistent/plugins.d/25-ip6tables /usr/share/doc/iptables-persistent/README /usr/share/doc/iptables-persistent/changelog.gz
If you go inside this directory then you will see two files(rules.v6 and rules.v4) which got created after package installation.
root@cyberithub:~# cd /etc/iptables/ root@cyberithub:/etc/iptables# ls -lrt total 12 -rw-r--r-- 1 root root 195 Jan 26 00:46 rules.v6 -rw-r--r-- 1 root root 288 Jan 26 00:46 rules.v4
So now if you try to run the same command again then you should be able to run it as you can see below.
root@cyberithub:~# iptables-save > /etc/iptables/iptables.rules
Hopefully this will help you as it helped me. Please let me know your feedback in the comment box if you are also facing the same error but due to some other reason and not exactly due to missing iptables-persistent package. So that it will help others as well.
thank you!