17 Essential SSH Commands to Know + Free Cheat Sheet
Secure Shell (SSH) is a powerful tool used to access and manage remote servers securely. Whether you’re a beginner or an experienced user, mastering these 17 basic SSH commands will enhance your efficiency and proficiency in managing your servers effectively. Let’s dive in and explore the essential commands that every SSH user should be familiar with.
Download ultimate SSH commands cheat sheet
How to Access Remote Server
Before we begin, make sure that you have access to a remote server. If you own a Hostinger VPS plan, the login details are located in the Servers tab of hPanel. However, if you use our shared web hosting, you need to go to Hosting -> Advanced -> SSH Access.
Pro Tip
SSH stands for Secure Shell, a protocol used to securely connect to a remote server or system. If you want to learn more about it, we have a detailed tutorial on how SSH works and how to set up SSH keys.
Now let’s start accessing your remote server:
- There are two recommended methods to establish an SSH connection:
- Using an SSH client (PuTTY). It will require you to enter the server’s IP and the port number into the corresponding fields.
- Using the built-in command prompt (Windows) or terminal shell (Linux, macOS). You will need to write:
ssh user@serverip
Remember to replace “user” with your real username and “serverip” with your server’s dedicated or shared IP address.
- Using an SSH client (PuTTY). It will require you to enter the server’s IP and the port number into the corresponding fields.
- Once you click the Open button on PuTTY or enter the command on the terminal, you will be prompted for a password. If you are connecting to the server for the first time, you might get a warning message, telling you that the server is not recognized. Just hit Yes to continue.
That’s it. Now you’re connected to the server and can start executing SSH commands.
List of Basic SSH Commands
In this part, we will go through popular SSH commands, complete with their syntaxes and useful options.
Here’s a quick look of the basic SSH commands that we’ll cover in this article:
SSH Command | Explanation |
---|---|
ls | Show directory contents (list the names of files). |
cd | Change Directory. |
mkdir | Create a new folder (directory). |
touch | Create a new file. |
rm | Remove a file. |
cat | Show contents of a file. |
pwd | Show current directory (full path to where you are right now). |
cp | Copy file/folder. |
mv | Move file/folder. |
grep | Search for a specific phrase in file/lines. |
find | Search files and directories. |
vi/nano | Text editors. |
history | Show last 50 used commands. |
clear | Clear the terminal screen. |
tar | Create & Unpack compressed archives. |
wget | Download files from the internet. |
du | Get file size. |
1. ls Command
This SSH command is used to list all files and directories. After entering ls, you will see an output that looks like this:
There are also a few useful options that you can combine with it:
- -l — displays the details of the files, such as size, modified date and time, the owner, and the permissions.
- -a — shows hidden files and directories.
2. cd Command
cd (Change Directory) is the command that we use to jump between directories. It’s a pretty simple command — just type cd followed by the name of the directory:
cd [directory]
As such, if you want to enter the home directory of your server, you can type:
cd /home
You may also write the full path of a certain directory if it is a few levels deep. For instance:
cd /home/TestDirectory/AnotherDirectory
You are now in the AnotherDirectory.
To go back one level, you can simply enter “..” (two dots) after cd command. What’s cool, you can go back further by adding another two-dots and separating them with a forward slash (/):
cd ../..
By entering this line, you are in the home directory again.
3. mkdir Command
You can use mkdir (Make Directory) command to create a directory. This is the syntax:
mkdir [folder name]
Let’s assume you want to create a new folder named “myfolder”. You will need to type:
mkdir myfolder
4. touch Command
The touch command in SSH is used to create a new file. Here is the syntax:
touch [file name]
If you want to create a .txt file named “myfile”, this is what you need to write:
touch myfile.txt.
The file extension could be anything you want. You can even create a file with no extension at all.
5. rm Command
rm command removes a chosen file or directory. To delete a file, enter:
rm [file name]
For instance, if you want to remove myfile.txt, simply execute:
rm myfile.txt
To delete a folder, you need to use the -r option to remove all the files and subfolders inside it:
rm -r home/hostinger/myfolder
6. cat Command
We use cat command to display the content of a file. Below is the syntax:
cat [file name]
It also allows you to create a new file by merging multiple files. For example:
cat info.txt info2.txt > mergedinfo.text
By executing this line, the content of info.txt and info2.txt will be saved into mergedinfo.txt.
7. pwd Command
pwd is a simple command that outputs the full path of your working directory. Once entered, you should see a result like this:
home/user/public_html
pwd command can come in really handy when you are accessing your site hosting account through SSH. Oftentimes, shared servers don’t tell you the directory you are in.
8. cp Command
This SSH command will copy files and folders. The syntax is:
cp [options] [source] [destination]
[source] is the file or folder you want to copy and [destination] is the duplicate.
Let’s say you have myfile.txt in your working directory, and you want to make a copy of it. The syntax would be:
cp myfile.txt myfile2.txt
If you want to make a copy in a different folder, run the following command:
cp /home/hostinger/myfile.txt /home/etc/
Be careful when writing the name of the destination. If you provide two file names, the cp command will copy the content of the source file into the destination file. Thus, the destination file will be overwritten without any warning. However, if the destination file doesn’t exist, then the command will create a new file.
[options] is not mandatory. However, there are several options that you can use:
- -f — if you don’t have writing permission to the destination file, it’ll be deleted and the command will create a new file
- -u — copy the source file if it is newer than the destination file.
- -n — will not overwrite an existing file.
- -a — archive the files.
Unlike duplicating files, copying folders requires you to use the -R (recursive) option. The option allows all folders and files inside it to be copied.
cp -R /home/hostinger/myfolder /home/etc/
9. mv Command
This command works similarly to cp. However, mv command will move the file or folder instead of copying it. This is the syntax:
mv [source] [destination]
Let’s say we want to move myfile.txt from /home/hostinger/ftp to /home/hostinger/myfolder/. The command should be:
mv /home/hostinger/ftp/myfile.txt /home/hostinger/myfolder
Unlike cp command, you don’t need the -R option to move a folder. For instance:
mv /home/hostinger/ftp/ /home/hostinger/myfolder/
This will automatically move all files and subfolders inside ftp to myfolder.
10. grep Command
grep command looks for a given string in files. For example:
grep 'line' info.txt
The above command would search for ‘line’ in a file named “info.txt”. What’s great, the command will print the entire line that contains the matched text.
Keep in mind that this command is case sensitive. If you want to ignore letter cases, use -i option.
11. find Command
We enter find SSH command to search for a file or files that meet the given criteria (name, size, file type, etc). The following is the basic syntax:
find [starting directory] [options] [search term]
[starting directory] is where you would like to start your search process. There are three main choices:
- / (slash) — search the whole system
- . (dot) — search the working directory
- ~ (tilde) — search the home directory
[options] is an additional argument that you can use to refine your search. Some of the most popular options are:
- -name — look for files based on their names
- -user — search for files that belong to a given user
- -size — look for files based on their sizes
[search term] is the keyword or number that you use to search for files.
Take a look at this example:
find . -name “index”
This command will return any files that have the word “index” on their names. And since we use “.” (dot), the command will only search the working directory.
We also have a great tutorial that provides an in-depth explanation about this SSH command.
12. vi/nano Command
Vi and Nano are two popular text editors that you can use in the command line. To open a file using Vi or Nano, you just need to enter:
vi [file name]
or
nano [file name]
If the specified file doesn’t exist, both text editors will automatically create it for you.
Unfortunately, some Linux distributions don’t offer Nano by default. Don’t worry, you can read our guide on how to install and use Nano.
13. history Command
This one is used to display the last used commands. You need to enter a number to limit the displayed results. For example:
history 20
As you probably guess, the example will show the 20 most recently entered commands.
14. clear Command
The function of clear command is simple — it clears all text from the terminal screen.
15. tar Command
tar is an SSH command that creates or extracts .tar.gz files. It is very popular because most third-party software binaries are in the .tar.gz format.
To archive a folder in .tar.gz format, use the following command:
tar -cvzf ArchiveName.tar.gz /path/to/directory
To unpack a .tar.gz file, enter this command:
tar -xvzf FileName.tar.gz
Notice that both commands use different four-character options — cvzf and xvzf. Each letter represents a specific instruction:
- x tells tar to extract files
- c tells tar to create an archive
- v stands for verbose. The option tells tar to display all file names that are processed by the command.
- z instructs tar to uncompress the archive
- f tells tar that you are supplying the name of the archive
Suggested Reading
How to Use the tar Command: Useful Examples for Archiving and Compressing Files in Linux
16. wget Command
wget is used to download files from the internet. For example, to fetch a file from a website and store it in our current directory, we’ll use:
wget http://fileurl/filename.ext
If you want to download multiple files, put all URLs into a file and use the -i option.
Let’s say the file containing the links is called downloads.txt. The command will look like this:
wget -i downloads.txt
17. du Command
You can use du (Disk Usage) command to view the size of files and folders in a specified directory:
du [directory path]
Unfortunately, the summary will show disk block numbers instead of bytes, kilobytes, and megabytes. Therefore, to show it in a human-readable format, you need to insert the -h option after du command:
du -h /home
The results will be more understandable:
Check out this article to read more about du command.
Conclusion
Learning SSH commands is crucial for managing Linux server or VPS. It is the most effective way to navigate through your system and modify files or folders.
Thankfully, you have learned 17 essential SSH commands that every webmaster should know. Now you can easily perform basic tasks on your remote machine, such as creating files, deleting them, jumping between directories, and so on.
Feel free to comment below if you have any questions!
Comments
February 11 2018
Can anyone help with this? I am trying to change directory to 'file001', however cd is not available. Here's the text: user@user1$ ls -a /root/lists: . .. .file001 user@user1$ cd root/lists/file001 Sorry, bad command or not available
April 21 2018
cd .file001
December 03 2018
Thanks for your effort. Useful collection and good, clean description
April 28 2019
Thank you very much for helping you all ... The description and explanation of the subject attracts any interested in it .. Thanks again Greetings ...
September 17 2019
Thank you so much about your efforts .
April 07 2020
Thank you very much for for this info
July 29 2020
1. How do we exit the text editor "Nano/VI"? 2. What commands do we use to compile a C file?
September 06 2020
Hey there Magdalene! :) To exit the text editor, you can use use "Ctrl + X" :) To compile a C file you can use the following command: gcc filename.c Use the correct filename for the file you are trying to execute the command GCC for - so if your file is named "program.c" then it would be "gcc program.c" :) This will create an executable file with your code! You can also use "gcc -g filename.c" which in addition to the executable file will include debugging information in a dysm folder :) That's as much info I can fit into a comment for you! I hope it helps! :)
July 29 2020
Whether you are a new developer or want to manage your own application, the following 20 basic sysadmin commands can help you better understand your applications. They can also help you describe problems to sysadmins troubleshooting why an application might work locally but not on a remote host. These commands apply to Linux development environments, containers, virtual machines (VMs), and bare metal.
September 06 2020
Very much so! :)
August 04 2020
how to perform installations such nodejs?
November 06 2020
Hey there Carlos. You can check this awesome guide here to check how to install nodeJS on ubuntu! :)
August 06 2020
How to reset whole server, as im using ubuntu server and due to some issues i needed to make it reset what to do please help me!
November 06 2020
Hey there Raval! If you want to wipe the entire server to start from 0, and you are using Hostinger's VPS, you can just chose another OS from your VPS Panel in the Hostinger dashboard, and then select your current one again. This is the easiest way to reset the whole server. Hope this helps!
September 24 2020
How can I connect with Mysql mysql -u root -p I have tried with command.. its connected but i didn't know about the password. What is default password of mysql
November 18 2020
Hey there. The password is actually the password of your database user. If you are using Hostinger's shared or Cloud Hosting, you just need to click on the database name and change the password there.
November 28 2020
I would like to install a websocket server with ratchet library into my shared web hosting It is possible by ssh? Thanks for your reply Angelo
February 09 2021
Hi there, Angelo! Ratchet Library would only be available on a VPS plan. You can check them our on Hostinger homepage ;)
December 26 2020
Hello I am using PuTTY on windows to get an SSH connection with my site. I am prompted to login so I use my SSH Username and I am also using the password I setup in FTP but I have not been able to get a connection.
February 09 2021
Hi Luke :) What error message are you getting?
January 25 2021
After you issue curl, the output of the API call may be difficult to read. Sometimes, you want to pretty-print the JSON output to find a specific entry. Python has a built-in JSON library that can help with this. You use python -m json.tool to indent and organize the JSON. To use Python's JSON module, pipe the output of a JSON file into the python -m json.tool command.
September 27 2021
they are not "ssh command". SSH is just a protocol to connect on a remote machine. Coommand you describe are not "ssh command", they are unix/linux command from any shell connected.
September 27 2021
Hi Jerome, yes - SSH is a protocol which securely connects you to a remote server, while the commands used to operate through an SSH connected software are often referred to as "SSH commands". They are based on Linux, so you're right - the same ones apply for Unix and Linux OS.
May 31 2023
how do i install docker?
June 02 2023
Hello, check out our tutorial regarding Docker installation here for Ubuntu, for CentOS. Or how to install WordPress via Docker for Windows, Linux, or macOS.