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.
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.
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.
You'll need a Telnet client which has support for SSH. A lightweight, freeware application that I use is PuTTY.
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.
Now we'll take a look at the calendar for the current month. Type:
Let's try the calculator. Type:
hit enter, then type:
See how easy command line stuff is ;). Ok, let's go on...
This command lists folders and files, in the directory your logged into. To change to another folder, type:
Type ls again to see the contents of that folder, or you can continue drilling down by using cd (folder name) commands
To return to your home directory, i.e. the top level, type:
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:
For just a summary of the total space used
Viewing the contents of files
To take a peek at the contents of a file, use this command:
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" is the command which creates the file
Now we'll put some content into the file using "pico", a very basic text editor. Type:
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
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:
Using the test.txt we created, to make copy of this file in another folder
where foldername is the name of the directory you wish to copy the folder into.
If you don't wish for a copy to be made, but just for it to be moved, use this command instead:
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:
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:
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
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
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.
.... 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
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:
To view the tables within your database, type:
To get back to the normal command prompt, type:
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.
You can get help on any particular command during your ssh session using the following syntax:
.. where command is the name of the function you want to discover more about; e.g.
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:
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.
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.
For help with using MySQL via the command line; see MySQL.com
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
paid cash taking online surveys - free to join online
In Loving Memory - Mignon Ann Bloch
copyright (c) 1999-2011 Taming the Beast Adelaide - South Australia