How to view history along with the date and time ?

Issue below command :

# export HISTTIMEFORMAT=”%d:%m:%Y %T “

After that check the history :

# history | less
1 07:10:2014 10:11:31 vi test.txt
2 08:10:2014 11:31:20 service httpd restart
3 09:10:2014 09:24:12 vi /etc/resolv.conf
4 09:40:2014 08:25:21 yum -y install vsftpd

If the system get rebooted then this changes will get reset, for the permanent change need to add the entry in .bash_profile file.

# vi /root/.bash_profile
export HISTTIMEFORMAT=”%d:%m:%Y %T “

Save and exit.

Done 🙂

How to recover file when accidentally typed ci instead of vi?

If you accidentally enter the command ci instead of the vi then ? Will file get lost ?

[root@~]# ci httpd.conf
httpd.conf,v <– httpd.conf
enter description, terminated with single ‘.’ or end of file:
NOTE: This is NOT the log message!
>>
initial revision: 1.1
done

Its “Checked In” to the source code control system , now original file replaced with a version controlled system with ,v on the end of the filename.

To recover of the original file need to “Check Out” file with the co command:

[root@~]# co httpd.conf
httpd.conf,v –> httpd.conf
revision 1.1
done
[root@~]#

The file has not lost and we can recover it 🙂

What is the swappiness value in Linux and how to modify it?

It is one of feature available in Linux operating system which is responsible  degree to which the kernel prefers to swap memory to fulfil the system memory requirement. The parameter value varies from 0 to 100. In Linux system default value set to 60. A low value result kernel will try to avoid swapping unless there is almost RAM is full for process on system. The higher value would force kernel aggressively to moved out memory pages from the physical memory to swap memory.

If we set higher value then there may be negative impact on the system cause when system accessing the swap memory (which is part of the hard drive) then system operations being bit slow as compare to the RAM as RAM is faster than the hard drive. To read/write system directly on hard drive will take time so it’s best to avoid transfer active memory pages to swap memory aggressively.

 

-To check the default value use command:

# cat /proc/sys/vm/swappiness

60

-To modify the parameter use command:

#echo 40 > /proc/sys/vm/swappiness

-To affect the changes use :

# sysctl -p

-Verify the parameter use :

# sysctl -a | grep swappiness

   vm.swappiness = 40

 

Done 🙂

 

Redirection rules

To add the redirection rule we can use the domain .htaccess file or add the rule in the domain apache configuration file both way it should work.

 

-Redirect all www traffic to a NON www :

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.domainname.com [NC]
RewriteRule ^(.*)$ http://domainname.com/$1 [L,R=301]

-Redirect all NON www traffic to a www :

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domainname.com [NC]
RewriteRule ^(.*)$ http://www.domainname.com/$1 [L,R=301]

-If there is SSL installed on your domain and wants to redirect ALL traffic from HTTP to an HTTPS :

RewriteEngine On
RewriteCond %{SERVER_PORT} !443
RewriteRule ^(.*)$ https://www.domainname.com/$1 [R]

How to increase the ulimit and file descriptors limit in linux.

If you get the error “too many open files” , please check below steps :

The file-max is the maximum File Descriptors (FD). It is a kernel setting enforced at the system level. The ulimit is enforced at the user level. It should be configured to be less than file-max.

The default settings are  very low , for high performance servers it  should be increased. The default settings assume that the several user share the same system and limit is  sufficient.

To change the file descriptor setting, edit the kernel parameter file /etc/sysctl.conf. Add line fs.file-max=[new value] to it.

for eg.     # vi /etc/sysctl.conf    and enter       fs.file-max = 400000

To apply the changes :
#sysctl -p

To change the ulimit setting, edit the file /etc/security/limits.conf and set the hard and soft limits in it :
for eg.# vi /etc/security/limits.conf
* soft nofile 40000
* hard nofile 40000

After changed reboot the server.
# reboot

Now,test system settings using the below commands:

#ulimit -a

To check the current open file descriptor limit:
# more /proc/sys/fs/file-max

OR sysctl -a | grep fs.file-max

To find out how many file descriptors are currently being used:

# more /proc/sys/fs/file-nr

To find out how many files are currently open:

# lsof | wc -l

That’s it 🙂

Find out and prevent DDOS attack on the server.

“A denial-of-service attack (DoS attack) or distributed denial-of-service attack (DDoS attack) is a attack in which the server resources become unavailable to its  users.”

To check and prevent DDOS follow below steps:

1.Below command will show you the list of IP’s which have logged in is maximum number of connections to your server.

#netstat -anp |grep ‘tcp\|udp’ | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n

2.The number of active connections that your server currently has.

#netstat -n | grep :80 |wc -l

3.There are many attacker who start the attack to gain the server connection and then do no reply until request is time out.If server active connection is more than the 100 then three will be a possibility of  sync attack on server.

#netstat -n | grep :80 | grep SYN |wc -l

4.You can block the IP and cross check it.

# route -n |grep IP address

OR

#  iptables -A INPUT 1 -s IPADRESS -j DROP/REJECT

After that save and restart the iptables.

5.At the end KILL all httpd connection and then restart httpd service.

#killall -KILL httpd
#service httpd startssl

Done:)

To find out spammer on EXIM mail server.

Follow the below steps :

1. To check the number of emails present in the queue:

# exim -bpc

2. To check the emails present in the queue with the mail id and sender ID:

# exim -bp
# exim -bp | less

3. To view the header of a particular email using mail ID:

# exim -MvH mail_id

4.  To view the body of a particular email using mail ID:

# exim -Mvb mail_id

5. To view a message’s logs:

# exim -Mvl mail_id

6. To trace path:

# exim -d -bt user@domain.com

