Get Server CPU Usage of A Specific User

Suppose your user name is jimgreen and you can run the following command to get a very specific CPU usage of it in real time:

top -b -n 1 -u jimgreen | awk 'NR>7 { sum += $9; } END { print sum; }'

You can even further capture the usage metric in PHP and do conditional actions:
$usage = shell_exec("top -b -n 1 -u jimgreen | awk 'NR>7 { sum += $9; } END { print sum; }'");
Now $usage contains the CPU usage of the user jimgreen.

How to count the number of files in a tar archive?

So you have a tar archive file and you want to know the number of files in it without expanding it. You know the text counter program ‘wc’? You can count the number of lines from the input by:

wc -l file.txt

You know how to list the content (files and directories) of a tar archive?

tar --list example.tar

Now you just combine these two to count the number of lines of the files list of the tar archive:

tar --list example.tar | wc -l

So that you have the number of files in the tar file example.tar. The pipe means relaying the output of the previous command to the next command as input.

Terse yet extremely to the point Linux Security tips and step to step guide

If you are just like me who is disgusted of reading through large chunks of texts for the only particular thing that he’s interested, securing Linux can’t be any more straightforward with this exhaustive list written by Kurt Seifried. Specially made for Linux novices who are just starting to harden his servers against malicious attempts, it is a good read not only because it’s useful but also because it’s easy to follow and digest.

Here we go:

How to find out the Linux distro release and the version number of your server?

To check the version date of your OS (Linux distros):

uname -v

To find out the release:

uname -r

To display and show the complete kernel signature of your hosting server:

uname -a

Something like this will output similar information concerning your Linux kernel:

cat /proc/version

If you haven’t had the uname utility installed, you can get the kernel release version by:

cat /proc/sys/kernel/osrelease

You may also be interested in some other useful things to see within /proc.

Change and Increase the Max PHP File Uploading Limit

The default php configuration comes with a hard cap of 2MB on the size of uploaded file determined by the php.ini directive upload_max_filesize in conjunction with post_max_size. The maximum uploading size of a file is the lower. Therefore, to increase the uploading cap and raise uploading limit, you will need to edit those 2 directives in php.ini.

The location of php.ini varies distribution by distribution, in this example, with Ubuntu 9.04 Jaunty, php.ini is located at /etc/php5/apache2/php.ini, so

sudo vi /etc/php5/apache2/php.ini

Press / to find upload_max_filesize and change it to, say 8M:

upload_max_filesize = 8M

Press ESC, :, wq and Enter. Now the php.ini is saved with the new uploading file limit. Reload apache2 to read the new configurations:

sudo /etc/init.d/apache2 reload

Now you should be able to upload any file up to 8MB in size, in case you need larger uploading limit, in addition to change upload_max_filesize to, say, 16M, you must also edit post_max_size to more than or equaling to 16M because file uploads are processed through HTTP POST method.

Best Linux Server Administration Books for Learning Linux

In the web hosting industry, Linux is undoubtedly the No. 1 server OS that has been used and talked about. With so many differently branded distributions such as Ubuntu, Debian and CentOS, the basic part has always been the same – the file system, the open source packages, the commands, the shell programming and so forth. It’s a hard nut to crack, but with a few good guides and books, you will soon be on your way of becoming a professional server administration.

Web Developers / Designers’ Books:

  1. Best HTML Books
  2. Best CSS Books
  3. Best JavaScript Books
  4. Best PHP Books
  5. Best MySQL Books
  6. Best Linux Books
  7. Best Apache Books (mod_rewrite Books)
  8. Best Web Hosting Books

A Practical Guide to Linux(R) Commands, Editors, and Shell Programming

A Practical Guide to Linux(R) Commands, Editors, and Shell Programming

Linux Pocket Guide

Linux Pocket Guide

Linux in a Nutshell, 5th Edition

Linux in a Nutshell, 5th Edition

Linux Administration: A Beginner’s Guide, Fifth Edition

