banner
 

Path: content/Tips/Linux/default.md

Linux

Local firewalling

To block an incoming IP address:

iptables -A INPUT -s 187.3.166.114 -j DROP

Or a particular port:

iptables -A INPUT -s 65.55.44.100 -p tcp --destination-port 25 -j DROP

The -s (ip address) is optional in this case.

Who logged on last?

Normal people use the 'last' command. You can also use this monstrosity if you are feeling hackish:

perl -we '$recs = ""; while (<>) {$recs .= $_};$uid = 0;foreach (split(/(.{292})/s,$recs)) {next if length($_) == 0;my ($binTime,$line,$host) = $_
=~/(.{4})(.{32})(.{256})/;if (defined $line && $line =~ /\w/) {$line =~ s/\x00+//g;$host =~
s/\x00+//g;printf("%5d %s %8s %s\n",$uid,scalar(gmtime(unpack("I4",$binTime))),$line,$host)}$uid++}print"\n"' < /var/log/lastlog

note - this didn't work the last time I tried t (in 2023)

Soak testing servers

Simple, safe way to raise system load:

NUMCPU=$(grep $'^processor\t*:' /proc/cpuinfo |wc -l)
for ((i=0;i<$NUMCPU;i++));do
  echo 'scale=100000;pi=4*a(1);0' | bc -l &
done ;\
sleep 60; \
killall bc

That brings the cpu load up for 60 seconds by calculating pi to absurd presision, thousands of times. Note the test to see how many threads it should run in parallel.

'stress' is available on Ubuntu platforms which can also test CPU, memory and disk load.

Somewhat hostile way to load up a system:

:(){ :|:& };:

(from http://www.cyberciti.biz/faq/understanding-bash-fork-bomb/ ) This can take the LA over 1000 so no running it on a live server!

Using find

The options -delete and -ls are a useful optimisation, saves running '-exec '{}' ls -la \;' and is also a lot faster as it doesn't have to spawn an extra process per file.

Unix Bar Charts

Little perl script to turn a stream of numbers into a pretty realtime barchart:

root@host:~# function baa
{
perl -ne '$min=0; $max=6750; $w=75; use POSIX; $d=ceil(log($max)/log(10)); $w-=$d; $v=$_<$min?0:$_>$max?$max:$_;
$s=$w*$v/($max-$min); $bar=join("", ("*")x$s); $bar=~s/.$/|/ if $v==$max; print sprintf("%${d}d ",$_)."$bar\n";'
}

then, something like this:

(while [ 1 ] ; do
 mailq | tail -1 | awk '{ print $5;}'; sleep 1; done) | baa

will generate this:

1700 *****************
1720 ******************
2000 *********************
2040 *********************
2060 *********************
1640 *****************

Strip last column of text file

To strip the last column of a file, use awk like this:

cat file | awk -F. 'NF{NF--};1' 

-F. : use '.' as the column separator

Links in this section

Filesystems
SSH
Systemd
VI

Last updated : 14 November 2024