How-To

SSH Basics, Part 2: Basic SSH Commands

Accessing SSH for the first time can be fairly daunting, but it doesn’t have to be. Command line access via SSH is the single most powerful tool you have for administering your server. Not only can you do everything on the command line of your linux server if you wish, there are certain tasks you can only accomplish through SSH command line access.

After learning how to check for permissions to access SSH, configure a client and log in to your server via SSH in part 1 of our SSH Basics series, you’re ready to execute your first commands. Let’s dive right in.

A user’s guide to SSH

The server I will be using for this tutorial is testingservint.net with the username userone. Log into your server to follow along, substituting your username for the one I am using. If you don’t remember how to log in, check the original article and come right back.

At ServInt, most customers use the CentOS operating system, one of many flavors of Linux. If you are using another Linux distribution, your commands might be slightly different, but many will be similar.

The Linux OS command line interface is based on typing commands and receiving output back from the server. Every command you type will be completed by hitting the [enter] key.

whoami and pwd

Let’s start off with two easy commands:

whoami — Who am I?

pwd — Where am I?

whoami_pwd

The output of these two commands shows us who we are logged in as and which directory we are currently in.

ls

Now, let’s display the contents of this current directory:

ls — What files and folders are here?

ls

The output above shows the default files and folders for a new cPanel user. You can see the public_html which is the web root for your sites (where the domain grabs its files from), and the mail folder, mail, which contains all of your emails.

If you want to get a bit more information from SSH about these files and folders, try this: ls -lht

You should see a list output (-l) with permissions and ownership, human readable sizes (using -h: not accurate for folders), and date last modified (sorted using -t) along with the file names.

cd

Next step is to change our directory:

cd public_html — Change directory to public_html, web root.

cd_publichtml

You are now in the public_html folder. Check your current directory with pwd to be sure. Now, check the contents of the directory with ls.

ls_public_html1

nano

In my example, you can see I have an index.htm file in my public_html directory. This is the file the web browser requests when someone visits your domain. Whatever contents are in this file will be rendered in the browser. If I go to testingservint.net in my browser, I will get a blank white page. This is because there is nothing (no text or code) in the index.htm file. Let’s change that!

We’ll use nano in this example. Nano is one of many file editors available in Linux. Nano is the most user friendly Linux editor, in my opinion. It can be a little complicated at first, but you’ll get the hang of it.

Note: If you don’t have an index.htm file, it will be created when you use nano (see below) to edit the file. Otherwise, you can run touch index.htm

nano index.htm — Edit index.htm using nano.

Here is what you should see:

nano_indexhtm1

I’m now going to type some content. You can press return/enter to move to the next line as you would in a normal editor. But navigating horizontally requires using the arrow keys. You can then save it with [ctrl] x.

Here is my finished product:

nano_indexhtm_save_buffer

As the image above shows, I am being prompted to “save modified buffer”. Type y and hit the [enter] key. You will then be asked to save the file as the same name, or as a different name. We want to overwrite the already existing file, so we want to use index.htm. Just hit the [enter] key once more.

nano_indexhtm_hello_save

Awesome! Now you can load the url for your site in the browser and see our changes:

safari_hello

mv

Now, what if I want to add some php code to the file? Well, since the html file (.htm or .html) won’t support it, we need to switch the file to .php using the mv command. Move is used to move a file to another directory or rename the file. Make sure you’re still in the public_html directory using pwd, then use the move command:

mv index.htm index.php — Move (and rename) index.htm to index.php.

mv_indexhtm2php

Easy! Now php code added to index.php will be supported by the browser/server. That is, as long as PHP is actually installed on the server (it is by default on ServInt cPanel servers).

The last few things I want to show you are statistics for the server.

php

Since we’re just coming from switching your index.htm file to index.php to support both html and php code, let’s find the php version the server is running:

php -v — Ask the server for the php version.

php_v

