1. Install an SSH Client
The first step to using SSH is to install an SSH client. Popular SSH clients include PuTTY for Windows, and OpenSSH for Linux and Mac.
2. Generate an SSH Key
Once you have an SSH client installed, you will need to generate an SSH key. This key will be used to authenticate your connection to the server.
3. Connect to Your Server
Once you have an SSH key generated, you can connect to your server. To do this, you will need to know the IP address or hostname of the server, as well as the username and password associated with the account.
4. Execute Commands
Once you are connected to the server, you can execute commands. This can be done by typing the command into the terminal window.
5. Transfer Files
You can also use SSH to transfer files between your local computer and the server. This can be done using the SCP (Secure Copy) command.
6. Disconnect
When you are finished using SSH, you can disconnect from the server by typing the “exit” command. This will close the connection and log you out of the server.
[ad_1]
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.
- 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.
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).
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.
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:
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 ..
And to go to a specific directory, type the full directory path such as:
cd public/wp-content/themes
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
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
The cat Command
To display the contents of a file, use 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
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]
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/
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 ls
:
Here, I’m presented with an error warning (command not found
) so I try again, with the correct command.
Sometimes you might type the command correctly but get the parameters wrong. If this is the case, check the SSH documentation for SSH commands and the WP-CLI documentation for WP-CLI commands.
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.
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!
[ad_2]
Source link