Using SSH and Unix commands - a beginners tutorial

Although I've been using SSH for quite some time, I still
consider myself very much a beginner. It's something I don't use often, but some basic SSH and Unix command knowledge
has been a very handy thing to have. The following guide and simple exercises are a non-geeky introduction to the wonderful world of SSH and Unix commands.

What is SSH?

Developed in 1995, SSH (Secure Shell) was developed as a secure alternative to Telnet.

Telnet is a protocol allowing for command line access to a Unix, Linux or FreeBSD based remote computer. If you've ever used DOS, you'd be familiar with the black screen. Some of the DOS command set will also work . If you're asking what DOS is, then you're probably a lot younger than I :). DOS was Microsoft's Disk Operating System; the precursor to Windows and what Windows was based on up until late in the 20th century. Oh my, now I really feel old.

What can you use SSH for?

SSH, depending on the level of access your host allows, lets you perform all sorts of diagnostics and file management functions, including archiving/decompressing files; setting passwords for particular folders, backing up MySQL databases and managing CRON jobs in a secure environment. It's a manually intensive way of going about things, but very powerful and flexible.

Accessing SSH

You'll need to ask your web host if you have access to SSH. Some hosting companies are really, really paranoid about letting their clients have SSH access, claiming security concerns. It's my understand that if this is their concern, then they don't really know *how* to secure their servers while still allowing for SSH access.

SSH software

You'll need a Telnet client which has support for SSH. A lightweight, freeware application that I use is
PuTTY.

Using PuTTY

After firing up the application, ensure the port setting is 22, which is the secure option.

Type in the host name or IP address - the host name will usually be your site name, but check with your web host first to make sure

Click the Open button

You'll probably get a security alert stating that the server key is not cached in the registry. If you want to be ultra secure, you can check with your web host that the dss fingerprint reported by PuTTY is the correct one. Once it's been confirmed by your host, or if you don't wish to go to that hassle, just click 'yes' and the key will be stored in your cache.

A window should appear and after a few seconds, a prompt stating "login as" will display

Type in the username, usually your account username

A password prompt should then appear after a few seconds. Type in your account password

Fun with SSH and Unix commands

You're now looking at the command line for the server operating system. Of course, there's only a specific set of Unix type commands you'll be able to access, well at least I hope there is :). This will vary from host to host. Unix commands can be also be used on other *nix type systems such as Linux and
FreeBSD.

You may notice a little bit of a lag from when you type something to when it appears on the screen. This is normal and depends on a number of factors including activity on the server and your ISP connection

Let's start getting a little familiar with some simple exercises, commands and tasks to get you acquainted which should work in most host's environments.

First off, let's start off with finding out the date and time on the server. Type this command:

date

Now we'll take a look at the calendar for the current month. Type:

cal

Let's try the calculator. Type:

bc

hit enter, then type:

2 + 2

hit enter.

To exit from the calculator; type

quit

See how easy command line stuff is ;). Ok, let's go on...

Basic navigation

Type: ls

This command lists folders and files, in the directory your logged into. To change to another folder, type:

cd foldername

Type ls again to see the contents of that folder, or you can continue drilling down by using cd (folder name) commands

To go back up a level of your account, type:

cd ..

To return to your home directory, i.e. the top level, type:

cd ~

Those couple of commands should take care of things in relation to basic navigation.

Determining space usage

To check how much space each folder is using in your account; type the following command:

cd ~ && du -h

For just a summary of the total space used

cd ~ && du -sh

Viewing the contents of files

To take a peek at the contents of a file, use this command:

more filename

This will display the contents of the file one screen at a time. You advance through the screens by hitting the spacebar. At the end of the file, you'll be returned to the command prompt.

Creating and editing files

For this exercise, we'll first create a blank file and then edit it. return to your home directory (cd ~) and type the following command

touch test.txt

"touch" is the command which creates the file

Now we'll put some content into the file using "pico", a very basic text editor. Type:

pico test.txt

Believe it or not, the screen you see next is very much like the cutting edge word processors from the late 80's :).

Type in a few random lines. You'll see at the bottom of the screen a menu with functions containing keyboard commands for those functions. The "^" characters you see indicates that the Ctrl key on your keyboard needs to be used in conjunction with another key.

To save the file we've just edited, use Ctrl+ X

You'll then be asked to confirm the save, which you do by pressing Y

You'll then see "File Name to Write: test.txt" - just hit enter.

Done!

View your saved file by typing:

more test.txt

Creating a directory/folder

Nice and simple; navigate to the top level folder where you want to make the new subfolder and type this command:

