SSH (Secure Shell) provides a secure and efficient way to access your hosting or server environment. It allows you to manage website files, execute commands, and interact with your database seamlessly.
With SSH, the connection to your filesystem remains encrypted and protects sensitive data from interception, including threats such as Man In The Middle attacks, and unauthorised access.
For enhanced security, we enforce SSH access with public and private key authentication. This method eliminates the risk of brute-force attacks by requiring cryptographic key pair instead of a password.
Public and private key authentication works by generating a unique key pair; a public key stored on the server and a private key kept on your local device. Tools like PuttyGen can help create these keys. When you attempt to connect, the server verifies your identity using the matching keys.
In this article, we’ll explore the essential commands to efficiently manage your WordPress web hosting environment using SSH .
Read our support guide on how to connect over SSH across our shared and managed hosting services and the best SSH clients
File Management
Navigate Directories
Here are a few commands for managing files and navigating directories in a Linux environment:
cd
To change the current directory to another directory, you’d use the ‘cd’ command.
cd /path/to/directory
For example, the following will change to the wp-content folder:
cd public_html/wp-content

pwd
Sometimes, you will want to check where you are in the server environment. To do this, use g pwd. This command prints the current working directory.
pwd
/path/to/folder/public_html/wp-content

ls -la
This command lists all files and directories in the current directory, including hidden ones (files that start with ‘.’ such as .htaccess file) with details like permissions, owner, size, and modification date.

File Operations
cp
The cp command allows you to copy files or directories to a new location. This is useful for creating backups or duplicating files.
Examples:
This command copies the contents of myfile.php to myfile2.php
cp myfile.php myfile2.php

this command copies the file myfile.php to the /plugins folder
cp myfile.php public_html/wp-content/plugins

mv
The mv command moves files or directories to a new location or renames them.
Examples:
Renaming a file
mv oldname.php newname.php

Move the wp-config-sample.php file to the parent directory:
mv wp-config-sample.php ..

rm
The rm command is used to delete files or directories. Use this command with caution, especially when deleting directories.
To delete a file:
rm file

To delete a directory and all its contents recursively and forcefully
rm -rf directory

grep
The grep command searches for a specific string of text within files. This is especially useful for locating code snippets, configuration details, or errors.
Find occurrences of a specific function or string
grep 'wp_enqueue_script' *.php

Search across all WordPress files recursively
grep -r 'get_option' /path/to/wordpress/

File Permissions
chmod
The chmod command allows you to modify file and folder permissions, which is crucial for maintaining security and proper functionality of your server environment.
755 Grants full access to the owner and read/execute permissions to others, commonly used for directories.
chmod 755 directory_name

644 Grants read/write access to the owner and read-only access to groups and others, typically used for files.
To apply these permissions across your WordPress site you can change all file permissions and directory permissions by running the following commands.
This searches for directories in the wordpress directory and updates the permissions to 755
find /path/to/wordpress -type d -exec chmod 755 {} \;

This searches for files in the wordpress directory and updates the permissions to 644find /path/to/wordpress -type f -exec chmod 644 {} \;

View File Contents
When managing a WordPress site or server using SSH, you’ll often need to view or edit configuration files. Here are two essential commands to help you
cat file
This command displays the entire contents of a file.
In this example, this shows the contents of the .htaccess file, used for configuring apache settings.
cat .htaccess

vim file
Opens a file in the vim text editor for editing.
Example:
vim .htaccess

Database Management
Managing a database is essential for maintaining and troubleshooting websites and applications. Below are some basic commands to access and manage your WordPress database.
Access MySQL/MariaDB
Logs into the database server. You’ll be prompted to enter the password for username.
mysql –h hostname -u username -p
Lists all databases available on the server. End with a semicolon (;) to execute the query.
SHOW DATABASES;
Switches to a specific database
USE database_name;
Lists all tables in the currently selected database.
SHOW TABLES;
Export/Import Databases
Creates a backup of database_name and saves it to backup.sql.
mysqldump -u username -p database_name > backup.sql
Restores the database from a backup file.
mysql -u username -p database_name < backup.sql
WordPress specific basic MySQL queries
List All WordPress Users
SELECT ID, user_login, user_email FROM wp_users;
Get a List of Installed Plugins
SELECT option_value FROM wp_options WHERE option_name = 'active_plugins';
Delete Spam Comments
DELETE FROM wp_comments WHERE comment_approved = 'spam';
Reset user password
UPDATE wp_users SET user_pass = MD5('NewPassword123') WHERE user_login = 'admin';
(Replace NewPassword123 with a more secure password and you can replace ‘admin’ with the actual username)
WordPress-Specific SSH Commands (WP-CLI)
You can manage your WordPress website using WP-CLI which is the command line for WordPress. You will be able to perform WordPress specific tasks such as WordPress core updates, Plugin updates, Theme updates, Plugin/Theme installs and more.
Updates the WordPress core to the latest version.
wp core update
Installs a plugin by name and activates it immediately.
wp plugin install plugin_name --activate
Example
wp plugin install woocommerce --activate.
Installs and activates a theme.
wp theme install theme_name --activate
Example:
wp theme install twentytwenty --activate
Displays a list of all WordPress users with details like username, email, and roles.
wp user list
Replaces instances of old_url with new_url in the database. Useful when migrating or updating site URLs.
wp search-replace 'old_url' 'new_url'
Debugging
Enables debugging in the wp-config.php file. Helps identify issues in development.
wp config set WP_DEBUG true --raw
Lists all scheduled WordPress cron jobs
wp cron event list
Summary
The commands in this article provide you a helpful starting point for managing your WordPress website’s hosting environment using SSH.
While SSH may seem complicated and daunting at first, with practice, it becomes an indispensable tool.
To get comfortable, consider creating a test environment, such as a fake hosting package in your Reseller account or Managed Cloud server, where you can safely practice SSH commands without affecting your live websites.
By mastering these basic commands, you’ll be well on your way to effectively managing your server and securely maintaining your site.
Read more on SSH – The Best SSH Clients for Secure Remote Connections
Add comment