SSH Questions
SSH support articles: everything you need to know about Secure Shell Protocol to connect your web hosting server securely.
How do I connect a website via SSH?
SSH, or ‘Secure Shell’, is a method of remotely accessing and managing your site files, as well as allowing for configuration changes and remote command execution within your file space. SSH on the 20i platform is encrypted, and protected by either 2FA or public/private key pairs to validate the connection, making it reliable and secure.
Below are instructions for connecting to your site via SSH for Windows, Apple Mac and Linux devices using public/private key pairs. If you wish to connect using 2FA, please see our guide How do I connect to SSH using 2FA?
Note: It's only recommended that you connect to your package via SSH if you're familiar with the command line interface.
Note: If your SSH connection closes immediately, your account may be blocked for unauthorized Sudo use. Please contact support for assistance.
Windows
If you're using Windows, you'll need to download an SSH Client to access SSH.
Note: This guide uses PuTTY which is a free SSH client for Windows and can be downloaded here:
https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
- Once you have downloaded PuTTY, search for and open PuTTY Key Generator on your computer.
- Select Generate and move the mouse around in the gray box to generate some Randomness. This will generate a public key.
- Add a passphrase (password). This is what you'll need to put into the command line interface.
- Log in to My20i, head to Manage Hosting and select the package that you want SSH access for. Then select, SSH Access from under the Security section.
- Then input the public key that was generated from PuTTYgen into the Public Key box. A handle will be generated automatically. Then select Add Public Key.
- Head back to PuTTY Gen and select Save private key. Save this as any name you want.
- Open PuTTY. Down the left-hand side, select to expand the SSH, select Auth and then Credentials.
- There should be an area labelled Private key file for authentication – select Browse next to this and add the private key you have just saved.
- Head back to Session using the left-hand menu and add the SSH host name. The hostname you wish to use depends on what data center you’re using. For the UK data center, use ‘ssh.gb.stackcp.com’, and for the US use ‘ssh.us.stackcp.com’. Then, once that has been added, select Open.
- The command line interface will then open. You’ll want to enter your domain here as your username (e.g. domain.co.uk).
- You'll then be prompted for the passphrase that you set earlier. Enter this in the prompted area – PuTTY will hide the characters being entered for security purposes, so do not worry if any input doesn’t seem visible.
- You'll then be logged into your account and have full access to your SSH area on your site.
Linux
Connecting to SSH on Linux will require you to set up a Public/Private Key pair on your local machine, add it to the SSH area on your package and then connect using the details provided.
- Open a Terminal session, and navigate to the directory that you wish to store your Public/Private Key pair in.
- Enter the command
ssh-keygen
and follow the on-screen prompts – the tool will prompt you for a name for the file, as well as setting a password. This is what you'll need to put into the command line interface later, so make note of it once set. - Once the files have been created by the process, log in to My20i, head to Manage Hosting and select the package that you want SSH access for. Then select, SSH Access from under the Security section.
- Back in your Linux terminal, use the command
cat
to view the contents of your public key file. This will be the file ending in .pub that was generated by the command earlier and should use the name you set.
For example, if you chose to name your key example, the command to open it would becat example.pub
- Copy the contents of the file, and paste it into the Public Key box in the SSH area on My20i. A handle will be generated automatically. Then select Add Public Key.
- Now that you’ve got your public key added to My20i, you can connect via SSH by using the command:
ssh -i keyname hostname
Where keyname is the name of your private key file you entered earlier. The hostname you wish to use depends on what data centre you’re using. For the UK data centre, use ‘ssh.gb.stackcp.com’, and for the US use ‘ssh.us.stackcp.com’. - You’ll be asked if you wish to connect using the key. Enter yes.
- You'll then be prompted for the passphrase that you set earlier. Enter this in the prompted area – PuTTY will hide the characters being entered for security purposes, so do not worry if any input doesn’t seem visible.
- You'll then be logged into your account and have full access to your SSH area on your site.
MacOS
To connect via SSH on a Mac you can follow this video or the instructions below:
- Log in to My20i.
- Head to Manage Hosting and select Manage on the hosting package you want to connect to.
- Select the SSH Access icon.
- You’ll now need to generate a public key, on a Mac this is done via the Terminal utility. Open Terminal on the Mac.
- Enter the following command into the Terminal:
ssh-keygen
- Press Enter
- You’ll be prompted to enter the location that you want the public and private keys saving. In this case, we’ll keep this as the default location. Press Enter
- You’ll be prompted for a passphrase, in this guide we’ll just leave this empty. If you enter one, make sure you remember this as you’ll be prompted for this later. Press Enter.
- You’ll be asked to confirm the passphrase, since we’ve left this empty just press Enter.
- Your keys will have been generated and the public key will have been saved in a specified location, in our example it has been saved in:
/Users/20i/.ssh/id_rsa.pub
- Copy the path and enter the following command:
cat /Users/20i/.ssh/id_rsa.pub
- The key will be outputted, copy the whole key from the start to the end.
- Paste the key back in My20i in the Public Key field in the SSH Access section. The handle should be added automatically. Press Add Public Key. Please note that can take up to 5 minutes.
- Head back to Terminal and enter the SSH username and hostname found at the top of the SSH Access section, it should be something like domain.com@ssh.gb.stackcp.com for packages in the UK, and domain.com@ssh.us.stackcp.com for packages in the US. You’ll need to prefix it with the ssh command as follows:
ssh domain.com@ssh.gb.stackcp.com
Orssh domain.com@ssh.us.stackcp.com
You’ll be prompted to confirm you would like to connect, simply type yes and press enter.
You should have successfully connected via SSH from your Mac to your hosting package at 20i.
How do I import a .sql file via the command-line?
If you've set up SSH access for a package then you can use one of two methods to import a MySQL database via the CLI.
If needed, please see our guide on how to connect to your package via SSH.
MySQL CLI
To import a SQL file via the command line, you can use the mysql command. Here’s how.
mysql -h hostname -u username -p database_name < file.sql
Replace hostname with your database server name.
Replace username with your database username.
Replace database_name with the name of the database where you want to import the SQL file.
Replace file.sql with the path to the SQL file you want to import.
After entering the command, you'll be prompted to enter the password for the specified username.
Enter the correct password, and the import process will begin.
Make sure the SQL file contains valid SQL commands and is in the correct format for the database you're using.
If the import is successful, the data from the file will be imported into the specified database.
Compressed Files
If the database file is zipped then you’ll need to unzip it first.
For a .zip file you can use the unzip command:
unzip databasefile.sql.zip
For a .gz file you can use the gunzip command:
gunzip databasefile.sql.gz
From here you can proceed to import the unzipped file as normal.
If the database file is zipped then you’ll need to unzip it first.
WP CLI
To import a SQL file using the WP-CLI tool, you can follow these steps. WP-CLI is a command-line tool for managing WordPress installations, and it includes a command for importing databases.
1. Navigate to your WordPress installation:
SSH into your package and navigate to the root directory of your WordPress installation.
2. Backup your current database (optional but recommended):
It's a good practice to create a backup of your existing database before performing an import. You can use the following WP-CLI command to export your database:
wp db export
This command will create a SQL dump of your database in the WordPress installation folder. You can use it as a backup in case anything goes wrong.
3. Import the SQL file:
Use the wp db import command to import the SQL file. Here's the command:
wp db import file.sql
4. Verify the import:
After the import is complete, you can verify by checking your WordPress site to ensure that the data from the SQL file has been imported correctly.
That's it! You've successfully imported an SQL file into your WordPress database using WP-CLI.
This method is especially useful for migrating WordPress sites or importing data into a new WordPress installation.
How do I connect to SSH using 2FA?
It's only recommend that you connect to your package via SSH if you're familiar with the command line interface. Here is how to connect using two-factor authentication (2FA).
Setting up your authenticator:
You will want to first download the Google Authenticator app which is available for both iOS and Android devices.
⦁ Log in to My20i, head to Manage Hosting and select the package that you require SSH access for. Then select SSH Access under the Security section.
⦁ Under Set SSH password, set the password and make note of this for later. Then select Set up Authenticator and proceed to scan the QR code on your iOS or Android device.
Connect SSH via 2FA: Windows
If you're using Windows you will want to download an SSH client to to access SSH.
You can download PuTTy from the following page: https://www.putty.org/
⦁ Open PuTTY and enter the SSH hostname here. The hostname you wish to use depends on what data centre you’re using. For the UK, use ‘ssh.gb.stackcp.com’, and for the US use ‘ssh.us.stackcp.com’. Then, select Open.
⦁ The command line interface will request you for a login. Log in with your domain (e.g. example.co.uk)
⦁ You will then be prompted for a verification code. Open Google Authenticator and enter your code.
⦁ Enter the SSH password which was set up earlier.
⦁ You'll now have access to SSH through 2FA.
Connect SSH via 2FA: Apple Mac
For Mac, you can make use of the Terminal utility, which should be available as standard.
- Open Terminal and enter the SSH hostname and username found at the top of the SSH Access section. This will follow the format of domain.com@ssh.gb.stackcp.com for packages in the UK, and domain.com@ssh.gb.stackcp.com for packages in the US. You’ll need to prefix it with the ssh command as follows:
UK: ssh domain.com@ssh.gb.stackcp.com
US: ssh domain.com@ssh.us.stackcp.com
- You’ll be prompted for a password – this will be the password you set during the creation of the 2FA setup. Enter this, and then your Google Authenticator code when prompted.
- You’ll now have SSH access
Basic UNIX commands and utilities
When managing your 20i web hosting, you may wish to enable SSH access for one or more of your packages.
Enabling SSH access will give you easier management of your package(s), as you'll be able to use UNIX commands and utilities to carry out tasks and activities.
Here's a list and explanation of some basic commands:
ls – 'ls' (list) is perhaps one of the more commonly used commands. It will list all of the files in the current working directory (the directory the user is currently-in).
[20i@localhost Public]$ ls
Documents Music Pictures
[20i@localhost Public]$ ls -lah
total 20K
drwxr-xr-x. 5 20i 20i 4.0K Jan 6 10:03 .
drwx------. 28 20i 20i 4.0K Jan 6 10:00 ..
drwxrwxr-x. 2 20i 20i 4.0K Jan 6 10:03 Documents
drwxrwxr-x. 2 20i 20i 4.0K Jan 6 10:03 Music
drwxrwxr-x. 2 20i 20i 4.0K Jan 6 10:03 Pictures
[20i@localhost Public]$
-lah - this series of letters changes the appearance of the output: 'l' means in the long list format (so it's ordered and more readable), 'a' means all files/directories (don't ignore hidden files that begin with a '.') and 'h' means to print sizes of files/directories in a human readable format, e.g., 1.0K, 234M, 3.5G instead of the exact number of bytes.
pwd – the 'pwd' command (print working directory), will output/write the full path of the current working directory.
[20i@localhost home]$ pwd
/home
[20i@localhost home]$
cd – 'cd' (change directory) allows a user to move from one directory to another.
[20i@localhost ~]$ cd Documents/
[20i@localhost Documents]$
mv – 'mv' (move) lets you move files and directories from one location to another.
[20i@localhost Public]$ ls -lah
total 20K
drwxr-xr-x. 5 20i 20i 4.0K Jan 6 10:06 .
drwx------. 28 20i 20i 4.0K Jan 6 10:00 ..
drwxrwxr-x. 2 20i 20i 4.0K Jan 6 10:03 Documents
-rw-rw-r--. 1 20i 20i 0 Jan 6 10:06 foo.txt
drwxrwxr-x. 2 20i 20i 4.0K Jan 6 10:03 Music
drwxrwxr-x. 2 20i 20i 4.0K Jan 6 10:03 Pictures
[20i@localhost Public]$ mv foo.txt Documents/
[20i@localhost Public]$ cd Documents/
[20i@localhost Documents]$ ls -lah
total 8.0K
drwxrwxr-x. 2 20i 20i 4.0K Jan 6 10:07 .
drwxr-xr-x. 5 20i 20i 4.0K Jan 6 10:07 ..
-rw-rw-r--. 1 20i 20i 0 Jan 6 10:06 foo.txt
[20i@localhost Documents]$
cp – 'cp' (copy) will allow a user to copy files and directories from one location to another.
[20i@localhost Documents]$ ls -lah
total 12K
drwxrwxr-x. 2 20i 20i 4.0K Jan 6 10:09 .
drwxr-xr-x. 6 20i 20i 4.0K Jan 6 10:11 ..
-rw-rw-r--. 1 20i 20i 12 Jan 6 10:09 foo.txt
[20i@localhost Documents]$ cp foo.txt foo2.txt
[20i@localhost Documents]$ ls -lah
total 16K
drwxrwxr-x. 2 20i 20i 4.0K Jan 6 10:14 .
drwxr-xr-x. 6 20i 20i 4.0K Jan 6 10:11 ..
-rw-rw-r--. 1 20i 20i 12 Jan 6 10:14 foo2.txt
-rw-rw-r--. 1 20i 20i 12 Jan 6 10:09 foo.txt
[20i@localhost Documents]$
rm – 'rm' (remove) allows the removal of many types of objects, such as, files, directories, and symbolic links.
[20i@localhost Documents]$ ls -lah
total 12K
drwxrwxr-x. 2 20i 20i 4.0K Jan 6 10:14 .
drwxr-xr-x. 6 20i 20i 4.0K Jan 6 10:11 ..
-rw-rw-r--. 1 20i 20i 12 Jan 6 10:14 foo2.txt
-rw-rw-r--. 1 20i 20i 12 Jan 6 10:09 foo.txt
[20i@localhost Documents]$ rm foo.txt foo2.txt
[20i@localhost Documents]$ ls -lah
total 16K
drwxrwxr-x. 2 20i 20i 4.0K Jan 6 10:16 .
drwxr-xr-x. 6 20i 20i 4.0K Jan 6 10:11 ..
-rw-rw-r--. 1 20i 20i 12 Jan 6 10:09 foo.txt
[20i@localhost Documents]$
man – the 'man' (manual) command can be used to display the documentation/manual page(s) for command line tools.
[20i@localhost ~]$ man grep
mkdir – 'mkdir' (make directory) can be used to create a directory.
[20i@localhost Public]$ ls -lah
total 20K
drwxr-xr-x. 5 20i 20i 4.0K Jan 6 10:07 .
drwx------. 28 20i 20i 4.0K Jan 6 10:09 ..
drwxrwxr-x. 2 20i 20i 4.0K Jan 6 10:09 Documents
drwxrwxr-x. 2 20i 20i 4.0K Jan 6 10:03 Music
drwxrwxr-x. 2 20i 20i 4.0K Jan 6 10:03 Pictures
[20i@localhost Public]$ mkdir Downloads
[20i@localhost Public]$ ls -lah
total 24K
drwxr-xr-x. 6 20i 20i 4.0K Jan 6 10:11 .
drwx------. 28 20i 20i 4.0K Jan 6 10:09 ..
drwxrwxr-x. 2 20i 20i 4.0K Jan 6 10:09 Documents
drwxrwxr-x. 2 20i 20i 4.0K Jan 6 10:11 Downloads
drwxrwxr-x. 2 20i 20i 4.0K Jan 6 10:03 Music
drwxrwxr-x. 2 20i 20i 4.0K Jan 6 10:03 Pictures
[20i@localhost Public]$
grep – 'grep' (global regular expression print) is a useful command line tool that allows you to search for patterns, and their corresponding lines, in the input file(s) specified.
[20i@localhost Documents]$ grep -n Test foo.txt
1:Test.
[20i@localhost Documents]$
du – 'du' will show the disk usage of the files and directories in the directory specified (if a directory is not specified, the current working directory will be used).
[20i@localhost Public]$ du -sh Documents/
8.0K Documents/
[20i@localhost Public]$
How do I connect to an Unmanaged VPS using SSH?
Unmanaged Virtual Private Servers from 20i are pre-configured to be accessible using SSH.
To connect to your VPS via SSH you’ll need to make sure you have an SSH client available in order to connect - If you’re running Mac OS or Linux you should be able to utilise your terminal application to connect. If you’re running Windows we’d recommend downloading and using PuTTY to connect.
https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
In this guide, we’ll describe how to use PuTTY to connect.
- In My20i, select Manage VPS.
- Head to Options -> Manage on the VPS you wish to connect to.
- In the VPS Information section on the right-hand side, you’ll note the IP Address, Username and Password.
- Open PuTTY and in the Host Name (or IP address) field, add the IP address from the VPS Information section. Select Open.
- You should be prompted with login as: to which you’ll just need to enter the username.
- Finally, you’ll then be prompted for the password, simply enter the password for the VPS as found in the control panel.
You should now be connected to your VPS via SSH.
Is Git supported on your hosting platform?
The short answer is yes. We offer Shared Web Hosting with Git, WordPress hosting with Git and Managed Cloud Hosting with Git. Our Self-Managed VPS also support Git, but do not include our version control GUI.
What is Git?
If you're not familiar with Git, this is what’s called a ‘version control system’. It helps developers of all kinds keep track of the changes they make to files, make records of what’s been altered, and maintain different versions of your code that you can swap between should something go wrong. We have a Beginner’s Guide to Git here.
There are two ways to use Git on our platform:
The first is our own Git Version Control tool which will show on the control panel when managing a hosting package. The tool simplifies Git, giving you graphical user interface. To get to this tool, you need to manage a hosting package from the manage hosting page and it will show under the Web Files section. We have a full guide on how to use this tool here.
The second way to use Git is through SSH. To use this, you’ll need to enable SSH access on the hosting package or Self-Managed VPS. This can be enabled when managing the hosting package within your account and there are multiple ways to do this as explained in the guides here: https://www.20i.com/support/reseller-hosting/connect-via-ssh and https://www.20i.com/support/ssh/connect-ssh-2fa
With the above information you can start using Git with any hosting you have with 20i.
How do I connect to a site with WinSCP?
FTP uploads on our system have a maximum size of 5GB. If you need to upload files larger than this, you'd need to upload these via SSH.
One such tool you can use is WinSCP. With WinSCP, you can connect to your site files via SCP to upload/download, much as you would a standard FTP client.
Firstly, you'll need to create a public/private key-pair as you would when connecting via SSH normally. If you're unfamiliar with this, our SSH guide covers this in more detail. You'll need to connect to your hosting package using this method before continuing, so make sure to set this up and then try connecting via PuTTY to confirm it's up and running before moving on to the next step.
- Log in to My20i, head to Manage Hosting and select the package that you want SSH access for. Then select SSH Access from under the Security section.
- As in the previous guide, input the Public Key that was generated from PuTTYgen into the Public Key box, which will generate a handle for it. Then, select Add Public Key on the page.
- Once done, save the private key locally under any name you want: you'll need this later.
- Open WinSCP. You'll be prompted with a 'Login' form upon loading. You'll want to select 'SCP' under 'File protocol:' at the top.
- Then enter the following details:
- Host name: The hostname will depend on what data centre your package is in. For UK packages, this should be ssh.gb.stackcp.com. For packages in the US, please use ssh.us.stackcp.com.
- User name: The domain name of the package
- Port: 22
- Leave the password area of the form blank for now and hit 'Advanced' beneath it. You'll be greeted with an Advanced Site Settings box:
- Navigate to SSH > Authentication and hit the '...' box under 'Private key file'. You can select your Private Key you saved earlier from here.
- Once that's added, hit OK and then Login at the bottom. You'll get a prompt for the password you set with the key pair earlier: enter it here.
- You'll then be fully connected and can use WinSCP to upload/download files as you would a standard FTP client.
How to use SFTP to transfer files securely with a remote server
FTP or “File Transfer Protocol” is a standard protocol for transferring files between two servers. FTP can also be insecure if not properly encrypted. If you’re using one of our Managed VPS or our shared platform then your FTP connection will be encrypted and secure.
SFTP, SSH File Transfer Protocol or Secure File Transfer Protocol is a different protocol that comes with SSH and works in a similar way. Unlike FTP, SFTP is always made over a secure connection. This connection can then be used to transfer files between servers and to traverse the filesystem on both servers.
When connecting to us, as both protocols are secure, you can use your protocol of choice. However, if you’re connecting to a system you’re unfamiliar with, in most cases you’re best off using SFTP for the connection. It’s more secure.
SFTP is part of many UI-based tools, and for those, we recommend referring to a guide for the tool in question. In this guide, we’ll demonstrate the use of SFTP through its command line interface.
Connecting using SFTP?First off, if you’re connecting to a package with us, you’ll first need to make sure that you’ve unlocked the FTP/SFTP on the package in question. It can be unlocked for either a period of time or for the IP address you’ll be connecting from. FTP/SFTP can be unlocked on the right hand side of any package's control panel.
When connecting to us using either FTP or SFTP you’ll need to unlock it here first and then you’ll be able to connect using the given details. If the domain in question has a CNAME set up to point ftp.domain.com to ftp.stackcp.com then you can also connect to our FTP/SFTP using ftp.domain.com. If in doubt ftp.stackcp.com will always work though.
If you’re connecting by SFTP elsewhere you may need to refer to connection guides for the supplier in question. When connecting to a general server the same authentication methods are available that are present in SSH.
When using your own server we would recommend creating SSH keys and transferring your public key to any system that you need to access. This is more secure and can save you time in the long run.
Once the SFTP is unlocked with us or you have set up SSH keys or a different authentication method for your own server, you should now be able to connect to it using the following:
$ sftp username@serverhostname
So using the example from Image1 above that would be:
$ sftp example.com@ftp.stackcp.com
If you are working on a custom SSH port (not the default port 22), then you can open an SFTP session as follows:
$ sftp -oPort=custom_port username@serverhostname
This won’t be necessary if using our shared or Managed Hosting on a VPS.
Once you’re connected to the remote server your prompt will change to an SFTP one.
sftp>
The best command to learn first with SFTP is the help command. This will give you a summary of available SFTP commands. You can get help by typing either:
sftp> help
Or
sftp> ?
This will display a list of commands:
Available commands: bye Quit sftp cd path Change remote directory to 'path' chgrp grp path Change group of file 'path' to 'grp' chmod mode path Change permissions of file 'path' to 'mode' chown own path Change owner of file 'path' to 'own' df [-hi] [path] Display statistics for current directory or filesystem containing 'path' exit Quit sftp get [-P] remote-path [local-path] Download file help Display this help text lcd path Change local directory to 'path' lls [ls-options [path]] Display local directory listing lmkdir path Create local directory ln oldpath newpath Symlink remote file lpwd Display (‘print’) local working directory ls [-1aflnrSt] [path] Display remote directory listing lumask umask Set local umask to 'umask' mkdir path Create remote directory progress Toggle display of progress meter put [-P] local-path [remote-path] Upload file pwd Display (‘print’) remote working directory quit Quit sftp rename oldpath newpath Rename remote file rm path Delete remote file rmdir path Remove remote directory symlink oldpath newpath Symlink remote file version Show SFTP version !command Execute 'command' in local shell ! Escape to local shell ? Synonym for help
We’ll go through some of the more useful commands and how to use them in the following sections.
Navigating with SFTPWe can navigate around the remote server’s file system using commands similar to normal Linux system commands.
First off we can find out where we are in the file system of the remote server. Just like on a normal Linux server we can use the following to get the current directory:
sftp> pwd
Output:
Remote working directory: /
If you are using SFTP with us the home directory for the user will be displayed as / rather than the actual full path. On other servers you would get the output:
/home/sites/11a/7/74089947f7/
As this is the actual home path but when you are using SFTP with our shared or Managed VPS hosting this will be converted such that the user home path becomes just / and any commands should be done relative to this.
We can also view the contents of the current working directory using the ls command:
sftp> ls
Output:
examplefile1.txt examplefile2.php public_html
The commands within the SFTP interface don’t have as many features as in a normal shell interface but they do have some of the more important optional flags:
sftp> ls -la
Output:
drwx--x--- 4 laravetest.com 48 4096 Sep 20 13:33 . drwx--x--- 4 laravetest.com 48 4096 Sep 20 13:33 .. -rw-rw-r-- 1 laravetest.com laravetest.com 959 Mar 8 2021 .bash_history drwxrwxrwx 3 laravetest.com laravetest.com 4096 Mar 8 2021 .composer -rw-rw-rw- 1 laravetest.com laravetest.com 67 May 14 2020 .htaccess -rw-r--r-- 1 laravetest.com laravetest.com 0 Sep 20 13:32 examplefile1.txt -rw-r--r-- 1 laravetest.com laravetest.com 0 Sep 20 13:33 examplefile2.php drwxrwxr-x 4 laravetest.com laravetest.com 4096 Mar 8 2021 public_html
To move between directories in the file system we can use the cd command.
sftp> cd public_html
We can now move around the remote server’s file system. We also need to be able to access and move around our local system as well though. We can run commands on the local file system by amending l to the front of the commands.
The commands we’re shown above have local file system equivalents so you can get the local working directory as follow:
sftp> lpwd
Output:
Local working directory: /home
You can list the contents of the current working directory on the local machine:
sftp> lls
Output:
config.php index.html local var
We can also move between directories on the local system.
sftp> lcd var
Being able to move between directories and see the files on both the remote and local system is good but you’ll also want to be able to move files between the servers.
Transferring Files from the Remote to the Local ServerIn order to download files from the remote server to our local server we can use the get command:
sftp> get examplefile1.txt
Output:
Fetching /examplefile1.txt to examplefile1.txt
By default, the get command downloads a remote file to a file with the same name on the local file system.
You can also copy the remote file to a different name by specifying the name afterwards if you wish:
sftp> get examplefile1.txt localexample.txt
You can also use some option flags with the get command. For example you can copy a directory and all its contents by specifying the recursive option -r:
sftp> get -r public_html/
We can then also tell SFTP to keep the original permissions and access times for the downloaded files using -p.
sftp> get -rp public_html
Moving files from your local server to the remote server it just as easily done but this time you’ll need to use the put command:
sftp> put examplefile1.txt
Output:
Uploading examplefile1.txt to /examplefile1.txt examplefile1.txt
You can use the same flags with the put command as you can with the get command. To copy a local directory therefore, you can do:
sftp> put -r var
A useful tool to use when uploading and downloading files from a remote server is the df command. This works in a similar manner to the command line version. You can use this to check the amount of available on the remote server:
sftp> df -h
Note: here we’ve used the -h flag to make the output more human readable.
Output:
Size Used Avail (root) %Capacity 8.7TB 8.4TB 275GB 275GB 96%
There is no local version of the df command in SFTP so in order to check the storage you will need to exit out of the SFTP connection to run df locally.
You can do this with the ! command. This will take us back to the local server where we can check the disk usage:
sftp> !
$ df -h
Output:
Filesystem Size Used Avail Use% Mounted on devtmpfs 373M 0 373M 0% /dev tmpfs 404M 0 404M 0% /dev/shm tmpfs 404M 46M 358M 12% /run tmpfs 404M 0 404M 0% /sys/fs/cgroup /dev/vda1 25G 1.7G 24G 7% / tmpfs 81M 0 81M 0% /run/user/0
Any other command will also work as expected on the local server. You can then return to your SFTP session with:
$ exit
You should now see your SFTP prompt again.
Basic File ManipulationSFTP will also allow you to do simple file maintenance.
For example, you can change the owner of a file on the remote server by using:
sftp> chown userID file
When using SFTP the chmod command doesn’t accept usernames like with the command line equivalent but instead uses UIDs. There also isn’t an easy way to know the appropriate UID from the SFTP unfortunately.
Depending on your access permissions on the system you’re using, a workaround could be:
sftp> get /etc/passwd sftp> !less passwd
Output:
root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
In the above, instead of using ! on it’s own first we’ve amended it to the beginning of another command. This then runs the given command in the local shell as if we weren’t in the SFTP interface.
This will work for any command available on your local server and we could have used this to run the df command earlier.
The UID we need is in the third column of the output, as displayed by the separating colons.
In a similar fashion we can change the group owner of a file with:
sftp> chgrp groupID remotefilename
Unfortunately again there is no easy way to get a listing of the remote server’s groups. We can work around this using the following:
sftp> get /etc/group sftp> !less group
Output:
root:x:0: bin:x:1: daemon:x:2: sys:x:3: adm:x:4:centos tty:x:5: disk:x:6: lp:x:7: mem:x:8: kmem:x:9: wheel:x:10:admin cdrom:x:11: mail:x:12:
Like before the third column as denoted by the colons holds the UID for the group given in the first column.
Luckily the chmod command works as expected on the remote file system:
sftp> chmod 755 public_html
Output:
Changing mode on /public_html/
There isn’t a command for changing local file permissions but you can set the local umask meaning that any files copied from the remote to the local server will have the correct permissions.
This can be done using the lumask command:
sftp> lumask 022
Local umask: 022
Now any regular files downloaded from the remote server will have 644 permissions. Given that the -p flag is not used.
SFTP allows you to create directories on both local and remote servers using the lmkdir and mkdir respectively. These work as you would expect.
The following only target the remote file system:
sftp> ln sftp> rm sftp> rmdir
These commands have the same behaviour as their equivalent shell versions. If you need to use these commands on the local system then you can return to the local shell with !:
sftp> !
Or you can execute the command from the SFTP interface by amending it with a !:
sftp> !chmod 644 index.php
When you have finished with SFTP you can use exit or bye to close the connection.
sftp> bye
While SFTP is a relatively simple tool, it’s very useful for administering servers and moving files between them.
A working example would be that you can use SFTP to enable particular users to transfer files without SSH access. If you set up additional FTP users in the control panel for the package with us, these can also be used with SFTP to allow multiple user SFTP access to different parts of a hosting package.
If you’re using FTP or SCP for your transfers then SFTP is a good way to have the strengths of both. Whilst SFTP isn’t the best tool for every situation, it is an effective and useful tool.
Can I use sudo on shared hosting?
Sudo is a Linux command that allows a user to run commands as a 'super user', root.
The use of the sudo command is not permitted on our shared hosting platform, and its use will result in an SSH access violation and your SSH access being suspended.
This is noted in the welcome message when connecting to the shared services via an SSH connection...
If the sudo command is used the terminal should close immediately, killing the current session. Attempting to reconnect should result in the terminal closing without warning.
Sudo is not permitted to ensure the security and integrity of our shared hosting platform.
How do I unblock SSH access after using sudo?
If your hosting package's SSH access has been blocked due to the use of sudo, please raise a support ticket with our Support Team to remove this block.
What if I need to use sudo?
If you require the use of the sudo command, you will need a Self-managed VPS rather than shared hosting.