DEV Community

Cover image for The Developer’s Guide to Start Using SSH
Kinsta
Kinsta

Posted on

The Developer’s Guide to Start Using SSH

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 toolkit.

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.

Let's dive in.

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.

How to use SSH: Finding Terminal on Mac

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.

How to use SSH: PuTTY

Installing the Putty SSH Client on Windows

Start by going to the PuTTY download page.

How to use SSH: 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.

  • Username.

  • Password.

  • Port.

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.

MyKinsta SSH

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:

Kinsta SSH starting screen

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).

Important

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

Use the 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 -l displays the details of the files, such as size, modified date and time, the owner, and the permissions.

  • ls -a shows hidden files and directories.

How to use SSH: The ls command

The cd Command

Use the cd command to change directories. You’ll be taken to the new directory and the command line will indicate where you are:

The cd command

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:

cd ..
Enter fullscreen mode Exit fullscreen mode

And to go to a specific directory, type the full directory path such as:

cd public/wp-content/themes
Enter fullscreen mode Exit fullscreen mode

The mkdir Command

Use the 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:

mkdir mytheme
Enter fullscreen mode Exit fullscreen mode

The touch Command

Once I’ve created my new directory, I could add a new file to it using the touch command:

touch style.css
Enter fullscreen mode Exit fullscreen mode

The cat Command

To display the contents of a file, use the cat command.

How to use SSH: The cat command

The rm Command

Use the rm command to remove a file or folder. Use with caution, as this can’t be undone:

rm style.css
Enter fullscreen mode Exit fullscreen mode

The cp Command

The 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]
Enter fullscreen mode Exit fullscreen mode

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:

cp /themes/theme1/footer.php /themes/theme2/
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

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.

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:

  1. Generate the key pair in Terminal or in your SSH client.

  2. Add the key pair to your hosting account – in MyKinsta you do this via the User Settings screens.

  3. Connect to your server in Terminal or your SSH client using the key pair.

Adding SSH key in MyKinsta

You can find full instructions on doing this in our guide to generating SSH key pairs for macOS, Linux, and Windows*Summary*

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!

Top comments (0)