Transferring files to a remote server is a fundamental skill for anyone managing a website, working with cloud services, or collaborating on projects. There are many ways to transfer files to a remote server: SCP (Secure Copy Protocol), SFTP (SSH File Transfer Protocol), rsync, FTP (File Transfer Protocol), Using GUI Tools
But in his article we will be discussing how to use the scp
command to transfer a file to a remote server and we will elevate the process by creating a simple Bash script to make it more efficient and hands-free.
What is the scp
command?
SCP which stands for Secure Copy Protocol is a protocol which ensures secure file transfers by leveraging the SSH (Secure Shell) protocol for both authentication and encryption. It provides a means to effortlessly copy files between your local machine and a distant server, ensuring a robust and protected transfer process.
How the scp
command works
The scp
command, an abbreviation for secure copy, functions similarly to the cp
command. However, it distinguishes itself by ensuring not only secure but also remote file copying.
The scp
command facilitates the secure exchange of files across networked computers by leveraging the SSH (Secure Shell) protocol. Operating at the same level of security as the ssh
command, scp
ensures the confidentiality and integrity of transferred files.
When you initiate the scp command, it initiates a secure connection with the designated remote server using the SSH protocol. This secured link establishes a protected pathway through which the specified files are then seamlessly transmitted over the network, ultimately reaching the predefined destination on the remote server. The use of the SSH protocol ensures that the file transfer process is not only efficient but also robustly shielded against potential security threats.
Syntax
scp [options] [source] [destination]
Keywords meaning
The term
scp
represents the command employed for file transfer in this context.The term
[options]
alludes to customizable settings that can be incorporated to alter how thescp
command operates. This aspect is optional, offering users the flexibility to tailor the behavior of thescp
command according to their specific requirements. Numerous options are available for thescp
command, enabling users to make adjustments (e.g -P, -r, -v, -i, -c, -B, etc)[source]
pertains to a file located either locally on your machine or on a remote server.[destination]
signifies either a directory located locally on your machine or a directory situated on a remote server.
Transferring a file to a remote server
Let's take for example we are transferring a file named example.txt
from our local machine to a remote using scp
, our command should look like this:
scp example.txt user_name@remote_server_ip:/path_to_destination
Replace use_name
with the username you used for your remote server, remote_server_ip
with the the IP adddress of the remote server and path_to_destination
with path to the directory on the remote server you want to store the file.
Copying a file from the remote server to your local machine
To retrieve a file from a remote server to your local machine, you should follow this syntax:
scp user_name@remote_server_ip:path_to_remote_file /path_to_local_destination
In this case, things are a bit different from before. Now, your remote server is the one giving the file (source), whereas in the previous situation, it was the one receiving the file (destination).
By default, scp employs the same methods for authentication as ssh, including options like password-based authentication and public key authentication. Therefore, to use the scp command, it's necessary to have ssh access to the remote server
How to automate with bash script
To write a bash script to help us automate the process of transferring files from a local machine to a remote server we will take note of 4 things.
Which are the four arguments the script will take in:
Path to the file to be transferred: The location of the file to transfer on your local machine.
The IP address of the remote server you want to send the file to.
Username
scp
connects with: This is typically the username on the remote server. Like in my case it'subuntu
.The path to the
ssh
private key: This is the path to the private keyscp
uses for authentication when connecting to the remote server.
The last part is not necessary if you have already added your
ssh
public key to theauthorized_keys
file on the remote server
As the final argument is optional, our Bash script must incorporate conditional statements to ascertain its provision. Additionally, we will validate whether the number of provided arguments is less than three. If the count falls below three, we cannot proceed with the execution, prompting us to notify the user with an error message.
The script should look like this:
#!/usr/bin/env bash
if [ "$#" -lt 3 ]; then
echo "Wrong usage. This is the right pattern to follow: ./scriptname PATH_TO_FILE IP USER_NAME PATH_TO_SSH_KEY"
elif [ "$#" -lt 4 ]; then
scp "$1" "$3@$2":~/
else
scp -i "$4" "$1" "$3@$2":~/
fi
This script serves as a straightforward illustration of integrating the scp
command into a Bash script. With just one command, you can efficiently transfer a file to a remote server. Additionally, it offers the flexibility to specify a private key path if required.
Since this is a bash script you use it by first making the file executable using chmod u+x file_name
. Then running it using ./file_name
Explanation to bash script:
#!/usr/bin/env bash
: This line, known as the shebang, instructs the operating system to execute the script by invoking the Bash interpreter.if [ "$#" -lt 3 ]; then
: This is an if condition assessing whether the count of command-line arguments provided to the script is below 3. In Bash, the special variable$#
holds the number of arguments received by the script. The test[ "$#" -lt 3 ]
evaluates to true if the argument count is less than 3.echo "Wrong usage. This is the right pattern to follow: ./scriptname PATH_TO_FILE IP USER_NAME PATH_TO_SSH_KEY"
: If the count of arguments is less than 3, the script will present a message guiding users on the correct usage of the script.elif [ "$#" -lt 4 ]; then
: Thiselif
statement is an additional conditional check, verifying whether the number of command-line arguments provided to the script is less than four. As part of theelif
(else if) construct, it is executed only if the precedingif
statement evaluates to false.scp "$1" "$3@$2":~/
: In case the script receives 4 arguments, this line will employ thescp
command with the-i
option to indicate the path to the private key file, as denoted by the fourth argument,$4
. The command transfers the file specified by the first argument,$1
, to the remote server identified by the second argument,$2
, and the user mentioned in the third argument,$3
. The:~/
appended at the end of the command designates that the file should be copied to the home directory of the remote user.fi
: This line marks the conclusion of theif-else
statement block.
Conclusion
I hope you find this post useful and learn something new in the process. If you have any questions or suggestions let me know in the comment section.
Feedback will help me a lot and motivate me to share more content like this in the future.
If you have read this far, Thank you very much!
Connect with me on Twitter | LinkedIn | Github
Happy Coding!