In search of a secure alternative to FTP? Or perhaps you wish to learn how to securely transfer files via the command line to your VPS? SCP is a simple (yet effective) option to easily transfer local files to a remote server.
The scp command uses SSH for transferring files and provides the same level of security and benefits as SFTP. However, the two do differ in a few aspects which will be touched upon later in this guide.
How to structure an SCP command
An SCP command is typically structured using three main components.
- Source File (Relative or absolute path)
- Target Destination (Username, IP, and file path)
For purposes of this demonstration, we’re going to leave out the option flag. In this case, we’ll simply provide the source file path (using an absolute or relative path structure), and lastly the destination; Which in this case will be your remote server username, IP, and file path.
Transferring a local file to the remote host
Here’s what we’ll cover in this portion:
- Assure you have a remote server (VPS) deployed
- Create a test file to transfer to the server
- Write an SCP command to securely transfer the local file
- Confirm that the file transfers successfully
Before continuing, you’ll need to assure that you have your remote server information. You can find this by opening up the built-in SSH console on your SkySilk dashboard, or through whichever third-party SSH terminal you may be using for connection. If you don’t already have a remote server you can create one in a few steps using the button below.
Set up a test file and directory
Once you have the necessary information, we can transfer our file in a simple command. For this guide, we’ve set up a directory named “files” on the desktop on a local machine. A file named “transferme.txt” has been included in the files directory which we will use to transfer over.
Keep in mind that you will not use the same IP as the one pictured, but rather this will be the IP of your own virtual server. This server has been promptly removed after publication of this article.
To briefly explain this command; “scp” is the initial secure copy command. Following is the name of the dummy file we’ve created to test that transfers are working. Next is the username of the remote server you’re using (In this case, we’ve created a sudo user named “user” as to avoid working in root), followed by the IP address of the remote server.
Additionally, we’ve added the directory in which we want to transfer the file. In this case, we’re using “:~” to indicate the root directory. Now hit ENTER.
It will prompt you for the user password at this point. This password is something that you set upon creation of the new user via the command line. Simply provide a password and you should see a success message that the file was transferred without any errors.
Confirm the scp transfer
To confirm that the file was indeed transferred, we can check this on our remote server via SkySilk’s built-in SSH console or any third-party connection terminal (i.e. PuTTY). Simply type “cd” to enter the root directory if you aren’t already there. Once in root (denoted by :~), simply run the ls command to list the files and directories.
And voila! There’s our transferme.txt file which was sent from our local system. While you can achieve the same results through FTP or even cloning a GitHub repository, SCP provides a completely secure method/alternative to FTP.
Transferring a file from your remote server to a local machine
This command should be quite self-explanatory given that it’s essentially the opposite of the previous.
To transfer a file from your local server, we can remain in our local terminal and simply provide the user and remote server IP before the target destination. However, this time around, our target destination will be (yes, you guessed it), the path on our local system where we’d like to transfer the file.
Creating a test file to transfer from the remote server
To test that this works properly, simply create another test .txt file on your server using the touch command. In this case we’ve named the test file “transfertolocal.txt”.
The command we need to run in order to transfer the file from your remote server to a local directory is as follows:
Notice that this command uses the relative file paths for both the remote and local directories. You’ll once again receive a prompt for the user password on the remote host. Enter your password, hit ENTER, and once again we see that everything went successfully as planned.
To confirm receipt of the file on the local system we can once again use the “ls” command within the directory used for transfer.
It looks as though everything worked perfectly!
Is there another alternative to FTP that’s safe and secure?
Of course. The one most closely related to SCP is SFTP. Or, in other words, Secure File Transfer Protocol. The two methods both use an SSH protocol when transferring files and serve as a secure alternative to FTP. However, there is one large difference between SCP and SFTP.
You are able to use SCP to transfer files. However, SCP is unable to remove files or list any remote directories. SFTP on the other hand, can provide all of the actions listed above and is essentially identical to FTP but uses the SSH protocol.