Linux Administration A Beginner's Guide, Fifth Edition

Practical Guide to Ubuntu Linux (Versions 8.10 and 8.04), A (2nd Edition)

Practical Guide to Ubuntu Linux (Versions 8.10 and 8.04), A (2nd Edition)

How Linux Works

How Linux Works

Linux Administration Handbook (2nd Edition)

Linux Administration Handbook (2nd Edition)

Ubuntu Linux Toolbox: 1000+ Commands for Ubuntu and Debian Power Users

Ubuntu Linux Toolbox 1000  Commands for Ubuntu and Debian Power Users

Best Web Hosting Books to Learn about Web Hosting

Web hosting and similar IT infrastructure outsourcing can be intimidating to anyone not tech-savvy enough. These are some of the best readings recommended by book readers from Amazon concerning this technical issue and related decision making process. Some books teach you to use web hosting and some tell you how to run a web hosting business.

Web Developers / Designers’ Books:

  1. Best HTML Books
  2. Best CSS Books
  3. Best JavaScript Books
  4. Best PHP Books
  5. Best MySQL Books
  6. Best Linux Books
  7. Best Apache Books (mod_rewrite Books)
  8. Best Web Hosting Books

Strategies for Web Hosting and Managed Services

Strategies for Web Hosting and Managed Services

Web Host Manager Administration Guide: Run your web host with the popular WebHost Manager software

Web Host Manager Administration Guide Run your web host with the popular WebHost Manager software

Web Hosting: A complete strategy for delivering high quality Web hosting services

web hosting

How to Host your own Web Server

how to host your own web server

The Complete Web Hosting Kit Professional

The Complete Web Hosting Kit Professional [CD-ROM] (CD-ROM)

The Web Hosting Manager

The Web Hosting Manager

Understanding Linux Web Hosting

Understanding Linux Web Hosting

Actually there are not too many books straightly dealing with the topic of website hosting, but a lot on its cousin areas such as Linux, Windows, Apache, PHP and MySQL.

Installing FTP (vsFTPd) Service on Ubuntu Server

FTP is an indispensable feature of servers that host and serve websites as it enables us to easily upload stuff to the remote server. On a Ubuntu server, with a little help of aptitude command (the package management program descended from Debian), you can install the most simple yet most common FTP daemon program for your server: vsFTPd.

apt-get install vsftpd

It is started automatically after successful installation. Stop it:

/etc/init.d/vsftpd stop

So that you can customize the configuration file:

vi /etc/vsftpd.conf

And make it look like:






Restart the FTP service:

/etc/init.d/vsftpd start

Now you can try connecting to the FTP and transferring some stuff.

vi code highlighting: change the default comments color from dark blue to light blue