mkdir foldername

Moving/copying files

Using the test.txt we created, to make copy of this file in another folder

cp test.txt foldername

where foldername is the name of the directory you wish to copy the folder into.

If the folder is a subfolder, you'll need to use a command along these lines

cp test.txt foldername/subfoldername

If you don't wish for a copy to be made, but just for it to be moved, use this command instead:

mv test.txt folder

Deleting files and folders

To delete a file, in this case the one we just created, change to the folder that it's in and type:

rm test.txt

Warning: Depending on your host's setup, you may *not* be asked to confirm deletion when using an rm command; therefore always use this command with care.

To remove an entire folder and the sub-folders beneath it:

rm -rf folder/

where folder is the directory name

Finding files and folders

File base structures can get pretty complex and it's easy to
lose track of where things are. This simple command will help you find what
you are looking for

locate fileorfolder.name

You don't need to know the tail extension for the file, but
the more information you can provide, the more accurate the search will be.

Searching for strings of text

Finding a file can be difficult enough, but what if you
can't remember anything about the file name? All is not lost if you can
remember a unique phrase or text string from the file.

If you know the directory it's in, change into that folder and type

grep 'text string or phrase' *

This will only search files within the directory you are currently in. If you don't have any idea where the file may be located; go as far up the directory tree as you feel you need to and try:

grep -r 'text string or phrase' *

This will search recursively through a directory and all levels of subdirectories within it

Command line IM - write

Long before IM communications became popular and the
slick/polished applications we see today, there were ways to communicate
with others via the command line using the "write" command. This
requires both parties to be logged into the same server.

Simply type:

write username

.... where username is the user name of the person you wish
to communicate with. Then just type away as you would in IM :). As limited
as the write command is, if run via an SSH session, it's a secure means of
communicating.

Connecting to a MySQL database

Just a simple exercise for this one. At the command prompt,
type

mysql -u username -p -- dbname

where username is the username associated with your MySQL
database and dbname is the database name.

You'll then be prompted for your password. This prompt
should then appear, indicating you are now connected to the database:

mysql>

To view the tables within your database, type:

show tables;

To get back to the normal command prompt, type:

exit

Archiving files and MySQL databases

If you've ever tried ftp'ing a few hundred megabytes of site files or a large
database to your machine, you'll know just how time consuming this can be. Archiving files and folders is a relatively simple task via the command line. Learn more about doing so in my article on
backing up
data.

Getting help

You can get help on any particular command during your ssh session using
the following syntax:

man command

.. where command is the name of the function you want to discover more about; e.g.

man ls

To scroll down through the help file, hit your enter button

To quit from the help system, type q

Finishing your SSH session

To exit your SSH session either just close the window or type:

exit

Handy tip

Typing out the same commands, especially complex ones, can get a little time consuming. You can use the up arrow on your keyboard to scroll through previously issued commands. The "history" command will also display a list of your previous commands.

To repeat the last command you issued, type:

!!

There's hundreds of other more powerful commands and functions that you can use in the SSH environment and also faster ways of achieving some of the above, but this tutorial was just to whet your appetite and build up some confidence :).
I hope you found it useful.

In the interests of transparency and disclosure, please
note that the owner of Taming the Beast.net often receives goods and services
mentioned in reviews for free, or may receive payments or affiliate commissions for
advertising or referring others to merchants of products and services
reviewed.

Copyright information.... This article is not available for reproduction
without explicit written permission from Michael Bloch and Taming the
Beast.net

Online
meeting & webinar software reviewPowerful, easy to use collaboration tools that can help improve your marketing
sales and training efforts. Learn more about these services in this review
& try a free
trial!

Credit
card transaction fraud screening! Effective fraud screening is an
essential part of running an online businesses. Fraud transactions cost you
money and can threaten your merchant account. Pick up a stack of transaction
screening tips in this free guide!

Guest posts & spider
junk food
If you receive unsolicited offers of guest posts for your site or blog, be a little wary of them.
::
Crowdturfing
Becoming Rampant
Crowdturfing is a threat to any SEO practitioner or site owner that tries to play by the rules
in regard to ranking well in search engines.
::Page speed optimization
Optimizing page load times can help with your rankings and sales - pick up some site speed optimization tips.
::Proofreading tips
A few handy tips on how to proofread your own articles when time is limited
::The traffic value of a
no.1 search rank
How much traffic will a no.1 search engine ranking give you compared to other top ten ranks? Find out in this article
::
Looking for more internet marketing or ecommerce web site development
guides?Articles Archives:
one - two - three