Server Administration & Management

TOP Command

If your server is under a high load, the command

# top

can provide you with real time information about the top CPU and memory using processes on the server. Top also has an interactive interface, so you can control the output. Some of these options are listed below.

P sort tasks by CPU usage (default).

M sort tasks by resident memory usage.

T sort tasks by time / cumulative time.

m toggle display of memory information. (on/off)

l toggle display of load average and uptime information. (on/off)

c toggle display of command name or full command line. (on/off)

More information on the process ‘top’ can be found by running the command:

# man top


The top command gives a list of the most active 20 processes in terms of CPU usage .

Display is updated every 5 seconds as a default value, you can change it by pressing d while top is running, enter the new display value in seconds, and press enter. The first line shows the time, for how long the computer has been running, number of users, CPU average load during the last 1minute, 5 minutes, 15 minutes. Again every terminal window is counted as a user, and this is the same info you get using uptime command.

The second line speaks for it self , some statistics of the processes.

The Third line is the CPU usage stats,

us “user” is the percentage of CPU time used by the user.

sy “system” the percentage of CPU time used by the kernel.

ni “Niced” is that used by the niced processes ( Niced processes are those which have a positive nice value , which means they have a different priority, see this post).

id “idle” is that used by idle processes.

wa “waiting” is that percentage of CPU time which is used by the processes waiting for I/O.

hi “hardware interrupt”, si “software interrupts” are the percentage of CPU time for which it has been servicing hardware and software interrupts.

The fourth and fifth lines speak for them selves .

Now we have 12 columns:

PID is the process ID.
USER is the name of the user running the process.
PR is the Process Priority.
NI nice value a negative nice value means higher priority, a positive nice value means lower priority.
VIRT is the total amount of virtual memory used by the task.
S Process Status
The status of the task which can be one of:
’D’ = uninterruptible sleep
’R’ = running
’S’ = sleeping
’T’ = traced or stopped
’Z’ = zombie
%CPU is the percentage of CPU time used by the process.
%MEM physical memory share.

There are a bunch of Interactive commands which can be run by pressing :

k to kill a process by giving it is PID.
r to renice a process by giving it is PID.
d to change the display time interval, just enter the new value in seconds.
q to quit top.

April 30, 2012 Posted by | Tips & Tricks, Unix/Linux | , , , | Leave a comment

How to stop website to get injected from hackers using .htaccess

In now a days its very easy to inject any forum.You can secure your forum by using following code in your .htaccess

# Worm sign
BrowserMatchNoCase SpammerRobot bad_bot
BrowserMatchNoCase SecurityHoleRobot bad_bot

# spam bots
SetEnvIfNoCase User-Agent “^EmailSiphon” bad_bot
SetEnvIfNoCase User-Agent “^EmailWolf” bad_bot
SetEnvIfNoCase User-Agent “^ExtractorPro” bad_bot
SetEnvIfNoCase User-Agent “^CherryPicker” bad_bot
SetEnvIfNoCase User-Agent “^NICErsPRO” bad_bot
SetEnvIfNoCase User-Agent “^Teleport” bad_bot
SetEnvIfNoCase User-Agent “^EmailCollector” bad_bot

# plagarism bot
SetEnvIfNoCase User-Agent “^TurnitinBot” bad_bot

# IP bot
SetEnvIfNoCase User-Agent “^NPBot” bad_bot

# Worm sign
SetEnvIfNoCase User-Agent “^LWP::Simple” bad_bot
SetEnvIfNoCase User-Agent “^lwp-trivial” bad_bot
SetEnvIfNoCase User-Agent “^lwp” bad_bot
SetEnvIfNoCase User-Agent “^LWP” bad_bot

# Anti-Clickjacking Defence
Header append X-FRAME-OPTIONS “DENY”

# Worm sign
Order Deny,Allow
Deny from env=bad_bot

April 28, 2012 Posted by | htaccess, Security | , , | Leave a comment

WordPress default .htaccess file

The default wordpress .htaccess file code is as follows

# BEGIN wordpress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_fileNAME} !-f
RewriteCond %{REQUEST_fileNAME} !-d
RewriteRule . /index.php [L]
# END wordpress

