SSH (Secure Shell) Access is now available for all shared hosting, reseller hosting, VPS, and Dedicated server accounts. Those that use SSH frequently find that managing their website can be faster than trudging through a GUI (Graphical User Interface).
GUIs are often aesthetically pleasing and organized into many different sections or pages. This is good because each section or page is designed for a specific task, but it can ultimately cause slower navigation for the user. It also causes more resource usage for the server and browser. Using a clean, text based SSH console can streamline things quite a bit.
Once you have connected to your account via SSH for the first time, you are ready to learn how to more quickly manage your site. This article describes basic commands that can help you through tasks in the command line environment.
Home sweet home – your starting point
When you first log into your account, you will be placed into your home directory. This is the topmost level your cPanel user can access on the server. All the files for your emails and all domains on your website will be under this directory. In our servers, your home directory takes the folder structure of /home/userna5. The ‘userna5‘ portion is representative of your cPanel username, so if your cPanel username is freddy5, then your home directory will be /home/freddy5.
The cd command – checking out the neighborhood
Now that you know where you begin, you can move around your account. To do this, you will use a command known as the cd command. This stands for ‘change directory‘. The base command is below, but by itself it doesn’t do anything. You need to give it some sort of direction.
Returning home
No matter where you wander off to, you can always return to your home directory by using the following command:
Moving to specific folders
Now that you know you can’t get lost, you can use the cd command to move to a specific directory. For instance, your public_html folder is the root folder for your website and is located inside your home directory. To get there use:
You can also move down more than one folder at a time, you simply need to add them together with a little ‘/’ in between them. If you have a folder named ‘blog‘ in your public_html folder, you can then move into that folder by typing:
Taking a step back (or two, or…)
You can also simply move up a level by using the .. option. For instance, to go up one level from where you are, use the following command:
To move up two levels, you would specify with a double .. command as shown below:
Where the heck am I? – the pwd command
If you get lost, you can always check to see where you are by using the pwd command, which stands for ‘print working directory’. This will tell you in which directory you are located. It is like checking your GPS in the account.
The result may be something like:
This would mean you are in your public_html directory.
Show me the files – using the ls command
Once you know how to move around, you will want to know how to tell what files are in the directory. That is where the ls command comes in. Below is the most common format for the ls command. It lists all the contents of the directory in long form with human readable file sizes.
The result will be a list like this:
total 32 drwxr-xr-x 3 root root 4.0k Jun 3 10:19 ./ drwxr-x--- 25 root root 4.0k Jun 3 10:16 ../ -rw-r--r-- 1 root root 12.5k May 5 13:29 error_log drwxr-xr-x 2 root root 4.0k Jun 3 10:19 images/ -rw-r--r-- 1 root root 52 May 5 13:29 index.php -rw-r--r-- 1 root root 0 Jun 3 10:15 testfile.txt
Moving on up! – using the mv command
Being able to move files from one folder to the next is a useful skill. This is accomplished with the mv command, which stands for ‘move’. To move a file from point a to be you just have to know where your destination is relative to where you are. For example, if you are in the public_html directory and want to move the test.txt file to the next directory down, named blog, then you simply perform the command as follows:
This begins by telling the computer to move the test.txt file, and follows with the destination, which is the blog directory.
You can even use the mv command to rename a file. You simply provide the new name as the destination with no other folder specified. This means it will ‘move’ the file to the same folder while changing the name.
Multiplicity – copying files with the cp command
Moving files is great, but if you want to keep the original in one place as and place a copy elsewhere, then the cp command is the way to go. This leaves the original file in place, while making a duplicate at the destination you provide.
You can also copy the file to a new place while renaming it.
Lastly, you can copy the file to the current folder but you will need to rename it. This is a common way to make a backup copy of a file.
Mother may I? – Changing permissions with chmod
The chmod command is powerful as it allows you to change who can access the file and in what ways. ‘chmod‘ is short for ‘change mode’. The mode is the set of permissions the server checks when a file has an access attempt. Permissions are often set by assigning a three digit string to a file, such as 755 or 644.
Permission categories
There are three numbers because there are three categories of permissions. The first in the list is the ‘Owner’ of the file. This is most often your cPanel user. Next is the ‘Group’, which means root group. Last is ‘Other’ which is everyone else on the system, including site visitors. The ‘Group’ and Other’ categories often have the most strict permissions for security purposes.
Permission types
The permissions themselves come in three flavors: Read, Write, and Execute. They are very self explanatory as Read allows a user to read the file, Write allows changes to be made, and Execute allows the user to execute the file on the system. Execute is the most common and safest permission as no changes are made to the file. Below is a chart that shows which combination of permission corresponds with which number. There are 8 possible combinations (0-7).
Octal value | Read | Write | Execute |
---|---|---|---|
7 | r | w | x |
6 | r | w | – |
5 | r | – | x |
4 | r | – | – |
3 | – | w | x |
2 | – | w | – |
1 | – | – | x |
0 | – | – | – |
Changing a file’s permissions
Once you have figured out which permissions your file needs, you can change it using the chmod command. Here we show you a couple of examples of more common permission settings. In the first one, we will be setting an index.php file to 644, which is read and write permissions for the Owner and read only permissions for the Group and Other categories. This is a very common setting for php files.
Another very often used mode is 755. This gives full permissions to the Owner category and read and execute permissions to the Group and Other categories. This is very common for directories and html files.
Can I keep it? – Changing file ownership using chown
Files and directories have owners. The ownership will help decide if you are able to see them or not. They can also determine what permissions are used when the file is asked to perform. The owner and root are also the only ones who can make changes to the permissions using chmod. In the SSH command line, you have the ability to change the ownership. There are two categories of owners for a file or directory. The first is the Owner and the second is the Group to which the file belongs. You can change either one or both of them by using the chown command. Below we show some basic examples.
In this example the chown command changes both the owner and the group to ‘test’.
chown test:test index.php
This example shows how to change just the owner to ‘test’.
chown test index.php
Our final example shows how to change just the group to ‘test’.
chown :test index.php
Viewing the contents of a file using cat
There are many times when you may need to simply view the contents of a file. This is often done when you want to quickly check the settings in a file. To view the contents of a file on the console screen you use the cat command, which stands for ‘concatenate’. Here we show a couple of examples on using the cat command.
cat test.txt
This will display the content of the ‘test.txt’ on your console screen. The ‘test.txt’ file may display as below:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In convallis leo nec lectus accumsan, id vehicula dui tincidunt. Proin eget pellentesque lectus. Duis id mauris vel massa imperdiet egestas sit amet eget ante. Nulla at fermentum turpis. Morbi eu dictum felis. Morbi vel ipsum nunc. Nunc ac sapien viverra, dignissim arcu vitae, gravida magna. Phasellus a adipiscing sapien. Cras vel aliquet massa. Mauris quis auctor eros. Ut gravida tellus at nisi facilisis, adipiscing cursus nisi luctus.
This example shows how you can use the cat command and display the line numbers.
cat -n test.txt
The resulting display will be almost the same as the base command, but with the addition of the line numbers. This makes it easier to locate content that you are looking for.
1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. 2 3 In convallis leo nec lectus accumsan, id vehicula dui tincidunt. Proin eget pellentesque lectus. Duis id mauris vel massa 4 imperdiet egestas sit amet eget ante. Nulla at fermentum turpis. 5 6 Morbi eu dictum felis. Morbi vel ipsum nunc. Nunc ac sapien viverra, dignissim arcu vitae, gravida magna. Phasellus a 7 adipiscing sapien. Cras vel aliquet massa. Mauris quis auctor eros. Ut gravida tellus at nisi facilisis, adipiscing cursus nisi luctus.
Give me more! – Viewing files with the more command
Much like the cat command, the more command allows you to see the contents of a file. The more command is different in that it displays a portion of the file at a time, allowing you to view it and then view additional portions as you wish, up to the end of the file.
The base command
Here we show the base command. It will show the first few lines on the screen and let you know how much of the document has been displayed.
more test.txt
You will be presented with some of the content on the screen.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi convallis vehicula nisi. Cras consequat consectetur lectus in cursus. Ut vulputate interdum arcu, imperdiet sodales nisl adipiscing vitae. Quisque blandit urna eu neque consectetur, sit amet scelerisque est dapibus. Donec ut euismod arcu. Vivamus sed urna ut eros blandit scelerisque et laoreet magna. Sed vestibulum mauris quis dolor venenatis sagittis. Cras hendrerit cursus turpis vitae volutpat. Donec adipiscing sagittis enim nec lacinia. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Quisque aliquam purus id facilisis lacinia. --More--(11%)
Display directions
This example is the same as the base command, but displays directions on what you can do next.
more -d test.txt
You will be presented with the specified numbers of lines of content on the screen.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi convallis vehicula nisi. Cras consequat consectetur lectus in cursus. Ut vulputate interdum arcu, imperdiet sodales nisl adipiscing vitae. Quisque blandit urna eu neque consectetur, sit amet scelerisque est dapibus. Donec ut euismod arcu. Vivamus sed urna ut eros blandit scelerisque et laoreet magna. Sed vestibulum mauris quis dolor venenatis sagittis. Cras hendrerit cursus turpis vitae volutpat. Donec adipiscing sagittis enim nec lacinia. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Quisque aliquam purus id facilisis lacinia. --More--(11%)
Display a specific amount of content
This last basic example demonstrates how you can use the command to display only a specific number of lines. In this case, we show 5 lines.
more -5 test.txt
You will be presented with some of the content on the screen.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi convallis vehicula nisi. Cras consequat consectetur lectus in cursus. Ut vulputate interdum arcu, imperdiet sodales nisl adipiscing vitae. Quisque blandit urna eu neque consectetur, sit amet scelerisque est dapibus. Donec ut euismod arcu. --More--(5%)
Less is more – Using the less command
While the more command allows you to navigate through the file from top down, the less command allows you to go through the file in both directions. It also allows you to do things like jump to specific line numbers or percentages of the file. You can even search for specific words in the document. Below we show you a couple of features of the less command.
Using the less base command
Here we just use the base command. It will display as much of the file as can fit on the screen.
less test.txt
Navigating the file
The less command acts more like an editor program than just a list of commands. Once the file begins to display on the screen you can navigate up and down through it using the arrow keys as well as the Page-Up and Page-Down keys. The Page-Up and Page-Down keys move a full page at a time.
Jumping to specific lines
Moving to a specific line number is easy. You simply type in the line number followed by ‘g’. For example, if you want to go to line to, you would type in ’10g’.
Jumping to specific percentage
Much like jumping to a specific line, you can jump to a specific percentage of the file. Type in the percentage (0-100) and then ‘p’. If you want to go 50 percent of the way through the file, type in ’50p’.
Search for certain keywords
You can also search for certain words within the file. To do that, type in the word you are looking for followed by a ‘/’. If I wanted to look for the word ‘ipsum’ in the file, I would type ‘ipsum/’ and all instances of the word would be highlighted.
Nano Nano! – Out of this world editing with the nano tool
We know how to view files with the more and less commands, but they do not allow editing. If you need to edit a file, nano is a great basic editor. While not as complex as tne vi tool, it is perfect for search and replaces or quick editing. The vi tool is more suited for development. Here we go over a few basics of the nano tool.
Opening the editor
Before you can begin editing, you need to open the editor. This is very simple and falls within the base command format of most commands and tools in Linux command line.
nano test.txt
The nano interface
Once you open the interface you will see the cursor in the upper left corner at the beginning of your file. At the bottom of the screen is a set of basic commands. You will notice the commands are tied to specific key combinations that begin with ‘^’. The ‘^’ is the symbol for pressing the Ctrl key on the keyboard, so to activate the Get Help command (^G) you would hold the Ctrl key down while pressing the ‘G’ key.
Navigating the file
Navigating the file is an easy process, simply use the arrow keys to move up, down, left and right as you would any normal word processing program.
Saving along the way
As you make the changes in your file, you do not have to save only when closing the file. You can save as you go along by performing the ‘WriteOut’ command. Press ^O and then hit Enter. This saves all the changes you have made so far.
Exiting the editor
Once you have finished making your changes, you can exit the editor by pressing ^X (Ctrl+X). You will be prompted to save any changes by entering “Y” (for Yes) or “N” (for No) and then pressing Enter.
Conclusion
The above commands are a very basic introduction to the world of command line using SSH. However, after getting used to them and the plethora of options they can provide, you will find yourself able to navigate and manipulate your account much faster than using a resource intensive graphical interface. Check back with us as we continue to expand our documentation with other useful commands.