
scp stands for Secure Copy Protocol. The scp command in Linux lets you securely transfer files between hosts using an encrypted SSH (Secure Shell) connection. Unlike regular copy commands such as cp, scp also ensures data integrity during transfers.
This article explains how to use the scp command to effectively transfer files and directories in Linux.
Before you begin:
limited user login option to use as your local and remote workstations respectively.scp Command SyntaxThe following is a basic scp command syntax:
In the above scp command:
[options]: Accepts optional flags to modify the command's behavior[source]: Specifies the file or directory to copy.[destination]: Sets the target location where you want to place the files.scp Command OptionsThe following are the most common scp command options:
Follow the steps below to create sample directories and files to test the scp command in Linux.
Create three sample .txt files on your local server.
Create a sample dir directory on your local server.
Create three sample .txt files on the remote server.
Create dir1 and dir2 sample directories on the remote server.
Follow the steps below to transfer files and directories using the scp command in Linux.
Copy the file1.txt file from your local server to the remote server.
The above command securely copies file1.txt from your local server to the remote server user's home directory. By default, the SCP command copies files to the user's home directory unless you specify another directory.
Output:
Enter yes to accept the remote server's fingerprint key when prompted.
Copy file4.txt from the remote server to your local server.
Output:
Copy the dir directory from the local server to the remote server using the recursive -r option.
The above command uses the -r option to recursively copy the dir directory from the local server to the linuxuser2 home directory on your remote server including all files and subdirectories.
Output:
Copy the dir1 directory from the remote server to your local server user's home directory.
Output:
You can explore the find command in Linux to quickly locate files on your system.
The SCP command uses password authentication by default to copy and transfer files. SSH key-based authentication enables passwordless authentication when transferring files using the SCP command. Follow the steps below to generate a new SSH key to use with the SCP command.
Generate a new SSH key pair on your local server.
Press Enter to accept the default private key file location and set an optional passphrase when prompted.
Output:
Copy your public key to the remote server.
Enter your remote user password when prompted to copy the public key.
Output:
Access the remote server using SSH and verify that you're not prompted to enter a password.
Output:
Follow the steps below to transfer files using the scp command with SSH key authentication.
Copy file2.txt from your local server to the dir2 directory on the remote server.
The above command securely copies the file2.txt file from your local server to dir2 on the remote server.
Output:
Copy the dir2 directory from the remote server to your local server.
Output:
Copy file3.txt to the dir2 directory on the remote server and preserve the original file attributes using the -p option.
The above command uses the -p option to copy file3.txt to the dir2 directory while keeping the original file attributes including the original timestamps and file permissions.
Output:
Copy file1.txt to the remote server and enable compression using the -C option.
The above command copies file1.txt to the dir2 directory and uses the -C option to compress data during the transfer to speed up the process.
Output:
Copy file2.txt and limit the bandwidth in kilobits per second (Kbps) using the -l option.
The above command uses the -l option to limit the transfer speed to 500 Kbps and avoid overusing network resources while transferring the file to the remote server.
Output:
Display detailed debugging messages in verbose mode using the -v option.
Output:
Specify a private key for authentication using the -i option. Replace /path/to/private_key with your actual private key path.
The above command uses the -i option to specify a private key file for SSH authentication. This is useful when using multiple SSH keys and requires a specific key to establish a connection.
Output:
Run the following command in your user home directory to view the default SSH private key location.
Output:
Copy file3.txt to the /dir2 remote server directory using a specific SSH port to transfer the files.
The above command uses the -P option to transfer files using the default SSH port 22. This is important when the SSH daemon runs on a custom port on the remote server.
Output:
Transfer archived files using tar and the scp commands. For example, compress the dir directory and transfer the archived file to the remote server.
The above command compresses the dir directory and copies the data over SSH to the remote server. The tar xzf - -C command extracts the files into the dir1 directory in the user home directory to enable advanced compression and efficient file transfers.
Output:
You have used the scp command in Linux, its syntax, options, and practical examples. Additionally, you've set up SSH key-based authentication, allowing you to transfer files and directories more efficiently without a password. The scp command allows you to securely and efficiently transfer files between hosts by combining various command options in Linux.
0 Comments
Be the first to comment and share your perspective with the community.