A Beginner's Guide to SSH

Difficulty: Easy
Description: A tutorial explaining the basics of using SSH and an SSH client such as PuTTY.
Author: Daniel St. Jules; Date Added: 2008-08-20; Views: 48742
SSH, or Secure Shell, is a popular network protocol that allows for the exchange of data using cryptography for additional security. It is most commonly used to log into a remote machine, such as a server, and execute commands via the command line. So, how is this useful to you, someone who I assume is not familiar with SSH? In the case that you may own a VPS or dedicated server, it will allow you to possess a greater control over your account, some highlights including the ability to modify configuration files which may not be available to you otherwise such as httpd.conf - this customization could allow for better optimization, increased performance, and as a result, a better bang for your buck.
So, what will you get out of this tutorial? You will learn how to connect to your server using PuTTY, a great and free SSH client, and will be introduced to some basic commands and various other useful commands. So, let us begin.
Once downloaded, which shouldn't take too long by the way as the installer is under 2MB, run it and install the client. The default values for the installer should be good enough, although you can change things if needed.

Here you'll need to specify the IP address of your server (ex: 192.168.100.1) You can also specify a port other than the 22 if need be, although most servers listen to it by default. After specifying these things, you may want to save the information for quicker access by clicking on Save, under "Load, save or delete a stored session". And now that you've done this, you can click on open to connect. At this point, a warning may come up saying that the server's host key is not cached in the registry. In most cases, simply press Yes to trust the host and continue.

Here it is evident that you'll need to specify a user to log in as. In many cases you'll be able to use the same user information as those made available to you for logging into your control panels such as plesk or cpanel. After typing in your username, press enter. It will then ask for a password. At this point, you should note that the password will not be displayed as you type. Nope, not even the usual *** asterisks. So, just input your password carefully as to avoid any typos and press enter to login.
Now you're in. Congratulations, you just opened up an SSH connection! From here on in you'll be able to input various commands using your keyboard. In order to input a command, simply type it and press enter. You should also note that the standard right click function has been replaced by the paste function, and so should be used if you want to copy-paste. When done using PuTTY, simply enter exit to log out and exit the client.
Change Directory (cd)
cd /path/to/directory/
cd .. will go up one directory
Make Directory (mkdir)
mkdir /path/to/directory/to/create/
Print Working Directory (pwd)
List the full path to the current directory
Listing Files (ls)
ls /path/to/directory/
ls -a list all files, including those starting with a period
ls -l list more information about the files
ls -al list all files, with additional information
ls -alR list all files and information in subdirectories too
Chmod (chmod)
Chmod 755 filename.html
Move or rename file (mv)
mv /path/to/oldfile.html /path/to/newfile.html
Remove file (rm)
rm filename.html
rm -rf will delete all files and subdirectories
Locate file (locate)
locate examplefile.html
Touch/Create file (touch)
touch /path/to/newfile.html
Edit file with VI editor (vi)
vi /path/to/firectory/filename.html
While using the VI editor, you can use press SHIFT + to use VI specific commands, including:
:q! quits the file without saving and exits vi
:w saves the file
:wq saves the file and exists vi
:LINENUMBER : 25 takes you to that line in the file
:$ : takes you to the last line of the file
:0 : takes you to the first line of the file
Top system processes (top)
Press Shift + M to sort by memory usage, or Shift + P for CPU.
For a comprehensive list of commands, please see http://www.ss64.com/bash/
cat /var/log/httpd/error_log
Modify Apache httpd.conf configuration file
vi /etc/httpd/conf/httpd.conf
Modify MySQL configuration file
vi /etc/my.cnf
Get number of files in directories and subdirectories
ls */ | wc –l
Find path to your php.ini file
php -i | grep php.ini
Restart Apache
service httpd restart
Restart MySQL
service mysql restart
Dump/backup MySQL database
mysqldump –-user username –-password=password mydatabase > /path/to/dump.sql
Show number of active connections to server (Apache)
netstat -an |grep :80 |wc -l
Show number of open connections to MySQL
mysqladmin processlist |wc -l
So, what will you get out of this tutorial? You will learn how to connect to your server using PuTTY, a great and free SSH client, and will be introduced to some basic commands and various other useful commands. So, let us begin.
Installing PuTTY
To start things off, you'll need to download an SSH client. I'd like to recommend PuTTY, although you could try a variety of clients, as found at http://en.wikipedia.org/wiki/Comparison_of_SSH_clients. But, for the purpose of this guide, you can download PuTTY at http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html. The windows executable installer should be good enough for any Windows user.Once downloaded, which shouldn't take too long by the way as the installer is under 2MB, run it and install the client. The default values for the installer should be good enough, although you can change things if needed.
Opening an SSH session using PuTTY
So now that PuTTY has been installed, start the client up by using the created shortcuts or going to Start->All Programs->PuTTY->PuTTY if installed with default settings. On start, the PuTTY configuration window will appear.
Here you'll need to specify the IP address of your server (ex: 192.168.100.1) You can also specify a port other than the 22 if need be, although most servers listen to it by default. After specifying these things, you may want to save the information for quicker access by clicking on Save, under "Load, save or delete a stored session". And now that you've done this, you can click on open to connect. At this point, a warning may come up saying that the server's host key is not cached in the registry. In most cases, simply press Yes to trust the host and continue.

