Linux 4 All

Best Linux tricks source

Posts in the Linux category

When Nginx is running on a server behind a Load Balancer, by default all traffic will come from Load Balancer IP, thus Nginx will only log the Load Balancer IP. In order to get your visitor real IP address, you can use X-Forwarded-For header.

In order to configure Nginx to log real IP address, add the following to nginx.conf, http section:

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    set_real_ip_from; # Varnish IP address
    set_real_ip_from; # Load Balancer IP range

    real_ip_header X-Forwarded-For;
    real_ip_recursive on;

Here is a little curl magic to get you how long it it will take to connect to a site. It will display time to connect, time to namelookup, time to first byte and so on.

curl -s -w "\ntime connect: %{time_connect}\ntime namelookup: %{time_namelookup}\ntime pretransfer: %{time_pretransfer}\ntime redirect: %{time_redirect}\ntime to first byte: %{time_starttransfer}\ntime total: %{time_total}\nhttp code: %{http_code}\n\n" -o /dev/null

time connect: 0.165
time namelookup: 0.133
time pretransfer: 0.166
time redirect: 0.000
time to first byte: 0.392
time total: 0.432
http code: 200

Some time ago I had to deal with a request to delete all but the newest 50 files from a folder. To make my life a little bit miserable, there were other folders inside that folder which shouldn’t be deleted and obviously some files with space in their names

In the end I did a small script taking the folder as first parameter and number of files to keep as the second one.

if [ $# -eq 0 ]
  echo " This script will delete all but newest files from a specified folder."
  echo ""
  echo " Usage: source $0  "
  exit 1

TOTAL_FILES=$(find $1 -maxdepth 1 -type f -print0| xargs -0 ls -lt | wc -l)
FILES_TO_DELETE=$(echo "$TOTAL_FILES-$2" | bc)

find $1 -maxdepth 1 -type f -print0 | xargs -0 ls -t | tail -$FILES_TO_DELETE | sed -e 's/ /\\ /g' | xargs rm

rm -rf is gone

It looks like you cannot run rm -rf as root on CentOS or RHEL6 boxes.

# cat /etc/redhat-release 
CentOS release 6.5 (Final)
# rm -vrf /               
rm: it is dangerous to operate recursively on `/'
rm: use --no-preserve-root to override this failsafe

Don’t try this on a RHEL5 boxes.

To disable cluster services and change VG locking type to no:

service rgmanager stop && chkconfig rgmanager off
vgchange -c n $CLUSTERED_VG_GROUP
sed -i 's/locking_type = 3/locking_type = 1'
service clvmd stop && chkconfig clvmd off
service cman stop && chkconfig cman off

To enable cluster services and change VG locking type to yes:

service cman start && chkconfig cman on
service clvmd start && chkconfig clvmd on
sed -i 's/locking_type = 1/locking_type = 3'
vgchange -c y $CLUSTERED_VG_GROUP
service rgmanager start && chkconfig rgmanager on

Assuming you have a huge incoming mail queue and you need to remove it quickly, here is how:

cd /var/spool/postfix
service postfix stop
mv incoming incoming.spam
mkdir incoming
chown postfix.root incoming
chmod 700 incoming
service postfix start

Here is how:

SELECT TABLE_SCHEMA, TABLE_NAME FROM information_schema.statistics WHERE index_type LIKE ‘FULLTEXT%’;

This is important as MySQL prior to 5.6 does not support FULLTEXT InnoDB tables.

Should you need to disable Trace and Track methods on Apache, add the following lines to httpd.conf and restart Apache.

 RewriteEngine on
 RewriteRule .* - [F]

TraceEnable off

I admit that is pretty lame, but until today I had no idea how to kill a specific MySQL query. It is quite easy.

First we need to get the queries:

mysql> show processlist;
| Id   | User | Host      | db   | Command | Time | State | Info             |
| 5585 | root | localhost | NULL | Query   |    0 | NULL  | show processlist |
1 row in set (0.00 sec)

Then kill the id:

mysql> kill 5585;
ERROR 1317 (70100): Query execution was interrupted

Here is how to test IMAP, POP and SMTP authentication using telnet or openssl:

POP3 test:

telnet $SERVER_IP 110
+OK Hello there. 
+OK Password required.
PASS Password
+OK logged in.

IMAP test:

telnet $SERVER_IP 143
a1 LOGIN Password

POP3-SSL test:

openssl s_client -connect $SERVER_IP:995
+OK Hello there. 
+OK Password required.
PASS Password
+OK logged in.

IMAP-SSL test:

openssl s_client -connect $SERVER_IP:993
a1 LOGIN Password

For the SMTP test, first we need to generate base64 encrypted username/password hash and then we can test it:

printf '\0%s\0%s' '' 'Password' | openssl base64

telnet $SERVER_IP 25
235 2.7.0 Authentication successful AHVzZXJAZXhhbXBsZS5jb20AUGFzc3dvcmQ=


photo credit: Micky.! via photopin cc