Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands using iptables from sorted by
Terminal - Commands using iptables - 31 results
iptables -L -vnx --line-numbers; iptables -t nat -D <chain-name> <number>
watch -d -n 2 iptables -nvL
2014-02-23 16:35:03
User: xxdesmus
Functions: iptables watch
0

This will highlight (with a box over it) any changes since the last refresh.

iptables -L INPUT --line-numbers
2013-12-19 16:14:25
Functions: iptables
2

Then you can remove the specific entry:

iptables -D INPUT 10

Just make sure these are set:

IPTABLES_SAVE_ON_STOP="yes"

IPTABLES_SAVE_ON_RESTART="yes"

Else your changes won't stick when you restart iptables.

iptables-save > iptables.current; vi iptables.current; iptables-restore iptables.current; service iptables save
2013-12-04 18:41:48
User: bigc00p
0

These series of commands allows you all at once to make a backup of your current config, edit that config, then saves it as the running config and makes it persistent. I would advise knowing what your doing to the config before running this because if you mess up say the port 22 portion, you may get knocked off the system. ;) Don't say I didn't warn ya!

while true; do iptables -nvL > /tmp/now; diff -U0 /tmp/prev /tmp/now > /tmp/diff; clear; cat /tmp/diff; mv /tmp/now /tmp/prev; slee p 1; done
2012-04-15 00:02:33
Functions: cat diff iptables mv
0

this alternative shows the differences as they occur so that they are made plain

REJECT_RULE_NO=$(iptables -L RH-Firewall-1-INPUT --line-numbers | grep 'REJECT' | awk '{print $1}');/sbin/iptables -I RH-Firewall-1-INPUT $REJECT_RULE_NO -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT -m comment --comment "Permit HTTP Service"
2012-02-02 12:21:06
User: ajmckee
Functions: awk grep iptables
0

Rather then editing the /etc/sysconfig/iptables file, or during a kickstart doing some awk/sed magic, easily add a rule in the correct place within iptables

sudo iptables -I INPUT -p tcp --dport 3000 -j ACCEPT
2011-12-16 10:39:13
User: Dhinesh
Functions: iptables sudo
Tags: iptables
-2

This command will open tcp port 3000 in your machine

iptables -P INPUT ACCEPT; iptables -P FORWARD ACCEPT; iptables -P OUTPUT ACCEPT; for table in `cat /proc/net/ip_tables_names`; do iptables -t $table -F; iptables -t $table -Z; iptables -t $table -X; done
2011-12-15 18:19:34
User: dash
Functions: iptables
Tags: Linux iptables
0

If you changed the default policy on any chain other than filter table chain's, this won't be enough.

iptables -A OUTPUT www.baidu.com -p tcp -j REJECT --reject-with tcp-reset
2011-06-26 03:12:22
User: kev
Functions: iptables
Tags: iptables
-10

To save all rules so that they are not lost in case of a server reboot:

/etc/init.d/iptables save

iptables -A INPUT -s 65.55.44.100 -j DROP
cd /etc/network/if-up.d && iptables-save > firewall.conf && echo -e '#!/bin/sh -e\niptables-restore < $(dirname $0)/firewall.conf' > iptables && chmod a+x iptables
echo 1 > /proc/sys/net/ipv4/tcp_syncookies echo 1 > /proc/sys/net/ipv4/ip_forward iptables -A FORWARD -p tcp ?syn -m limit -j ACCEPT
tail -f /var/www/logs/domain.com.log | grep "POST /scripts/blog-post.php" | grep -v 192.168. | awk '{print $1}' | xargs -I{} iptables -I DDOS -s {} -j DROP
2010-11-30 06:22:18
User: tehusr
Functions: awk grep iptables tail xargs
1

Takes IP from web logs and pipes to iptables, use grep to white list IPs.. use if a particular file is getting requested by many different addresses.