You can see my PHP version is PHP 5.4.20. There is additional information too, like the fact I have eAccelerator installed.

free

Next, let’s find the memory usage:

free -m — Ask the server for the available and used memory.

free_m

To understand what all of the output means, check out our article on Understanding and Troubleshooting RAM in Linux.

df

Next, let’s find out our disk space usage and inode usage.

df -h and df -i — Ask the server for the disk usage and inode usage.

df_h_df-_i

The values are fairly simple to understand. I would only worry about use% right now.

httpd

Lastly, let’s see what installed apache modules are available to us. For example, if your site uses gzip compression, you’ll look for mod_deflate or mod_gzip (older versions of apache use mod_gzip). If you see either, you can use gzip for your site.

httpd -l (That’s a lower case L as in “lemon.”) — List installed apache modules.

Note: You may or may not get some errors about “ulimit: open files: cannot modify limit”. They can be ignored.

man

To understand more about each of the above commands — or any other commands you’ve heard about — read the man pages.

Every Linux command has documentation available right there on the command line that explains how the command works in a man (short for “manual”) page. Simply type man and then the command (for example: man free) to access the man page for any command.

With these first few commands, you should be starting to understand the “call and response” of the command line. There are plenty of resources online — not to mention other ServInt University tech articles — to give you plenty of practice working on the command line. Once you understand the basic pattern of typing commands and reading output, you can add to your list of commands with ease. In no time, you’ll become a master at Linux. Until then, remember that if you have any questions, you can reach out to the ServInt MST in your customer Portal.

Photo credit:  pandoraice

Find out more about ServInt solutions

Starting at $25

Comments
  1. Want to get serious about server admin? Learn how to work in #Linux on the command line. http://t.co/CXl015UlBl
  2. Good question Rob. Personally, I like to specify the absolute path when using mv and cp commands. Even rm (to delete files), which is partially why I started doing it. I want to avoid any possible confusion and damage to my data. So, for example, let’s say your wp_update_files folder was placed in the public_html folder for userone – /home/userone/public_html/wp_update_files/ Then, we’ve got our original wordpress install within a folder named wp – /home/userone/public_html/wp/ I would first make sure to remove the wp-content folder from wp_update_files to prevent overwriting media and themes I’ve worked so hard to add to wordpress – rm -rf /home/userone/public_html/wp_update_files/wp-content (forget the flash in this case as we want to delete the actual folder, not contents within). Then, we can cp the files over making sure to overwrite all currently existing files – cp -rf /home/userone/public_html/wp_update_files/* /home/userone/public_html/wp/ You can see I used a * to specify all of the contents of the folder, then move it to wp/ (the slash just helps keep things logical in your mind, it’s not necessary). In some cases, you will see cp -rf not actually force the overwrite (avoid the yes or no prompt), and instead prompt you to confirm overwriting each and every file. There are ways around this, like running y | cp -rf to throw “y” to every single question by spamming the console. If you feel comfortable with that, go for it. As long as you’re using full/absolute paths you should be fine. We take daily backups of your server, so you can always have us restore directories if necessary.
  3. Learn how to work on the command line or your server with our #TechBench series on #SSH basics. http://t.co/CXl015UlBl
  4. Great post Nathan! One thing that continues to drive me crazy is the usage of trailing / and * when moving or copying files and folders. For example, very often when upgrading WordPress or other scripts, you're required to unpack / unzip the update and then copy / overwrite all the files from that folder to your install folder. So, let's say you have: Update folder: /wp_update_files/ WP install folder: /wp/ What command do you use to mv or cp all the files and folders from the former to the latter? It seems this gets even more complicated if the target folder does or doesn't exist. In general, when do you use a trailing slash, astrix, both, or neither in either the source or target directories / folders?
  • Hosting Advice
  • The New York Times
  • The Hill
  • Bloomberg
  • The Seattle Times
  • Computer World
  • Ars Technica
  • MSNBC