There are a couple of reasons why you should learn how to use SSH to manage your WordPress site.
First, SSH offers a secure, fast, and convenient way to manage your site.
Second, it’s a powerful tool for creating, managing and deleting site files.
Third, you can take back control of your site when you can’t log in with the admin credentials.
Fourth, using the command line is really cool! I bet you have seen lots of movies with geeks who do many things by using the command line.
Check out this post to learn what SSH is, how to connect to your site over SSH, and a few basic commands for managing a site.
- This article is not for developers only. Writing a few basic SSH commands isn’t rocket science. Still, pay close attention to each explanation to get the most from this article.
- Before delving deeper into the details, check if your hosting allows an SSH connection. It’s not a rule, but a few shared hosting plans don’t allow users to connect to their sites over SSH.
- A wrong SSH command may result in a huge fiasco. Ideally, connect to a test site instead of your most important site. Create a backup copy to revert to if bad things happen.
What Is SSH
SSH stands for Secure Shell Access. According to the Network Working Group, it’s “a protocol for secure remote login and other secure network services over an insecure network.”
In plain English, SSH is a protocol that allows for secure remote logins to your site. As long as you use the proper credentials, you can remotely log in to the server that keeps your site’s files from any computer.
Managing your site directly from the dashboard is intuitive and straightforward. The WordPress dashboard is enough for most users’ needs, but it comes with a few limitations regarding file management, automation, and remote access. That’s where SSH comes in, complementing the WordPress dashboard.
Most likely, you used an FTP (File Transfer Protocol) client to upload or edit a file in the root of your site. Using an FTP client is simple, but it has a significant drawback: an insecure connection.
FTP has existed since the early days of the internet when there were no security issues. SSH is similar to FTP, but it’s secure, fast, and for Linux, and macOS configurations, doesn’t require you to install any software.
How to Connect to WordPress Over SSH
The SSH client (a.k.a., you) initiates a connection to the server and checks its identity with a public key. The server sends back a public key to confirm its identity. Next, the client and the server agree upon the encryption used and start sending data requests.
You are lucky if your computer runs on Linux or macOS because you don’t need special preparation to manage your site over SSH. Terminal is all you need.
The users of Windows 8 or earlier versions must download an SSH client, such as PuTTY.
Windows 10 includes an SSH client, but it’s not installed by default. You have to go to Settings > Apps, click on Manage optional features, choose Add a feature, and look for OpenSSH Client. Install it, and you are good to go.
At this stage, you’ve set the interface to connect over SSH, but you need the credentials to access the site. Most likely, they are included in the first emails sent by the hosting provider.
You will need the server address, username, password, and port. If you can’t find that email, go to your cPanel and search for a section like SSH Access, SSH, or SSH Credentials. (Each host has its unique interface, so it’s impossible to offer a precise name for the SSH section.)
If you still can’t find it, contact your host’s support and ask for the proper guidance.
First SSH Commands
If you are on Linux or macOS, open Terminal and type:
Replace username and server.name with your username and server name to connect over SSH to your site.
If you are on Windows, open the PuTTy interface and fill out the needed pieces of information (the hostname, port, and connection type) to connect to your site root. If you did it correctly, you can now access your site files over SSH using the command line.
If you correctly configured the SSH connection, the following commands are all you need to manage your site.
You have access to your site root, but you don’t know what each site file is. First things first: you should have a bird-eye view of your site, and the ls SSH command is what you need to get it. Use it to display details about the files within the current directory.
ls -a displays hidden files, and ls -l shows the file details (modified date and time, size, ownership).
This command reveals the full path of your location on the server. It helps to make sure that you are in the proper location.
If you want to create a new directory, type in this command:
mkdir folder name
Replace “folder name” with the name of your site’s folder.
This command removes a directory. Just type in the directory name to delete it. Bear in mind that there is no undo option. Don’t skip backing up the site before using the SSH commands.
Use this command when you need to jump between directories. Type in this command to get into the desired directory:
cd folder name
“cd /” is to get to the root directory, and “cd –” goes to the previous directory.
Use the cp command to copy a file or directory. The complete syntax is:
cp [options] [source] [destination]
“[options]” can take values like:
- “cp -r” copies all the content of a folder.
- “cp -a” archives the files.
Pay attention—this command overwrites the destination folder if it exists, and there is no undo option.
This command moves a file or a directory. The complete syntax is:
mv [source] [destination]
You can keep the same name or rename the file or the directory you move. Use mv myfile.extension /dir/ to keep the same name for the file or directory or mv myfile.extension /dir/myfile1.extension to rename the file or directory.
Use this command to create a new file. For instance, this command creates an empty users.txt file:
Type in this command if you want to display the content of a file:
This command removes a file. Just type rm and the name of the file:
Use this command when you want to search for a particular string in a file. The complete syntax is:
grep [options] [“search term”] [filename]
grep -i [“search term”] [filename] is for insensitive search terms. grep -c [“search term”] [filename] counts how many times the search term is in the file.
Use this command to search for a particular file or directory. The syntax is:
find [starting directory] [options] [search term]
There are three variables for the starting directory:
- / (slash) searches through all the folders and directory.
- . (dot) searches through the current directory.
- ~ (tilde) searches through the home directory.
Use options to filter your search by name, user, and size. For example, this command find ~ -name “footer” searches the files for the name footer in the home directory.
This command is helpful when you want to read the first lines of a file. It displays the first ten lines.
This command is helpful when you want to read the last lines of a file. It displays the last ten lines.
Use wget to download a file from the internet to the current directory by specifying the full path of the file. For instance,
This command lets you know the size of files or folders. Use -h to make sure that the information is in a human-readable format. The following command displays the size of the footer.php file:
du -h footer.php
This command is simple and effective. Just type clear to declutter the terminal screen and start with a blank one.
Don’t remember the latest commands? Don’t worry. Use history to display the latest commands. Don’t forget to mention how many commands you want to display. For instance, history 10 displays the ten most recent commands.
Use this command to compress files and folders. The basic syntax is:
Worth mentioning choices for [options] are -r, which compress the folder and doesn’t delete it, and -m, which deletes the folder.
This command decompresses the zip files in your current directory.
Connecting to your site over SSH is a task for medium to advanced users. It requires a lot of attention because of every small detail matters. Don’t rush yourself. Be patient, and you will master the terminal.
The above 20 SSH commands are enough to edit your site but use them carefully to edit or delete the wrong files.