Learning how to use SSH to access your WordPress site is something you may not have considered.Â For most WordPress users, the most familiar way to connect to your site will be via the WordPress dashboard and admin screens, combined with SFTP and phpMyAdmin to access the database.
But SSH might just prove to be one of the most valuable tools in your WordPress toolbelt.
If you want to speed up your workflow, youâ€™ll find that learning how to use SSH to connect to your WordPress site will make you more efficient.
In this post, weâ€™ll show you how to do it.
What is SSH?
First, letâ€™s start by identifying what SSH is and when you might use it.
SSH means â€˜Secure Shellâ€™. The SSH specs define it as:
â€œA protocol for secure remote login and other secure network services over an insecure network.â€
This means you can use it to access your WordPress site remotely, from any computer and regardless of where your site is hosted, as long as you have the login credentials. Using this method is inherently secure.
SSH is designed to provide secure login, so you can be confident no one can access your connection while you are using it. Itâ€™s also quick and easy to use once youâ€™ve got the hang of it. And if you want to interact with the WordPress REST API, itâ€™s one way to send commands.
To connect to your server via SSH, youâ€™ll need two things:
- An interface.
- Login credentials.
If youâ€™re running Linux or macOS, you have an interface built into your operating system, so you donâ€™t need to install an SSH client. But if youâ€™re running Windows, youâ€™ll need to install a client. Iâ€™ll show you how to do that in this post.
Tools You Need to Connect to Your Site via SSH
To start, youâ€™ll need to use either the terminal or an SSH client. These work in very similar ways.
Connecting via the Terminal on macOS or Linux
The Terminal is an application that comes with Linux or macOS, that allows you to use the command line to send commands, either to your machine or to a remote server.
It isnâ€™t a graphical interface so you wonâ€™t be using a mouse. Instead, you type in text commands. For a first-time user, it can be little disconcerting, as youâ€™re probably used to seeing visual representations of your commands. But once you get used to it, youâ€™ll find it quicker.
If you worked with computers in the days before graphical user interfaces, using systems such as Microsoftâ€™s MS-DOS, you might be familiar with this kind of interface. The good news is that the Terminal gives you many more options than MS-DOS did!
To open the Terminal on Mac, open Spotlight and type Terminal. You can also access it via Applications > Utilities.
To open Terminal on Linux, find it in the Applications menu. If it isnâ€™t immediately obvious, type Terminal into the prompt to find it.
Connecting via an SSH Client on Windows
An SSH client is a program that youâ€™ll need to install if youâ€™re running Windows, in order to connect via SSH.
The most popular SSH client is PuTTY. Youâ€™ll need to download and install that before you can access your site from Windows.
Installing the Putty SSH Client on Windows
Start by going to the PuTTY download page.
From here, choose the package that corresponds to your version of Windows. If you arenâ€™t sure whether to download the 32-bit or 64-bit version, check out the FAQ page. If in doubt, the 32-bit version is the safer option.
Click on one of the two options under MSI (â€˜Windows Installerâ€™). The file will download to your machine and run the installer.
Once PuTTY has installed, open it and you can then use it to connect to your site.
How to Connect to Your Server via the Command Line
Once youâ€™ve found or installed the Terminal or an SSH client, the next step is to connect to your remote site.
In either Terminal or an SSH client, you use the command line to connect and send commands to your server. Thatâ€™s the terminology Iâ€™ll use from now on, instead of referring to Terminal or an SSH client.
To do this, youâ€™ll need four pieces of information:
- Server address.
The server address is usually your domain name or IP address. If your site is hosted with Kinsta, use your IP address.
The username and password will be your FTP username and password, not the ones for your WordPress admin.
The port will be provided by your hosting provider. In some cases, a default port is used, but for extra security at Kinsta, we use different ports for different sites.
In MyKinsta, you can find the details by selecting your site and finding the SFTP/SSH section of the Info screen.
Just copy this and paste it into the command line.
Once youâ€™ve done that, you will be prompted for your password. Copy that from MyKinsta and youâ€™ll be given access to your server.
Youâ€™ll see a screen that looks something like this:
If youâ€™re not with Kinsta hosting, your screen will look a little different, but will still give you access to your server.
How to Use SSH to Interact with Your Site
Now you can start using SSH to interact with your server and your site. There are a number of SSH commands you can use as well as the commands provided by the WordPress Command Line Interface (WP-CLI).
SSH is a very powerful tool and if you arenâ€™t careful, you could break your site. It will let you add and delete files, so if you enter the wrong command, you could lose content or delete all your images, for example. Always use it with caution and make sure you backup your site before using it.
Basic SSH Commands
SSH comes with a range of commands you can use to connect and interact with your server.
Once youâ€™ve looked into your server via the command line, you simply type in SSH commands in the same way you would do if you were using Terminal to interact with your own computer.
Letâ€™s take a look at some of the most useful SSH commands you could start using.
- The ls Command
- The cd Command
- The mkdir Command
- The touch Command
- The cat Command
- The rm Command
- The cp Command
- The mv Command
The ls Command
ls command to get a list of the files and directories in your current location.Â You can add more to the command to get more information:
ls -ldisplays the details of the files, such as size, modified date and time, the owner, and the permissions.
ls -ashows hidden files and directories.
The cd Command
cd command to change directories.Â Youâ€™ll be taken to the new directory and the command line will indicate where you are:
You can then useÂ
ls again to find out whatâ€™s in the new directory.
You can useÂ
cd to move down more than one directory or to move up or across directories. To go up, enter two dots afterÂ
cd like this:
And to go to a specific directory, type the full directory path such as:
The mkdir Command
mkdir command to create a new directory. So if Iâ€™m in the themes directory, I could create a new directory for a new theme:
The touch Command
Once Iâ€™ve created my new directory, I could add a new file to it using the touch command:
The cat Command
To display the contents of a file, use theÂ
The rm Command
rm command to remove a file or folder. Use with caution, as this canâ€™t be undone:
The cp Command
cpÂ command is used to copy files and folders. You need to provide the name of the file or folder and the location where you want to copy it.
cp [options] [source] [destination]
You can also add options at the beginning of the command, but donâ€™t need to. So to copy the footer.php file from one theme to another when youâ€™re in the wp-content directory, you would use this:
Need a blazing-fast, secure, and developer-friendly hosting? Kinsta is built with WordPress developers in mind and provides plenty of tools through a powerful dashboard. Check out our plans
cp /themes/theme1/footer.php /themes/theme2/
Donâ€™t add a name for the destination file. If you do, and thereâ€™s already a file with that name at that destination, then the file will be overwritten with the contents of the file youâ€™re copying.
If you donâ€™t include a filename in the detonation and that file already exists, then you will get an error message and the copy wonâ€™t take place.
Iâ€™ve already mentioned that there are some optional elements you can add at the beginning of this command. These are:
-f: if you donâ€™t have write permission for the destination file, itâ€™ll be deleted and the command will create a new file.
-u: copy the source file only if it is newer than the destination file.
-n: will not overwrite an existing file.
-a: archive the files.
-R: for folders only, copy all the files and folders inside that folder.
The mv Command
As an alternative to copying a file, you can move it using theÂ
mv command. For this, you just provide the source and destination: there are no options.
mv /themes/theme1/footer.php /themes/theme2/
This will remove the footer.php file from theme1 and move it to theme2. If you do this with a folder, it will always move the files and folders within that directory too.
You can find information about more SSH commands in the SSH documentation.
Using WP-CLI to Interact with Your Site
In addition to the standard SSH commands, WordPress also gives you the WP-CLI interface, which provides even more commands. This includes interacting with files and folders, the admin options, and your database.
With Kinsta, WP-CLI comes with your hosting and you can start using it straight away. If your hosting provider doesnâ€™t give your WP-CLI, you can install it by following the steps in this guide to WP-CLI.
Testing and Troubleshooting Your SSH Connection
Sometimes you might find that SSH doesnâ€™t behave in the way you want it to. Either you canâ€™t connect, or the command youâ€™ve used doesnâ€™t work in the way you expect.
If that happens, try some of these troubleshooting tips.
If You Canâ€™t Connect Through SSH
If you canâ€™t connect to your server, take a look at any error messages or prompts that the command line gives you. Sometimes youâ€™ll have to reply to a yes/no prompt or fix a typo in your command.
If that doesnâ€™t work, check that the login credentials you provided are correct. You will be able to get these from the InfoÂ page for your site in MyKinsta. Other hosting providers will probably have an SSH section in cPanel or whichever dashboard theyâ€™re using.
If youâ€™ve tried again and it still doesnâ€™t work, try resetting your SSH password. Check that your internet connection is working and there isnâ€™t a firewall preventing you from accessing your server. If in doubt, close Terminal or your SSH client down and open it up again.
Log in to your hosting account and check that there arenâ€™t any server issues preventing you from gaining access. If youâ€™re with Kinsta, go to our system status page to find out if anything isnâ€™t working.
If youâ€™re on a public network, it may not allow SSH access to your port. Try testing it with other ports that you know are working.
If all else fails, open a support ticket with your hosting provider who will be able to access logs and work out why you havenâ€™t been able to connect.
If Your SSH Commands Donâ€™t Work as They Should
Another common problem is for an SSH command not to work in the way you expect it to. This is normally due to the command being entered incorrectly.
Hereâ€™s an example where I typedÂ
is instead ofÂ
Here, Iâ€™m presented with an error warning (
command not found) so I try again, with the correct command.
If your SSH commands donâ€™t work because the connection isnâ€™t working, you will see an error message telling you this. The command line constantly provides you with information on whatâ€™s going on, which is one of its benefits. In fact, it often gives you more and clearer information that a graphical interface.
How to Generate a New SSH Key Pair for Extra Security
Generating an SSH key pair adds an extra layer of security when compared to using your username and password to connect to SSH. You do it from within the Terminal or your SSH client. Hereâ€™s how.
Generating a key pair involves three steps:
- Generate the key pair in Terminal or in your SSH client.
- Add the key pair to your hosting account â€“ in MyKinsta you do this via the User Settings screens.
- Connect to your server in Terminal or your SSH client using the key pair.
You can find full instructions on doing this in our guide to generating SSH key pairs for macOS, Linux, and Windows.
Using SSH to connect to your server and your WordPress site will make you more efficient than relying on the WordPress admin interface, FTP, and even phpMyAdmin.
Follow the steps above to connect to your server via SSH and youâ€™ll find you can do more on your site without having to switch between applications or browser windows.
Now itâ€™s your turn: whatâ€™s your preferred terminal command you use on a daily basis? Let us know in the comments!