VPS Notes

Beginner Tutorial - Logging In to your VPS and issuing basic Linux commands

Only basic commands can be done in your VPS control panel. To really use the full potential of the server, you need to log in to it's command line. From the command line, you can do many things such as: install software, modify configuration, and manipulate data.

Connecting from Windows

Assuming you are using Windows, you need a program called PuTTY. You can find and download the latest version from here. Here is also a direct link of the exe.
Open PuTTY, type the IPv4 address from your welcome email in the Host Name section, and then click open.
w01
If a security alert pops up, just click yes to skip it.
w02
A "login as: " message will be shown, type root and then ENTER. Type your root password and click again ENTER.
w03
And you will be logged in with some welcome message shown.
w04

Basic Linux Commands

Many people are intimidated of Linux because much of management are done through a command line. There are many commands to memorize, without a GUI to guide. In reality, it's not really difficult. All you need is to start with a small set of commands and your skills will grow through time as the needs arise.
File System
In Windows, the file system starts with C:\ drive. In Linux, the counterpart is the "/" folder. It is the top most directory, and everything else is a descendant of it. It has several subdirectories, but the most commonly used are:
  • /etc - this is where configuration files of programs resides. It is like the C:\Windows\ folder in Windows.
  • /bin and /sbin - this is where common executable programs are found. Executables are like *.exe files in Windows
  • /usr - this is where user programs are located, similar to C:\Program Files.
  • /home - is where user data are stored, like C:\Users folder in Windows.
passwd - this is the first command you want to issue the first time you log to your VPS for security purposes. It will help you change the password of the account you used. Just enter your current password, and then the new password you wish to use.
$ passwd
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully

pwd - prints the current directory where we are. Command result below shows that we are at "/root", which is the home folder of the root user:
$ pwd
/root

ls - this is like the dir command in Windows. It will show all the contents of a folder. For example, the contents of "/var" are shown:
$ ls /var
backups  cache  crash  lib  local  lock  log  mail  metrics  opt  run  spool  tmp
If you don't specify the directory, it will show the contents of the current working directory.
free - Display amount of free and used memory in the system. In the example below, the parameter "-m" means print in megabytes. Only take notice of the second like with the text "buffers/cache". In the example below, it shows we used 151MB RAM and there are still 360Mb RAM available.
$ free -m
             total       used       free     shared    buffers     cached
Mem:           512        373        138          0          0        221
-/+ buffers/cache:        151        360
Swap:            0          0          0

df - displays usage of disk space. The parameter "-h" means to display in human readable form. In the example below, it shows we used 16GB disk space and there are 45Gb left that we can use.
$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/simfs       60G   16G   45G  26% /
none            128M  4.0K  128M   1% /dev
none             26M  988K   25M   4% /run
none            5.0M     0  5.0M   0% /run/lock
none            128M     0  128M   0% /run/shm

wget - this is a tool to download a file from the internet. Below is an example on how to download an image to your current directory. The parameter is just the URL of the file you wish to download:

wget http://peach.blender.org/wp-content/uploads/c
--2014-01-25 09:43:54--  http://peach.blender.org/wp-content/uploads/poster_rodents_small.jpg
Resolving peach.blender.org (peach.blender.org)... 82.94.213.220
Connecting to peach.blender.org (peach.blender.org)|82.94.213.220|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 70348 (69K) [image/jpeg]
Saving to: ‘poster_rodents_small.jpg’

100%[==============================================================================================================================>] 70,348 40.0KB/s in 1.7s

2014-01-25 09:44:02 (40.0 KB/s) - ‘poster_rodents_small.jpg’ saved [70348/70348]

uptime - is a command to tell you how long your system has been running. For example:
$ uptime
 09:54:49 up 42 days,  8:34,  1 user,  load average: 0.00, 0.03, 0.06
