How to securely transfer files via ssh using scp command under Linux
On Unix or Linux operating systems, the scp utility (secure copy) is similar to the more famous command cp, but is used to copy between hosts over a secure and encrypted network. Transfer files and directories between
Since it relies on ssh for data transfer, it provides the same security and uses the same authentication as ssh. Unlike rcp, the scp command will prompt you for a password for authentication if required.
In this article, we will delve into secure file transfer in Linux and learn how to use the scp command. With detailed explanations and example use cases of common scp switches and options, you'll learn how to use this utility.
It’s important to know the following before you start
- Since scp relies on ssh for data transfer, an ssh key or password is required to authenticate on the remote system (Linux file transfer ssh).
- Confirm that you have write permissions on the target system and read permissions on the source file.
- Note: The scp tool uses a colon (:) to differentiate between remote and local locations.
- When copying a file with the same location and name on both sides, scp will overwrite the file without prior warning.
SCP Linux command syntax
In order to start using the scp command, you need to understand its basic syntax, which is in the following format:
less复制代码 scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2
Options: Represents scp options such as ssh configuration, password, restrictions, ssh port, recursive copy, etc.
[user@]SRC_HOST:]file1: Indicates the source file.
[user@]DEST_HOST:]file2: represents the target file
Like any other command, the scp tool has some options for controlling its behavior. The most common are the following:
- -P: Indicates the ssh port of the remote host to connect to.
- -p: Specifies to preserve file modifications, mode and access time from the original file.
- -q: Need to suppress non-error messages and progress meters.
- -C: Force scp to compress data when passing the -C flag to ssh to enable compression on encrypted secure connections.
- -r: Tells the scp command to copy the entire directory recursively.
- -S: The name of the program used to encrypt the connection. Since ssh is used, the program must understand the ssh switch.
- -v: Verbose mode. Enables scp and ssh to output messages about their progress. This is useful when debugging connection, configuration and authentication issues.
Transfer local files to remote destination
Before we start using scp, let us use cp to copy files from one folder to another. We all know how to use the cp command to achieve this:
bash复制代码 cp /home/linuxmi/linuxmi.mp3 /home/linuxmi/audio
It copies the file linuxmi.mp3 in the home directory of user linuxmi to the home folder of the same user, which is the audio directory of linuxmi.
In a similar manner, an example of the scp command that can be used is as follows:
typescript复制代码 scp /home/linuxmi/linuxmi.mp3 linuxmi@linuxmi.com:/home/linuxmi/audio
Use linuxmi as the login name to upload the linuxmi.mp3 file to the server linuxmicom to the remote directory /home/linuxmi/audio���. Please note that scp will prompt you for the remote password for linuxmi before starting the upload. If no remote directory is provided, the files will be copied to the remote user's home directory. If you don't provide a filename on the destination folder, scp will be made to copy the file with the original name. If you want to save the file under a different name, you need to specify a new file name.
Transfer remote files to local target
In order to download the file, you can mention a remote location as the source location. For example :
scp linuxmi@linuxmi.com:/home/linuxmi/audio/* /home/linuxmi/downloads
It will download all audio files in the remote directory /home/linuxmi/audio on linuxmi.com to the local directory /home/linuxmi/downloads.
Note the file name:
The file name can be composed of user and host to indicate the transfer of the file to or from that host. To avoid scp processing filenames containing ":" as a host specifier, local filenames can be either absolute or relative. However, the remote filename should include both user and host.
Default port number:
If on the remote host, SSH is listening on a different port (i.e. a port other than the default port 22), you can use the -P switch to indicate that port.
Transfer local directory to remote target
Copy a directory to a remote location, similar to copying files. You just need to use the -r flag for recursive transfers.
To copy the entire directory to the remote system, run the command with the -r option, for example:
scp -r /home/linuxmi/local_folder linuxmi@linuxmi.com:/home/linuxmi/remote_folder
File transfer between two remote systems
The following command will copy the file /folder/linuxmi.mp3 from the remote host linuxmi.com to the directory /folder on the remote host www.linuxmi.com.
scp linuxmi@linuxmi.com:/folder/linuxmi.mp3 linuxmi@www.linuxmi.com:/anotherfolder
After initiating this operation, you will be asked to provide the passwords for both remote targets.
In order to route traffic through the local host (i.e. the computer on which the command is executed), use the -3 switch as follows:
scp -3 linuxmi@linuxmi.com:/folder/linuxmi.mp3 linuxmi@www.linuxmi.com:/anotherfolder
The above is the detailed content of How to securely transfer files via ssh using scp command under Linux. For more information, please follow other related articles on the PHP Chinese website!

The main tasks of Linux system administrators include system monitoring and performance tuning, user management, software package management, security management and backup, troubleshooting and resolution, performance optimization and best practices. 1. Use top, htop and other tools to monitor system performance and tune it. 2. Manage user accounts and permissions through useradd commands and other commands. 3. Use apt and yum to manage software packages to ensure system updates and security. 4. Configure a firewall, monitor logs, and perform data backup to ensure system security. 5. Troubleshoot and resolve through log analysis and tool use. 6. Optimize kernel parameters and application configuration, and follow best practices to improve system performance and stability.

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy

The average annual salary of Linux administrators is $75,000 to $95,000 in the United States and €40,000 to €60,000 in Europe. To increase salary, you can: 1. Continuously learn new technologies, such as cloud computing and container technology; 2. Accumulate project experience and establish Portfolio; 3. Establish a professional network and expand your network.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.

The core of the Linux operating system is its command line interface, which can perform various operations through the command line. 1. File and directory operations use ls, cd, mkdir, rm and other commands to manage files and directories. 2. User and permission management ensures system security and resource allocation through useradd, passwd, chmod and other commands. 3. Process management uses ps, kill and other commands to monitor and control system processes. 4. Network operations include ping, ifconfig, ssh and other commands to configure and manage network connections. 5. System monitoring and maintenance use commands such as top, df, du to understand the system's operating status and resource usage.

Introduction Linux is a powerful operating system favored by developers, system administrators, and power users due to its flexibility and efficiency. However, frequently using long and complex commands can be tedious and er

Linux is suitable for servers, development environments, and embedded systems. 1. As a server operating system, Linux is stable and efficient, and is often used to deploy high-concurrency applications. 2. As a development environment, Linux provides efficient command line tools and package management systems to improve development efficiency. 3. In embedded systems, Linux is lightweight and customizable, suitable for environments with limited resources.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools