Moving Beyond SSH: Familiarizing Yourself with SSH

Published on Jan 8, 2009   //  Development

Moving Beyond SSH

This week we’re still on the topic of SSH. Learning this thoroughly is very important if you’re going to be managing your server exclusively. Today we’ll be going over some basic (but vital) commands that will be mostly required when managing your server. But first, we need to learn some keyboard shortcut commands, which are also necessary to using SSH.

Keyboard Commands

Since there is no mouse control when using SSH, you will have to do everything with your keyboard. Most things can be done by typing commands, but if you’re editing a file via SSH, you have no way of typing in any commands, so this is where keyboard commands come into play. Luckily, there aren’t very many to remember.

These keyboard commands are the exact same in Windows, Mac OS X (yes, use Control, not Command for these) and Linux.

Control-X – Use this to exit out of an open file.

Control-C – Use this stop a running command immediately or to get the current line number in an open file.

Control-O – Use this to save an open file.

Arrow Keys – While not editing a file, use up/down to scroll through a list of previously issued commands. While editing a file, use up/down/left/right to navigate through the file.

Copy and Pasting – In PuTTY on Windows, simply selecting text with your mouse, and releasing your mouse button, will copy it to your clipboard. Right clicking will paste the text from your clipboard. On Mac OS X, use the normal Command-C and Command-V shortcuts. On Linux, it may either by the normal Control-C and Control-V, or it could also be Control-Shift-C and Control-Shift-V.

Commands

I should first note that when you login to your server via SSH, you’re placed into the /home directory.

Let’s go over some useful commands that you enter into your SSH windows. After typing a command, press enter/return to send it.

Changing Directories – You will use this command to navigate through directories on your server. The following example would get you to your /etc folder.

cd /etc

You basically type in the initial command name (in this case cd), followed by a space and then type in your desired location. Directory paths preceded by a forward slash are absolute to the very root of your server, while paths not preceded by a forward slash are relative to your current directory. If you want to go up a directory, you’d do this:

cd ..

Which will move up one directory level. To move up multiple directory levels, append /.. to the end of that command (once for every level you want to move up). To get to the very root of your server, just type in:

cd /

Editing Files – If you need to edit a file through SSH, you will use this command:

nano file.txt

You can use either absolute or relative paths to the file. If the file doesn’t already exist, it will open a blank editor.

Creating a File – If you just want to create a file without opening it, use this command:

touch newfile.txt

Listing the Contents of a Directory – Since your server lacks a graphical interface, telling what files and directories are in a folder can be tricky. However, learn this command and you’ll have no problem.

ls

Which will list the files and directories of your current working folder. If you want to list the contents of a different directory, format your command like this:

ls /path/to/other/directory

By default, hidden files and directories (any file or directory that starts with a period) are not listed with the ls command. You can change that by using the -a option. Like so:

ls -a

Or for a different directory:

ls -a /path/to/other/directory

Create a New Directory – You may often want to create a new directory, which you can do with this command:

mkdir newdirectory

Which will create a directory called “newdirectory” in the current working directory. You may also use absolute paths here to create a directory elsewhere.

Deleting a File or Directory – You can use this command to delete a file:

rm file-to-delete.txt

Which will immediately delete that file. If you would like to delete a directory, you need to add the options -rf:

rm -rf directory-to-delete

Again, you can use this with absolute or relative paths.

Change File Permissions (chmod) – Changing file’s (or folder’s) permissions is easy:

chmod 755 file.txt

Replace 755 with your desired chmod number, and file.txt with the file or folder you want to change.

Downloading a File from the Internet to Your Server – When you’re installing software on your server, you’ll often times have to download something to your server, this makes that easy:

wget http://example.com/example.tar.gz

Which would download that file (well, if it existed) into your current directory.

Unpacking an Archive – When installing software you’ll often times have to unpack archived files. If the file has an extension of .gz, you’ll first have to uncompress the archive:

gzip -d archive.tar.gz

Then you’ll have archive.tar in your directory, which we unpack like this:

tar -xvf archive.tar

Which will unpack the contents of archive.tar to your current working directory.

Next time

For next time I’ll try to make a PDF with all these commands in it (and any that I may have missed or more advanced commands that I didn’t think were vital, but are useful anyways), that you’ll be able to download (and print if you like) and use as a cheat-sheet when you’re plugging away in SSH.

3 Comments to “Moving Beyond SSH: Familiarizing Yourself with SSH”

  • Will you explain how to change the ssh port? and what is SSH 1 vs 2?

  • One of my favorites: top

    for seeing all running processes and their pids and how much cpu and ram and swap is being used.

  • Yes, later in the series we will cover changing the SSH port and other security-related changes. We probably won’t cover SSH 1 vs SSH 2, as this is not of particular concern to most people (all recent server Operating Systems and SSH connection tools all use and support SSH 2). This seems like a good (technical) comparison of SSH 1 and 2:

    http://www.snailbook.com/faq/ssh-1-vs-2.auto.html