The default colors for comments (texts in /* */ or following // or #, …) in vi code highlighting are a little too dark. Ever wanted to make it more recognizable in SSH console?

Find and edit /etc/vim/vimrc with vi:

vi /etc/vim/vimrc

And add in this line:

colorscheme desert

Wherein desert is one of the available color schemes vim comes with. Now we will need to edit the actual color scheme file and change the highlighting colors:



hi Comment ctermfg=darkcyan


hi Comment ctermfg=blue

Save the change and exit. Run:

source /etc/vim/vimrc

And the changes will now take effect.

The default directory color of ls –color is also too dark, you can learn how to change the default directory color of ls –color.

Use Shell Environment Variable LS_COLORS to Change Directory Listing Colors of ls –color

After you have enabled the color switch of ls command in shell console, it’s nice but some may complain that the deep blue color of the directories are too dark to recognize sometimes. Let’s change that.

Just open up the .profile or .bash_profile file under your home directory and put this line in it:

export LS_COLORS='di=01;34'

Done! Now the color of the ls directory listings is much lighter and easier to recognize. There’s also a tip of how to change the default dark color for comments in vi text editor.

Linux SSH commands to show and monitor server resources and real-time performance: memory, swap, disk usage, CPU usage and I/O …

Below are a few general commands found in most popular Linux distros which you can use via SSH to check the status of your hosting server.

To show used and available RAM memory and swap space usage:

free -m

To show current disk storage usage by mounted device:


To show disk usage statistics of the current directory by directories and files:


To show the hard disk space a directory or a file takes up:

du filename

To show the length of time this server has been up and the server loads in the past 1 minute, 5 minutes and 15 minutes:


To display a real-time updated server resource usage including: server uptime, user logged on, load average, current tasks, CPU usage, memory usage and swap usage:


To display a list of real-time active or sleeping processes your server is up to:


To show some information about the current status of virtual memory, CPU usage, I/O usage:


This is also a good tool to find out system performance bottlenecks.

To display currently logged on users on the system:




To print a full screen text graph of the server load refreshed every few seconds:


If you are on shared hosting, chances are your server usage has been imposed some hard limits such as the largest amount of files / directories possible and the hard storage limit. View them by:


Typical iptables Firewall Rules for a Server that Hosts Websites

iptables is a rather handy tool to protect your server from unwanted and potentially malicious connection attempts. To list the current rules, run in SSH:

iptables -L

A typical set of firewall rules set by iptables on a simple server, be it VPS or dedicated, for hosting and serving websites should be like this:

iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -i ! lo -d -j REJECT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp -m multiport --dports 8001,8002,8003,8004,8005,8006,8007,8008,8009,8010 -j ACCEPT
iptables -A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
iptables -A INPUT -j REJECT
iptables -A FORWARD -j REJECT

Which enables

  1. ports 80 and 443 for web pages serving via HTTP and HTTPS
  2. port 21 and a series of tcp ports for FTP (passive mode) so that you can upload stuff to the server with your favorite FTP client
  3. port 22 for SSH access which can be modified for more security. If you have altered the default SSH connection port 22 to a random one, make sure you also change the port in the iptables rules set accordingly or the server will reject you.
  4. port 3306 for MySQL database server. Note that you may or may not need to open port 3306 for MySQL. For example, if you use ‘localhost’ as database server, there’d be no need most of the time.

And disables everything else.

These commands will only be in effect for the current session, once the server is restarted, all rules will be lost. In order to save these rules and make the server automatically load and apply them every time you reboot, write them into a file to be loaded upon every system start. Run:

iptables-save > /etc/iptables.up.rules

Command iptables-save saves the rule set to the file /etc/iptables.up.rules from the memory. Now configure the server to read and apply the rule set file /etc/iptables.up.rules every time it starts:

nano /etc/network/interfaces

And add a line immediately below ‘iface lo inet loopback’:

pre-up iptables-restore < /etc/iptables.up.rules

Now you are set. Reboot the server and see if all takes effect.

Customize or change the default SSH port 22 to a random custom one of your choice

By default, all newly set up servers listen and accept SSH login on port 22 which is known universally. To make it a little harder for hackers to break into your user account, one of the first steps you want to take is to change the default SSH port to a different on that’s randomly chosen by you.

To do this, simply modify the sshd configuration file by:

nano /etc/sshd/sshd_config

For novice SSH users, nano is more intuitive than vi. After loading the file in the editor, find and change this line:

Port 22


Port 8433

Ctrl + o and ctrl + x should save the change and get you out of the editor.

The port number can be anything between 1024 and 65535, inclusive. You can make it instantly in effect by reloading the new configurations:

/etc/init.d/ssh reload

Now the server will only accept SSH accesses on the port 8433. After modifying this, make sure you also change the remote port setting in your local SSH client or it will be rejected by the hosting server.

How to Change Login Password of Linux SSH User Account?

If you are root, you can change anyone’s password by:

passwd someuser

Wherein someuser is the user name of the account. It will prompt you to enter the new password twice.

If you are yourself and logged in with your own SSH account, you can also change your own password by simply:


It will also ask you to type your new password twice. Now you can log into your SSH with the new password.