Friday, August 14, 2015

Bash commands

awk '/string_to_find/{ print NR; exit }' fileName   ----- for finding the line number of the matching string

tail --lines=+line_number_here fileName | less  -----  for displaying the lines of a file starting at a specific line

Tuesday, October 7, 2014

How to Log Client IP AND X-Forwarded-For IP in Apache

When you use Load Balancer or any proxy in-front of you web servers, the visitor IP address is replaced with the load balancer or proxy IP. You will need to make the following changes in your apache config in order to retrieve the client IPs:

In your apache config (e.g. httpd.conf)

LogFormat "%a %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\"" proxy
SetEnvIf X-Forwarded-For "^.*\..*\..*\..*" forwarded
SetEnvIf X-Forwarded-For "^$" noforwarded


If you use a single log file, then change the CustomLog in httpd.conf:
CustomLog logs/access_log combined env=noforwarded
CustomLog logs/access_log proxy env=forwarded


If you are using separate log files for each domain, then you should change the CustomLog in your vhosts config:
CustomLog /log_location/domain-name.com-access_log combined env=noforwarded
CustomLog
/log_location/domain-name.com-access_log proxy env=forwarded

Tuesday, September 30, 2014

How to find only files with specified last modified date?

To find all files in the current directory that have been modified since yesterday (24 hours ago) use:

find . -maxdepth 1 -mtime -1

Saturday, September 27, 2014

Friday, September 26, 2014

iptables

Add a rule
sudo iptables -A INPUT -p tcp -m state --state NEW,RELATED,ESTABLISHED -m tcp --dport 1234 -j ACCEPT 

Insert a rule to a specific line
sudo iptables -I INPUT 7 -p tcp -m state --state NEW,RELATED,ESTABLISHED -m tcp --dport 1234 -j ACCEPT

The above example inserts the rule before the line 7. If no line number is defined it will use the default value of 1 in which your rule is inserted at the topmost line. 

Delete a rule
-D INPUT -p tcp -m state --state NEW,RELATED,ESTABLISHED -m tcp --dport 1234 -j ACCEPT 

Saving rules
service iptables save

Restart service
service iptables restart

Thursday, August 7, 2014

How to Unban IP manually in Fail2ban

fail2ban-client set [jail] unbanip [ip]