How to Transfer Files Safely Using the SCP Command

SCP (Secure Copy) is a command line utility that allows you to securely copy files and directories between two locations.

With scp you can copy files or directories:

  • From your local system to a remote system.
  • From a remote system to your local system.
  • Between two remote systems from your local system.

When transferring data with scp, files and passwords are encrypted so that no one snooping on the traffic feels anything sensitive.

In this tutorial, we’ll show you how to use the scp command with practical examples and step-by-step explanations of the most common scp options.

SCP command syntax

Before discussing how to use the command scpLet’s start by reviewing the infrastructure.

The scp utility expression takes the following form:

scp [OPTION] [[email protected]]SRC_HOST:]file1 [[email protected]]DEST_HOST:]file2
  • OPTION – scp options like encryption, ssh config, ssh port, limit, recursive..etc
  • [[email protected]]SRC_HOST:]file1 – The source file.
  • [[email protected]]DEST_HOST:]file2 – destination file

Local files must be specified using absolute or relative paths while remote file names must include user and host specifications.

scp provides a number of options that control every aspect of its behavior. The most commonly used options are:

  • -P Specifies the ssh port of the remote host.
  • -p Maintain file modifications and access times.
  • -q Use this option if you want to prevent progress meter and no error messages.
  • -C. This option will force scp to compress the data as it is sent to the destination device.
  • -r This option will tell scp to copy the directory recursively.

before start

ranking scp It relies on ssh to transfer data, so an ssh key or password is required to authenticate to remote systems.

See also  Tutorial on installing and configuring MariaDB on CentOS 7

colon (:) I am offering scp Distinguish between local and remote sites.

To be able to copy files, you must at least see the permissions read (Read) in the source file, as well as the permissions Type (writing) on ​​the target system.

Be careful when copying files of the same name and location on both systems, scp will overwrite the file without warning.

When transferring large files, it is recommended to run the scp command in a screen or tmux session to make it easier to anticipate the unexpected.

Copying files and directories between two systems using SCP

With scp, linux users can easily copy files and directories to remote systems using just the command line. Follow the following guide to copy files and directories using scp

Copy local files to the remote system using the scp Perintah . command

To copy files from a local system to a remote system, run the following command:

scp file.txt [email protected]:/remote/folder

over here, file.txt is the name of the file we want to copy, akun_jauh is the username to login on the remote server, 10.10.0.4 It is the IP address of the server. Guide /remote/folder It is the path to the directory or folder where you want to copy files, if you don’t specify a remote directory, the files will be copied to the remote user’s home directory.

You will be asked to enter the user password and the transfer process will begin.

akun_jauh@10.10.0.4's password: file.txt 100% 0 0.0KB/s 00:00

Ignores the name of the destination file to copy the file with the original name. If you want to save the file with another name, you must specify a new name:

scp file.txt [email protected]:/remote/folder/namabaru.txt

If SSH on the remote host is listening on a port other than the default 22, you can specify the port with arguments -PFor example, the following command will connect to port 2322 because SSH has changed from port 22 to port 2322:

ssh -P 2322 file.txt [email protected]:/remote/folder

The commands to copy directories are the same as the commands to copy files. The only difference is that you need to use the flag -r to repeat.

See also  Brief description of the file Linux / Unix /etc/shadow

To copy a directory from a local system to a remote system, use Options -r:

scp -r /local/directory [email protected]:/remote/folder

Copy the remote files to the local system using the scp Perintah . command

To copy remote files to the local system, use the remote site as the source and the local site as the destination.

For example to copy a file with the name file.txt From a remote server with an IP 10.10.0.4 Run the following command:

scp [email protected]:/remote/folder:/remote/file.txt /local/directory

If you do not set an SSH login without a password (without password) to the remote machine, you will be asked to enter the user password.

Copying files between two remote systems using the scp Perintah الأمر command

Unlike rsync, when using scp, you don’t need to be logged into either server to transfer files from one remote machine to another.

The following command will copy the file /files/file.txtfrom remote hosts host1.com for proof /files on mehost2.com He is a distant host.

scp [email protected]:/files/file.txt [email protected]:/files

You will be asked to enter the passwords for both remote accounts. The data will be transferred directly from one remote host to another.

To route traffic through the device that issues the command, use the options -3:

scp -3 [email protected]:/files/file.txt [email protected]:/files

conclusion

In this tutorial, you learned how to use the scp command to copy files and directories.

You may also want to set up SSH key-based authentication and connect to a Linux server without having to enter a password.

If you regularly connect to the same system, you can simplify your workflow by specifying all your connections in your SSH configuration file.

See also  Learn about Command id in Linux

Source link

About wahyuway

Check Also

How to install and configure Anaconda on CentOS 7

How to install and configure Anaconda on CentOS 7

Anaconda is the most popular machine learning and data science tool used in large scale …

Leave a Reply