7. To get sorted list of email sender in exim queue:

# exim -bpr | grep “<” | awk {‘print $4’} |cut -d “<” -f 2 | cut -d “>” -f 1 | sort -n | uniq -c| sort -n

8. To check the script that will originate spam mails:

# grep “cwd=” /var/log/exim_mainlog|awk ‘{for(i=1;i<=10;i++){print $i}}’|sort| uniq -c|grep cwd|sort -n

9. If we need to find out exact spamming script. To do this, run following command:

# ps auxwwwe | grep user | grep –color=always “/home/user/public_html/templates/” | head

10.  To delete the emails of a specific user:

# grep -lr ‘user@domain.com’ /var/spool/exim/input/ | sed -e ‘s/^.*\/\([a-zA-Z0-9-]*\)-[DH]$/\1/g’ | xargs exim -Mrm

# exim -bp | grep “user_email-account” | awk ‘{print $3}’ | xargs exim -Mrm

11. To delete Frozen emails from the email queue:

# grep -R -l ‘*** Frozen’ /var/spool/exim/msglog/*|cut -b26-|xargs exim -Mrm
# exim -bp| grep frozen | awk ‘{print $3}’| xargs exim -Mrm
# exiqgrep -z -i | xargs exim -Mrm

12.  To delete Spam emails from the email queue:

#  grep -R -l [SPAM] /var/spool/exim/msglog/*|cut -b26-|xargs exim -Mrm

13. To check the no. of frozen mails:

# exiqgrep -z -c

14. To check exim logs:

# tail -f /var/log/exim_mainlog

15. Force delivery of one message:

# exim -M mail_id

16. Force another queue run:

# exim -qf

17. Force another queue run and attempt to flush frozen messages:

# exim -qff

Optimize MySQL Databases on Plesk server.

1. Open an SSH connection to the server and log in as the root user.
2. Once logged in as the root user, create a file name optimize.sh with the following data:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#!/bin/sh
DBNAME=$2
if [ -e /etc/psa/.psa.shadow ]; then
PLESKJAWNS=”-uadmin -p`cat /etc/psa/.psa.shadow`”
else
PLESKJAWNS=””
fi

printUsage() {
echo “Usage: $0″
echo ” –optimize <dbname>”
echo ” –optimizeall”
echo ” –repair <dbname>”
echo ” –repairall”
return
}
doAllTables() {
# get the table names
TABLENAMES=`mysql $PLESKJAWNS -D $DBNAME -e “SHOW TABLES\G;”|grep ‘Tables_in_’|sed -n ‘s/.*Tables_in_.*: \([_0-9A-Za-z]*\).*/\1/p’`
# loop through the tables and optimize them
for TABLENAME in $TABLENAMES
do
mysql $PLESKJAWNS -D $DBNAME -e “$DBCMD TABLE $TABLENAME;”
done
}
doAllDatabases() {
# get the database names
DATABASES=`mysql $PLESKJAWNS -e “Show Databases” | grep -v + | grep -v psa`
for DATABASE in $DATABASES
do
# get the table names
TABLENAMES=`mysql $PLESKJAWNS -D $DATABASE -e “SHOW TABLES\G;”|grep ‘Tables_in_’|sed -n ‘s/.*Tables_in_.*: \([_0-9A-Za-z]*\).*/\1/p’`
# loop through the tables and optimize them
for TABLENAME in $TABLENAMES
do
mysql $PLESKJAWNS -D $DATABASE -e “$DBCMD TABLE $TABLENAME;”
done
done
}
if [ $# -eq 0 ] ; then
printUsage
exit 1
fi
case $1 in
–optimize) DBCMD=OPTIMIZE; doAllTables;;
–optimizeall) DBCMD=OPTIMIZE; doAllDatabases;;
–repair) DBCMD=REPAIR; doAllTables;;
–repairall) DBCMD=REPAIR; doAllDatabases;;
–help) printUsage; exit 1;;
*) printUsage; exit 1;;
esac
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 Update the permissions on the file using the following command:
[root@server~]# chmod +x optimize.sh

To repair and optimize a single database, run the following commands:

[root@server~]# ./optimize.sh –repair DBNAME
[root@server~]# ./optimize.sh –optimize DBNAME

To repair and optimize a all databases, run the following commands:

[root@server~]# ./optimize.sh –repairall
[root@server~]# ./optimize.sh –optimizeall

Optimize MySQL Databases on Cpanel/WHM server.

Optimize MySQL Databases on Cpanel/WHM server :

1. Open an SSH connection to the server and log in as the root user.

To repair and optimize a single database, run the following commands:

#mysqlcheck -u DATABASEUSER -p –auto-repair –check –optimize –all-databases

It will ask for the DATABASE user password :

To repair and optimize a all databases, run the following commands:

#mysqlcheck –auto-repair –check –optimize –all-databases

It will repair and optimize your Mysql databases, Restart MySQL after you’ve done the above:

#/etc/init.d/mysql restart

Done:)

Administrative Commands

find processes using maximum memory :

#ps aux | tr -s ” ” | cut -d ” ” -f 4,11 | sort -n -r | uniq | head -10

Find Out The Top 10 Memory Consuming Process

# ps -auxf | sort -nr -k 4 | head -10

Find Out top 10 CPU Consuming Process

# ps -auxf | sort -nr -k 3 | head -10

To clear cache :

#sync; echo 3 > /proc/sys/vm/drop_caches

To  display count  of files in current directory :

#find . -type f | wc -l

To display count of folders in current directory :

#find . -type d | wc -l

Display cpu model name:

#cat /proc/cpuinfo | grep -i ‘Model’  or   grep -i ‘Model’ /proc/cpuinfo