Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to allow user to check how many rules are present in filter table, but not able to view actual rules, is that possible?

literally I mean iptables -L | wc -l (I know it's faulty), but sudo doesn't seem to support pipe commands like that, I mean, currently If I wanted to allow that, I need to grant permission to run iptables -L instead of iptables -L | wc -l

Any thoughts?

share|improve this question
not sure what shell you are using but I'am able to run the following: sudo iptables -L|egrep -v 'Chain|target'|wc -l without any problem – BitsOfNix Jan 2 '13 at 13:48
@AlexandreAlves updated question ... – warl0ck Jan 2 '13 at 13:54
The reason that pipe commands do not work with sudo, is because it is actually two separate commands... – Gert van den Berg Jan 2 '13 at 14:06
up vote 4 down vote accepted

Create a script that runs the commands to get the number of iptables rules. I would suggest something like this:

/sbin/iptables -L | grep -Ecv '^$|^Chain |^target'

Save script in /usr/local/bin, make it executable and check that only root can write to it. Recommended mode: 0555

Edit sudoers to allow the user to run that script using sudo.

share|improve this answer
Looks easy to do that! But anyway, I modified iptables program to do the counting ... thanks anyway, scripting is a good ideas to problems like this – warl0ck Jan 2 '13 at 15:04
(This is not completely valid with the edited case where the user doesn't have permissions to run iptables -L) (the info seem to require root to access, so a modified iptables (or other command getting the info from /proc/net/ip_*) running setuid root or via sudo is likely the only practical solution) – Gert van den Berg Mar 23 at 8:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.