April 26, 2012 Posted by | htaccess, WordPress | , | Leave a comment

How To redirect a main-domain to sub-domain using .htaccess

Here is the .htaccess code for redirecting the main domain http://unixserveradmin.com to sub-domain http://unixserveradmin.com/linux

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^unixserveradmin.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.unixserveradmin.com$
redirectMatch permanent ^/?$ http://www.unixserveradmin.com/linux

April 24, 2012 Posted by | htaccess | , | Leave a comment

How to parse php pages in html page using .htaccess

You can use following code in .htaccess file to parse php pages in html pages

RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html

To can check above code is working ir not? by creating one test page with following code

# vim test.html

< html>
< head>
< body>
< h1>
< ?php echo “WORKING FINE!”; ?>
< /h1>
< /body>
< /html>

April 22, 2012 Posted by | htaccess, PHP | , , | Leave a comment

How to redirect all users except your IP using .htaccess

Sometimes you didn’t want any visitors to see what you were doing, so you can set up a temporary blog at temp.mydomain.com. All you needed to do then was to redirect all visitors to that domain, but allow yourself to stay on the main domain of http://www.mydomain.com, and continue your maintenance work.

The solution was simple; You simply created a .htaccess file on your main domain, in the root folder. The htaccess contained the following information:

RewriteEngine on
RewriteCond $1 !^http://www.mydomain.com/temp
RewriteCond %{REMOTE_HOST} !^12.345.678.901
RewriteRule (.*) http://temp.mydomain.com/$1 [R=301,L]

It really is that simple to redirect all visitors, except you own IP, to a subdomain. The script above does the following:

1. Turn on the rewrite engine (no need to do this if it is already on in your htaccess).
2. Exclude the folder /temp from my rewrite (this is where the files for my subdomain are stored).
3. Exclude the IP address of 12.345.678.901 from my rewrite rule (you can find out your IP address by going to whatismyip).
4. I am then telling it to redirect everything (.*) to my subdomain http://temp.mydomain.com.

All you need to do is swap in the addresses for you subdomain files, your IP address, and your subdomain address. Simple as that!

April 20, 2012 Posted by | Firewall, htaccess | , , | Leave a comment

How to enable private PHP error logging by using .htaccess

To hide the PHP errors from visitors insert the following code in .htaccess file

# Disable php errors
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off

Once disable the error logs for visitors enable the private PHP error logging by using following code in the .htaccess file

# enable PHP error logging
php_flag log_errors on
php_value error_log /home/path/public_html/domain/PHP_errors.log

The PHP_errors.log file needs to be permission 755 or 777

April 18, 2012 Posted by | htaccess, PHP | , , | Leave a comment

How to change php for single hosting account using .htaccess

We can change the php for single hosting account by using following code in .htaccess file. There are few good values which should be change for specific script. Use following code in .htaccess to changes the values as per your requirement.

php_flag register_globals On
php_flag magic_quotes_gpc off
php_flag session.use_trans_sid off
php_flag session.use_only_cookies 1
php_flag session.bug_compat_warn off
php_flag session.use_only_cookies on

php_admin_flag safe_mode Off

php_value engine off
php_value magic_quotes_gpc off
php_value session.use_cookies 1
php_value post_max_size 20971520
php_value max_execution_time 600
php_value upload_max_filesize 12M
php_value magic_quotes_runtime Off

April 16, 2012 Posted by | htaccess, PHP | , , | Leave a comment

How to install Shoutcast server

Shoutcast is a free-of-charge audio homesteading solution. It permits anyone on the internet to broadcast audio from their PC to listeners across the Internet  or any other IP-based network.

Basically most of the people don’t like to run shoutcast as root as that can be really harmful due to that its better if we create shoutcast user and then run shoutcast on server. Following are the basic steps to install shoutcast on server

1.) Login to root

2.) adduser shoutcast

# useradd shoutcast

3.) passwd shoutcast

# passwd shoutcast

4.) Once you add user “shoutcast” login as the new user “shoutcast”.

5.) Lets wget shoutcast from the shoutcast.com provider

# wget ftp://ftp.skynet.be/mirror1/tucows.skynet.be/linux/files/shoutcast-1-9-2-linux-glibc6.tar.gz

