Linux 4 All

Best Linux tricks source

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

POP3 test:

telnet $SERVER_IP 110
+OK Hello there. 
USER user@example.com
+OK Password required.
PASS Password
+OK logged in.

IMAP test:

telnet $SERVER_IP 143
a1 LOGIN user@example.com Password
a1 OK LOGIN Ok.

POP3-SSL test:

openssl s_client -connect $SERVER_IP:995
+OK Hello there. 
USER user@example.com
+OK Password required.
PASS Password
+OK logged in.

IMAP-SSL test:

openssl s_client -connect $SERVER_IP:993
a1 LOGIN user@example.com Password
a1 OK LOGIN Ok.

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' 'user@example.com' 'Password' | openssl base64
AHVzZXJAZXhhbXBsZS5jb20AUGFzc3dvcmQ=

telnet $SERVER_IP 25
AUTH PLAIN AHVzZXJAZXhhbXBsZS5jb20AUGFzc3dvcmQ=
235 2.7.0 Authentication successful AHVzZXJAZXhhbXBsZS5jb20AUGFzc3dvcmQ=

e-mail

photo credit: Micky.! via photopin cc

In a previous post I was explaining how to log visitor real IP address in access_log. Now it’s time to do the same for error_log.

For this we are going to compile and install a module called mod_vgremoteip

Steps outlined bellow:

1. Get the module

git clone https://github.com/vgno/mod_vgremoteip.git

2. Install gcc and httpd-devel tools
yum install httpd-devel
yum install gcc

3. Compile the module
apxs -a -i -c mod_vgremoteip.c

4. Configure Apache to use it
LoadModule vgremoteip_module  modules/mod_vgremoteip.so

# Name of header which contains the 'real' client IP.
 VGRemoteIPHeader X-Forwarded-For
# Subnet to mark as trusted subnet (this ip will be allowed to set the X-Forwarded-For header and marked as a proxy ip).
# You should specify this.
VGTrustedProxy 10.0.0.0/8
# You can also specify a single ip addresses.
# Do not specify hostnames.
VGTrustedProxy 127.0.0.1

5. Restart Apache and you are done.
service httpd restart

Get mail queue

When dealing with mail issues, the following one-liner will display all Postfix mail IDs from the queue.

postqueue -p | egrep -v ^[[:space:]] | grep -v ^$ | awk '{print $1}' | grep -v ^\( | grep -v ^\- | sed -e 's/\*//g'

Get MySQL InnoDB tables

Run this query to get all InnoDB tables:

SELECT table_schema, table_name FROM INFORMATION_SCHEMA.TABLES WHERE engine = 'innodb';

Change engine='innodb' to engine='myisam' to get MyISAM tables.

X-Served-By Header

  • In Linux
  • On 22 Nov | '2014

Apache has the ability to return its Hostname via the X-Served-By header. When troubleshooting nodes behind a Load Balancer, this can help localize a problematic node.

Configuration:
Export the server’s hostname to Apache:

CentOS File: /etc/sysconfig/httpd
Ubuntu File: /etc/apache2/envvars

Add the following to Apache’s environment configuration file:

export HOSTNAME=$(hostname)

Add the X-Served-By header

Add the following to a Virtual Host or any other Apache configuration file:


PassEnv HOSTNAME
Header set X-Served-By "%{HOSTNAME}e"


The above lines require that mod_headers is enabled. Make sure that this module is enabled if you encounter any issues

Restart Apache:

service httpd restart
service apache2 restart

The following one-liner will display number of Apache PIDs and sort them after memory usage:

ps aux | awk '$11 ~ /httpd/ {c++; SUM +=$6; print $6/1024" MB, PID:", $2|"sort -rn| head"} END {print c" Total Apache Processes"} END {print SUM/1024" MB Total Memory"} END {print "Top 10 Memory users:"}'

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

Here is how Apache needs to be configured to log real IP addresses:

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" forwarded
SetEnvIf X-Forwarded-For "." forwarded=1
CustomLog logs/access_log combined env=!forwarded
CustomLog logs/access_log forwarded env=forwarded

Basically you were adding a new LogFormat with X-Forwarded-For value named forwarded and configure CustomLog to use combined if the request was sent directly to the server: such as a curl cronjob, or forwarded if the request was passed through the Load Balancer.

Here is a quick trick for using tcpdump to get all MySQL queries running on a server:

tcpdump -i $INTERFACE -s 0 -l -w - dst port 3306 | strings | perl -e '
while(<>) { chomp; next if /^[^ ]+[ ]*$/;
  if(/^(SELECT|UPDATE|DELETE|INSERT|SET|COMMIT|ROLLBACK|CREATE|DROP|ALTER)/i) {
    if (defined $q) { print "$q\n"; }
    $q=$_;
  } else {
    $_ =~ s/^[ \t]+//; $q.=" $_";
  }
}'

I think I got this from Percona website

This query will display both the total size and number of tables for each MySQL engine.

SELECT engine,ROUND(SUM(index_length+data_length)/1024/1024,2) AS 'size in MB',count(engine) FROM information_schema.tables GROUP BY engine;

A nice MySQL query to get all FTP users and passwords from Plesk:

# mysql -u admin -p$(cat /etc/psa/.psa.shadow) -D psa
SELECT REPLACE(sys_users.home,'/home/http/vhosts/','') AS domain,
sys_users.login, accounts.password
FROM sys_users
LEFT JOIN accounts on sys_users.account_id=accounts.id
ORDER BY sys_users.home ASC;