Once your VPS service is configured, you will likely need to transfer files, such as your website files still stored on your localhost. To do this, you can use SCP to transfer files easily and securely.
In this tutorial, we will cover the basic ways to use SCP to transfer files from your localhost to a server and vice versa, move multiple files at once, and use SCP with servers that require a custom port.
How to Use SCP for File Transfers
Before starting the tutorial, please note that when using this feature, you will be asked for your SSH password (if your SSH uses password authentication rather than a key). Additionally, here are the basic commands for using SCP
scp [opsi] [username@IP atau disk sumber]:/[folder dan nama file sumber] [username tujuan@IP atau disk tujuan]:/[folder tujuan]With that command, here is a brief explanation
- [opsi] this command is used if you have specific requirements, such as if the destination server uses a custom SSH port, you are using a recursive command, or other needs
- [username@IP atau disk sumber] this option is used to specify the location of the file and folder on the device you are using. For example, if you are using Windows and the file is in the /scp/ folder on your D drive, you can use the D:\ option directly
- [folder dan nama file sumber] here you can enter the path of the file you want to copy to another device, for example /SCP/documents.txt
- [username tujuan@IP atau disk tujuan] unlike the source disk, here you need to specify the SSH login details or, if on localhost, you can use the disk option
- [folder tujuan] here you can write the location where you will save the file on the new device, for example in the /var/www/html/images/ folder
With those basics, you should be able to use the standard SCP commands. If you are still unsure, let us try the most frequently used SCP commands one by one.
1. Transferring Files from Windows Localhost to a Linux Server
For example, if you want to transfer a file from the folder D:/SCP/gambarku.png to the folder /var/www/html/images/ on a server with the IP 192.168.1.100 and the root user, you can use the following command
scp D:/SCP/gambarku.png [email protected]:/var/www/html/images/Very easy, right? But what if you want to transfer files from a server to a Windows localhost?
2. Transferring Files from a Linux Server to Windows Localhost
Actually, to transfer files from a server to your localhost, you just need to reverse the paths in the command. For example, using the same file from the first option, if you want to download it to your localhost in the D:/Download/ folder, you just need to change the command to this
scp [email protected]:/var/www/html/images/gambarku.png D:/Download/Those are two easy ways to move files between your localhost and a VPS. However, these methods only work if your VPS uses the default SSH port. So, what if your server uses a custom SSH port?
3. Using SCP to Transfer Files with a Specific Port
If your server uses a specific port, for example port 1945, you just need to add the flag -P (capitalized) after [username tujuan@IP atau disk tujuan]: so the result will look like this
scp -P 1945 D:/SCP/gambarku.png [email protected]:/var/www/html/images/If you want to transfer from the server to your localhost, you just need to reverse the command
scp -P 1945 [email protected]:/var/www/html/images/gambarku.png D:/Downloads/How is that, easy right? But what if the file or folder name contains spaces? Adding a space in SCP usually means adding a new option.
4. Transferring Files or Folders with Spaces in Their Names
If your file or folder name contains spaces, for example D:/SCP/ini gambarku.png you can add single quotes around the file path. For example
scp -P 1945 "D:/SCP/ini gambarku.png" "[email protected]:/var/www/html/media file/"To transfer files from the server to your localhost, you just need to reverse the command
scp -P 1945 "[email protected]:/var/www/html/media file/ini gambarku.png" "D:/Downloads Gambar/"But of course, the four methods above are only effective if you are transferring a few files. What if you want to transfer many files and folders at once?
In this case , you can use the recursive option to transfer many files and folders with a single command
5. Transferring Files Recursively
You can use this method to transfer many files and folders directly from your localhost to a server, or vice versa.
You just need to add the flag -r to transfer files and folders recursively, and you do not need to add the file name, for example with the following command
scp -r -P 22 D:/SCP/ [email protected]:/var/www/html/images/With that command, you can transfer the files and folders inside /SCP/ without needing to do them one by one.
6. Transferring Files Between Servers
What if you want to transfer files between servers? For example, from server 192.168.1.100 to 192.168.1.99, you can use the following command:
scp -r -P 22 [email protected]:/var/www/html/ [email protected]:/var/www/html/Of course, all the previous methods only work if your server uses a password for authentication. If your server only supports SSH Keys, you need to use a slightly different command.
7. Transferring Files Using SCP with an SSH Key
To do this, make sure you have uploaded the public key to the server and saved the corresponding private key on your localhost. For example, if the private key is saved in the C:/Users/NamaPengguna/.ssh/privatekey.ppk folder (if you create an SSH Key using PuTTYgen, you will usually get a private key in .ppk format), you can use the following command
scp -i 'C:/Users/NamaPengguna/.ssh/privatekey.ppk' D:/SCP/gambarku.png [email protected]:/var/www/html/images/Those are several ways to use SCP to transfer files easily according to your needs. If you have other conditions, you can check the available SCP options by running the command scp help.
COMMENTS
Loading...