« GeekStorage Profile: Jeff Criswell, Account ManagerWindows - Coming Soon to a VPS Near You »

Up and Running With SSH

August 1, 2012 at 1:29 PM

Connecting to SSH via Terminal or Command Prompt

First, of course, fire up Terminal or Command Prompt, and you'll see a screen that looks a lot like this:

Terminal Prompt

To login remotely from this screen, type the following:

ssh [email protected]

Be sure to replace username with your cPanel username, and my.host.name.com with your domain, or your hosted IP which can be found in cPanel on the left hand stats bar. After the connection is established, you will be asked for a password. Enter your cPanel password to finish logging in to SSH.

I have a command prompt for SSH but now what??

Now you're cooking with fire! Let's get some easy commands under your belt! First, it is important to know that you are working within a structured filesystem, so what you will first want to do is figure out where you are. When you first login, you are always placed in your home directory, this is typically '/home/username/', but to find out the exactly path, enter the following command at the SSH prompt:

pwd

This command means "tell me my present working directory", you should see output like so:

/home/friend

Now that you know where you are, you can start moving around effectively and making some real changes! Let's get a listing of the directory, this is essentially the same as listing your files in FTP. Enter this command:

ls

You should see a "list" of files that perhaps look foreign, such as ".bash_history" or "etc", "tmp", etc. What we are primarily interested in is getting to our website files in the "public_html" folder! Let's go in to that folder with the following command:

cd public_html

The "cd" command basically says "change my current working directory" to the one specified, in this case, your public_html directory, where all of your website data is stored.

There are a wide variety of ways in which you can modify, your files. For example the command below would allow you to edit your index.html file in a simple text editor:

nano index.html

If you're familiar with Vim or GVim from your current operating system, you may prefer to use vim:

vim index.html

In nano, once you are done changing your file, hit Ctrl+X to close the editor. You will be prompted if you want to save any changes, just hit Y to save changes, or N to discard any changes.

You will want to become familiar with command flags when working with SSH. Remember the 'ls' command we used earlier to get a directory listing? If you want to see more information about the files, use the "l" flag, like so:

ls -l

Quite a different display! With the 'ls -l' command you can see the CHMOD settings you may be familiar with from FTP, and the user / group assignments of the files (where you may see your username in the file listing). Want a list of all available flags for a particular command? Just append "--help", this will work with almost all commands. Run the following:

ls --help

Whoa! What just happened? Well, it looks like we have more information that we can fit on one screen being spit back at us. Fear not! If you run in to situations where there is too much information to read on one screen, "pipe" it to the "more" command. This may sound complicated, but what you are doing is sending the output of the command in to the "more" command which allows you to scroll through the output as you like. This is accomplished like so:

ls --help | more

Hey hey wait, what's this "|" thing??? Well, it's called the "pipe" and what it does, it allows you to send the output of one command to the input of another command. Simply put, it passes on it's details to the command after the pipe. Now, you might be wondering how you scroll using the output you have right now, since the arrow keys do not work, what gives? There are some simple controls for the "more" command output:

  • Enter - Scroll down one line
  • Ctrl+D - Scroll down one screen
  • q - Cancel & close "more" output

Now, so far we haven't really done anything important. Let's do something real! Let's say we just launched a new website at http://ourdomain.com/friendsforever/ but we also want http://ourdomain.com/friends4ever/ and http://ourdomain.com/friends/ to work for access to the site. Well, there are two ways to do this: .htaccess in each subfolder, or symlinks. What are symlinks you say? They're magic! Let's get our example site ready:

cd ~/public_html/
mkdir friendsforever
cd friendsforever
nano index.html

Whoa! What's this magical "~" character? This is a great shortcut, it basically means "my home directory", so instead of typing /home/username/public_html/ we can just type ~/public_html/ to get to our public_html directory, no matter where we are in the filesystem. Next, we create the friendsforever directory with the "mkdir" command, and then we begin editing our index.html file inside this folder. Put whatever you like in this file, such as "Test Site" or "We can be friends forever if this works!" After you're done editing the index.html file, hit Ctrl+X and then Y & Enter to save your changes.

Pages: 1· · 3·