6.) The extract shoutcast:

# tar -zxvf shoutcast-1-9-2-linux-glibc6.tar.gz

# rm -rf shoutcast-1-9-2-linux-glibc6.tar.gz

# mv shoutcast-1-9-2-linux-glibc6 shoutcast

# cd shoutcast

7.) Now, You need to edit the shoutcast configuration.

# vim sc_serv.conf


uncomment AdminPassword and set an admin password as per your requirement.

8.) Now you can save and start shoutcast and it will work perfectly for you without any problem.

9.) Steps to start shoutcast?

# ./sc_serv sc_serv.conf

April 14, 2012 Posted by | Tips & Tricks, Unix/Linux | , , , | Leave a comment

How to use yum behind proxy

Here are following steps to update yum, behind proxy running on internal network

To make yum command use proxy, your best best is to edit /etc/yum.conf and add your proxy server reference:

# vim /etc/yum.conf


You don’t have to restart anything but it may be a good idea to do yum clean all and then yum check-update:

# yum clean all

# yum update all

# yum check-update

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: be.mirror.eurid.eu
* epel: epel.uni-oldenburg.de
* extras: be.mirror.eurid.eu
* updates: centosa5-msync-dvd.centos.org
base                                         | 3.7 kB 00:00
base/primary_db                              | 4.5 MB 00:01
cr                                           | 3.0 kB 00:00
cr/primary_db                                | 1.2 kB 00:00
epel                                         | 3.4 kB 00:00
epel/primary_db                              | 3.7 MB 00:00
extras                                       | 3.5 kB 00:00
extras/primary_db                            | 6.3 kB 00:00
updates                                      | 3.5 kB 00:00
updates/primary_db                           | 1.8 MB 00:01

April 12, 2012 Posted by | Tips & Tricks, Unix/Linux | , , | Leave a comment

tune2fs – adjust tunable filesystem parameters

tune2fs command is one of the advanced unix commands which allows you to adjust various tunable parameters of the ext2/ext3 filesystems. Naturally, it also helps you confirm the existing parameters configured for your filesystems.

Confirm current filesystem parameters with tune2fs

# tune2fs -l /dev/sda1

Reserved space on a Unix filesystem

By default, every filesystem in Unix has some space reserved for the superuser (root). This means that no regular Unix user can fill your filesystem up to 100%, and so it’s always going to have enough free space to continue normal function.

As a standard, each filesystem has 5% of space reserved in this way. If you look at the above output, you may notice the following lines there, which regulate the space reservation:

# tune2fs -m 6 /dev/sda1

# tune2fs -m 5 /dev/sda1

Default block size for a filesystem

#  tune2fs -l /dev/sda1 | grep Block

Turn off auto hard disc boot scanning for ext3

# tune2fs -c -1 -i 0 /dev/sdXY

Show system installation date

# tune2fs -l $(df -P / | tail -n1 | cut -d’ ‘ -f1 ) | grep ‘Filesystem created:’

April 10, 2012 Posted by | Tips & Tricks, Unix/Linux | , , | 5 Comments

What type of Computer do you have

Prints the type of computer you have. I think this should be used more in distros and other applications because it is so easy to get. This can also be asked by tutorials as an easy way to get your base hardware.

# cat /sys/devices/virtual/dmi/id/board_name

April 8, 2012 Posted by | Tips & Tricks, Unix/Linux | , , | Leave a comment

How to displaying system temperature of System

# cat /proc/acpi/thermal_zone/THRM/temperature

temperature:             30 C

April 6, 2012 Posted by | Tips & Tricks, Unix/Linux | , , , | Leave a comment

How to monitor specific process (ie apache) using Top Command

How to monitor specific process (ie apache) using Top Command

# top -p `pidof httpd | awk ‘{gsub(/[ ]/,”,”);print}’`

April 4, 2012 Posted by | Apache, Tips & Tricks, Unix/Linux | , , , , | Leave a comment

How to check disk for bad sectors

Checks Hard disk drive or bad sectors, just like scandisk or chkdisk under some other operating system.

# badblocks -n -s /dev/sdX

April 2, 2012 Posted by | Tips & Tricks, Unix/Linux | , , , | Leave a comment