Common SCP and sftp operations

Source: Internet
Author: User
Tags ftp file secure copy
Direct File Replication in different regions: SCP

The full name of SCP is secure copy (remote file copy program). This command is included with OpenSSH-clients. It is used to copy data between machines, the transmission between machines is completely encrypted.

The simplest SCP usage is as follows:

[Root @ WWW ~] # SCP [- PR ] [-L rate] File [Account @] HOST: directory name <= Upload [root @ WWW ~] # SCP [- PR ] [-L rate] [account @] HOST: File Directory name <= Download options and parameters: -P: retain the permission data of the original file; - R: when the source is a directory, you can copy the entire directory (including subdirectories) -L: the transmission speed can be limited, in kbits/s, for example, [-l 800 ] Means the transmission speed is limited to 100 Kbytes/ S #  1 . Copy all/etc/hosts * of the Local Machine 127.0 . 0.1  [Root @ WWW ~] # SCP /Etc/hosts * student @ 127.0 . 0.1 :~ Student @ 127.0 . 0.1  '  S password: <= enter the student Password Hosts 100 % 207           0 . 2kb/s 00 : 00  Hosts. Allow  100 % 161           0 . 2kb/s 00 :00  Hosts. Deny  100 % 347           0 . 3kb/s 00 : 00  # Remaining time of file name display progress capacity (bytes) transmission speed # You can take a closer look, there are five fields in the message, meaning as shown above. #  2 . Set 127.0 . 0.1 Copy/etc/bashrc of the remote host to the local host/ Under TMP [root @ WWW ~] # SCP Student @127.0 . 0.1 :/Etc/bashrc/tmp

ActuallyThe colon (:)Hello! The remote host file is connected after the colon. Therefore, if the colon is in front, it indicates downloading from the remote host. If the colon is in front, it indicates uploading local data! If you want to copy the directory, you can add the-r option!


Assume that there is an archive named/root/dd_10mb_file on the local machine. The Archive size is as large as 10 MB. Suppose you want to upload the file to the/tmp bottom of, and you have the right to use the root account on However, because the bandwidth is precious, you only want to pay 100 kbyes/s for this action. How can you issue instructions?


Because this file does not exist by default, we must use dd to create a large file:

Dd If=/Dev/Zero of =/root/dd_10mb_file BS = 1 m COUNT =10

After the setup, because the data is uploaded, in the option of observation-L, the speed is bit, And the bytes to be converted to the capacity needs to be multiplied by 8 times. Therefore, the command should be issued as follows:

SCP-L800/Root/dd_10mb_file root @


Simulate FTP file transmission mode: SFTP

SSH is used to log on to the remote server for work.What if you just want to download or upload files from a remote server? Instead of using SSH, you must use SFTP or SCP.. Both commands use the SSH channel (port 22) to simulate FTP and copy operations. Let's talk about SFTP first. This command is similar to SSH in usage, but SSH is used for login while SFTP is uploading/downloading files.

[Root @ WWW ~] # SFTP student @ localhostconnecting to localhost... student @ localhost'S password: <= enter the password here!SFTP> exit <= The field is waiting for your input.FTPWhere are the related commands!

After entering SFTP, it will be the same as the normal ftp mode operation method! Let's talk about the instructions for using the SFTP interface!

Actions on remote server hosts
Change the directory to/etc/test or another directory. CD/etc/test
CD path
List file names in the current directory Ls
Create directory Mkdir directory
Delete directory Rmdir directory
Display the current directory PWD
Change an archive or directory group Chgrp groupname path
Change the file or directory owner Chown username path
Change the permissions of an archive or directory Chmod 644 path
Among them, 644 is related to permissions! Go back to the basics!
Create a link Ln oldname newname
Delete an archive or directory Rm path
Change the file or directory name Rename oldname newname
Leave remote host Exit (OR) Bye (OR) quit
Act on the client (both L and l are in lower case)
Convert the directory to the path on the local machine LCD path
List the file names in the current local directory Lls
Create a directory on the local machine Lmkdir
Display the current local directory Lpwd
Uploading/downloading data
Upload files from local machine to remote host Put [local directory or file] [remote]
Put [local directory or file]
If this format is used, the file will be placed in the directory of the current remote host!
Download the file back from the remote host Get [remote host directory or file] [Local Machine]
Get [remote host directory or file]
If this format is used, the file will be placed in the directory where the current local machine is located! Wildcard characters can be used, for example:
Get *
Get *. rpm
The format is also acceptable!

On the whole, SFTP in Linux, if you do not consider the graphic interface, then he can replace FTP! Because all functions are covered! Therefore, you can directly turn off the FTP service without considering the graphic interface's FTP software. Instead, you can use sftp-server to provide the FTP service! Pai_^


Assume that localhost is a remote server with student on the server. You want to (1) Upload the/etc/hosts of the local machine to the student home directory, and (2) copy the. bashrc of student to the/tmp of the Local Machine. How can you achieve this through SFTP?


[Root @ WWW ~ ] # SFTP student @ localhostsftp > Lls/etc/hosts <= Check whether this file exists on the local machine. /Etc/ Hostssftp > Put/etc/hosts <= If yes, upload it! Uploading /Etc/hosts to/home/student/ Hosts /Etc/hosts 100 % 243       0 . 2kb/s00 : 00  SFTP > Ls <= Is the upload successful? Check the file name hostssftp in the remote directory. > Ls -A <= Is there any hidden file ?. ... Bash_history. bash_logout.bash_profile. bashrc. Mozilla hostssftt > LCD/tmp <= switch the local directory/ TMP SFTP > Lpwd <= Just confirm it! Local working directory: / Tmpsftp > Get. bashrc <= No problem. download it! Fetching /Home/student/ . Bashrc to. bashrc /Home/student/. bashrc 100 % 124       0 . 1kb/s 00 : 00  SFTP > Lls-A <= Check the local file name. Font -Unix keyring-rNd7qX. X11- Unix... gdm_socket lost + Found scim-panel-socket: 0 -Root. bashrc. Ice -Unix mapping-root. x0- Locksftp > Exit <= leave!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.