This tells us that the VPS has been running for 42 days. The load average shows how busy the system has been for the past 1, 5, and 15 minutes (hence the 3 values shown 0.00, 0.03, and 0.06). Normally, you want the value to be below 1.0.
ps - this is a command to see currently running programs. A common parameter to it is "aux". This is the output in one of my VPS running a web server:
$ ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1   3352   656 ?        Ss    2013   0:00 init
root         2  0.0  0.0      0     0 ?        S     2013   0:00 [kthreadd/954]
root         3  0.0  0.0      0     0 ?        S     2013   0:00 [khelper/954]
root       114  0.0  0.0   2796    12 ?        Ss    2013   0:00 /sbin/udevd --daemon
root       155  0.0  0.0   2792    12 ?        S     2013   0:00 /sbin/udevd --daemon
root       156  0.0  0.0   2792     8 ?        S     2013   0:00 /sbin/udevd --daemon
root       299  0.0  0.0   2516     8 ?        Ss    2013   0:00 /usr/sbin/xinetd -dontfork -pidfile /var/run/xinetd.pid -stayalive -inetd_compat -inetd_ipv6
root       301  0.0  0.0   2572   380 ?        Ss    2013   0:05 cron
root       319  0.0  0.0   2196   460 ?        S     2013   0:09 /usr/sbin/syslogd
mysql      339  0.0  9.2 253604 48652 ?        Ssl   2013  22:08 /usr/sbin/mysqld
www-data 14997  4.3  4.2  53500 22288 ?        S    13:23   0:00 php-fpm: pool www                                         
root     15028  0.0  0.5   9568  3048 ?        Ss   13:24   0:00 sshd: jon [priv]    
jon      15040  0.0  0.2   9568  1448 ?        S    13:24   0:00 sshd: jon@pts/0     
jon      15041  0.0  0.3   3448  1776 pts/0    Ss   13:24   0:00 -bash
www-data 15051  0.0  0.5  37144  2720 ?        S    13:24   0:00 php-fpm: pool www                                         
jon      15052  0.0  0.2   2816  1056 pts/0    R+   13:24   0:00 ps aux
root     21740  0.0  0.1   6636   788 ?        Ss   Jan16   0:01 /usr/sbin/sshd -D
root     25728  0.0  0.3  57972  1972 ?        Ss   Jan19   0:00 nginx: master process /usr/sbin/nginx
www-data 25729  0.0  0.6  58480  3256 ?        S    Jan19   1:01 nginx: worker process
www-data 25730  0.0  0.6  58504  3296 ?        S    Jan19   1:02 nginx: worker process
www-data 25732  0.0  0.6  58480  3248 ?        S    Jan19   1:01 nginx: worker process
www-data 25733  0.0  0.6  58476  3236 ?        S    Jan19   1:03 nginx: worker process
root     25753  0.0  0.4  37144  2612 ?        Ss   Jan19   0:31 php-fpm: master process (/etc/php5/fpm/php-fpm.conf) 
As you could see, there are only a few programs required to run on your server to host a working website. The database is mysql, the webserver is nginx, and the one executing the PHP scripts is php-fpm. On a KVM based VPS, you will see more programs running. But that is normal.

Installing Programs

In Windows, you need to find the website of an application, download the *.exe installer file, and install it to your system. In Ubuntu, many programs are available in default repositories. You just need to type some commands to search or install them. No need to go to any websites and download manually.

Search for available programs - if we want to look for text editors, we would issue a command like:

$ apt-cache search "text editor"
gedit - official text editor of the GNOME desktop environment
gedit-common - official text editor of the GNOME desktop environment (support files)
gedit-dev - official text editor of the GNOME desktop environment (development files)
mate-core - MATE Desktop Environment (essential components)
mate-text-editor - official text editor of the MATE desktop environment (transitional package)
pluma - official text editor of the MATE desktop environment
pluma-common - official text editor of the MATE desktop environment (common files)
pluma-dbg - official text editor of the MATE desktop environment (debugging symbols)
pluma-dev - official text editor of the MATE desktop environment (development files)
sublime-text-installer - Sublime Text 3 installer - beta build
kate - K Advanced Text Editor
katepart - kate KPart
kwrite - simple graphical text editor
ed - classic UNIX line editor
x11-apps - X applications
nano - small, friendly text editor inspired by Pico

"apt-cache search" means we want to search available software given a keyword. The system will list all matching programs, one line each. If for example we are interested with the "nano" program shown at the bottom of the list, use the following command to install it:
$ apt-get install nano
And the nano program will be installed. If you want to uninstall it later, use this command:
$ apt-get remove nano

But to fully uninstall it together with associated configuration files, use this:

$ apt-get --purge remove nano

Editing Text Files

The most common thing you will do in Linux is to edit configuration files. There are many popular editors available, but I prefer to start with "nano". Because this is very simple to use and with the least number of things to memorize. The installation procedure of nano is shown above.
To edit a file called test.txt, type:
$ nano test.txt
It will open the editor. If the file does not exists, it will have a blank content. Otherwise, the current content will be shown. Just start typing using letters, numbers, and characters from the keyboard. You may also use the arrow keys to change the location of the cursor:
w05
When you want to save, use "Ctrl + o" and then ENTER. If you want to save, use "Ctrl + x" and then ENTER.w06
Tags: nano, putty, ssh