Here it is evident that you'll need to specify a user to log in as. In many cases you'll be able to use the same user information as those made available to you for logging into your control panels such as plesk or cpanel. After typing in your username, press enter. It will then ask for a password. At this point, you should note that the password will not be displayed as you type. Nope, not even the usual *** asterisks. So, just input your password carefully as to avoid any typos and press enter to login.
Now you're in. Congratulations, you just opened up an SSH connection! From here on in you'll be able to input various commands using your keyboard. In order to input a command, simply type it and press enter. You should also note that the standard right click function has been replaced by the paste function, and so should be used if you want to copy-paste. When done using PuTTY, simply enter exit to log out and exit the client.
Basic Commands
Here is a list of some basic commands, along with some arguments, to help you with using SSH.Change Directory (cd)
cd /path/to/directory/
cd .. will go up one directory
Make Directory (mkdir)
mkdir /path/to/directory/to/create/
Print Working Directory (pwd)
List the full path to the current directory
Listing Files (ls)
ls /path/to/directory/
ls -a list all files, including those starting with a period
ls -l list more information about the files
ls -al list all files, with additional information
ls -alR list all files and information in subdirectories too
Chmod (chmod)
Chmod 755 filename.html
Move or rename file (mv)
mv /path/to/oldfile.html /path/to/newfile.html
Remove file (rm)
rm filename.html
rm -rf will delete all files and subdirectories
Locate file (locate)
locate examplefile.html
Touch/Create file (touch)
touch /path/to/newfile.html
Edit file with VI editor (vi)
vi /path/to/firectory/filename.html
While using the VI editor, you can use press SHIFT + to use VI specific commands, including:
:q! quits the file without saving and exits vi
:w saves the file
:wq saves the file and exists vi
:LINENUMBER : 25 takes you to that line in the file
:$ : takes you to the last line of the file
:0 : takes you to the first line of the file
Top system processes (top)
Press Shift + M to sort by memory usage, or Shift + P for CPU.
For a comprehensive list of commands, please see http://www.ss64.com/bash/
Other useful commands
View Apache error logcat /var/log/httpd/error_log
Modify Apache httpd.conf configuration file
vi /etc/httpd/conf/httpd.conf
Modify MySQL configuration file
vi /etc/my.cnf
Get number of files in directories and subdirectories
ls */ | wc –l
Find path to your php.ini file
php -i | grep php.ini
Restart Apache
service httpd restart
Restart MySQL
service mysql restart
Dump/backup MySQL database
mysqldump –-user username –-password=password mydatabase > /path/to/dump.sql
Show number of active connections to server (Apache)
netstat -an |grep :80 |wc -l
Show number of open connections to MySQL
mysqladmin processlist |wc -l