Sure, its already down pipe and you bandwidth may suffer but that isnt the concern. This one liner saved me from all the traffic hitting the server a second time, reconfigure your system so your system will work like blog-post-1.php or the similar so legitimate users can continue working while the botnet kills itself.

function clearIptables(){iptables -P INPUT ACCEPT; iptables -P FORWARD ACCEPT; iptables -P OUTPUT ACCEPT; iptables -F; iptables -X; iptables -L}
iptables-save > firewall.conf; rm -f /etc/network/if-up.d/iptables; echo '#!/bin/sh' > /etc/network/if-up.d/iptables; echo "iptables-restore < firewall.conf" >> /etc/network/if-up.d/iptables; chmod +x /etc/network/if-up.d/iptables
2010-11-13 23:58:28
Tags: sudo iptables
0

a simple command in order to make iptables rules permanent, run @ sudo!

iptables -A FORWARD -i br0 -m iprange --src-range 192.168.0.x-192.168.0.y -m iprange --dst-range 192.168.0.w-192.168.0.z -j DROP
2010-11-13 23:55:23
Functions: iptables
Tags: sudo iptables
0

Destination IPs will become invisible to source IPs!

dpkg -L iptables | perl -lne 'print if -f && -x'
sudo iptables -L -nv
for i in `cat /proc/net/ip_tables_names`; do iptables -nL -v --line-numbers -t $i ; done
2010-04-23 13:53:49
User: bw
Functions: iptables
1

show your current iptable rules from every available iptable table

iptables -L -n -v
2009-10-07 21:13:59
User: gpenguin
Functions: iptables
0

I was previously unaware of the -v switch. As a result I never got specifics about which interfaces the allowed or dropped applied to. Thought I'd share the wealth...

FYI, -n prevents DNS resolving of IPs.

iptables -t nat -A POSTROUTING -p tcp --dport 25 -j SNAT --to-source IP_TO_ROUTE_THROUGH
curl -s http://isc.sans.org/sources.html|grep "ipinfo.html"|awk -F"ip=" {'print $2'}|awk -F"\"" {'print $1'}|xargs -n1 sudo iptables -A OUTPUT -j DROP -d > 2&>1
iptables -t nat -A PREROUTING -p tcp --dport [port of your choosing] -j REDIRECT --to-ports 22
2009-06-18 17:38:59
User: brizznown
Functions: iptables
9

Stuck behind a restrictive firewall at work, but really jonesing to putty home to your linux box for some colossal cave? Goodness knows I was...but the firewall at work blocked all outbound connections except for ports 80 and 443. (Those were wide open for outbound connections.) So now I putty over port 443 and have my linux box redirect it to port 22 (the SSH port) before it routes it internally. So, my specific command would be:

iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-ports 22

Note that I use -A to append this command to the end of the chain. You could replace that with -I to insert it at the beginning (or at a specific rulenum).

My linux box is running slackware, with a kernel from circa 2001. Hopefully the mechanics of iptables haven't changed since then. The command is untested under any other distros or less outdated kernels.

Of course, the command should be easy enough to adapt to whatever service on your linux box you're trying to reach by changing the numbers (and possibly changing tcp to udp, or whatever). Between putty and psftp, however, I'm good to go for hours of time-killing.

iptables -D fail2ban-SSH -s <ip_address_to_be_set_free> -j DROP
2009-05-08 19:22:15
User: mheadd
Functions: iptables
3

Removes an iptables rule created by fail2ban. This example shows how to remove a rule for an IP from the fail2ban-SSH chain. Can be used for any service monitored by fail2ban.

For more on fail2ban, see http://www.fail2ban.org/wiki/index.php/Main_Page

iptables -F && iptables -X && iptables -P INPUT ACCEPT && iptables -OUTPUT ACCEPT
2009-03-27 15:03:54
User: lme
Functions: iptables
Tags: Linux iptables
0

This turns your iptables packet filter to a "Allow any from any to any" filter, so you can rule out any filtering issues when you have a problem to enable a connection from or to your host.

To re-enable it, run /etc/init.d/iptables restart