What is SFTP?
Before understanding SFTP, let's look at what is FTP. FTP (File Transfer Protocol) file transfer protocol, is coming between two end systems a common method of transferring files.
SFTP, namely SSH File Transfer Protocol (SSH File Transfer Protocol), or a secure file transfer protocol (Secure File Transfer Protocol). SFTP is a separate SSH packet encapsulation protocol, connected in a similar manner by security. Its advantage is that it can use a secure connection to transfer files, but also through the file system on the local and remote systems.
In most cases, preference SFTP instead of FTP, SFTP most basic reason is that the security features and the ability to use SSH connection. FTP is an insecure protocol, you should only trust networks or use under certain circumstances.
Although SFTP integrates many graphics tools, but this is a use guide will demonstrate how to use an interactive command line interface to use it. The following is a guide.
How to use SFTP connections
By default, SFTP using SSH protocol for authentication and establish a secure connection. For this reason, in the same present in the SSH authentication method is available.
Although by default the way, is very convenient to use and set up a password, we strongly recommend that you build a SSH key to your public key pass you need access to all systems. In the long run, it will be more secure, but also save your time.
If you have not set up SSH key, click here to see the SSH key guide to set up your SSH key to access your server.
If you can use SSH to connect to your host, then you have completed all the necessary requirements for use SFTP to manage your files up. Use the following command to test SSH access:
ssh username @ remote_hostname_or_IP
If the previous command is successful, enter the command exit:
Establish an SSH connection open an SFTP session. This process can use the following command:
sftp username @ remote_hostname_or_IP
This way, you connect to a remote system, you will become SFTP prompt prompt.
In SFTP command line help documentation
The old rules, to understand the most useful commands - help, it will help give you an overview of SFTP. You can enter any of the following to open the help of the two:
After that, you can use the following command will display a list:
bye Quit sftp
cd path Change remote directory to 'path'
chgrp grp path Change group of file 'path' to 'grp'
chmod mode path Change permissions of file 'path' to 'mode'
chown own path Change owner of file 'path' to 'own'
df [-hi] [path] Display statistics for current directory or
filesystem containing 'path'
exit Quit sftp
get [-Ppr] remote [local] Download file
help Display this help text
lcd path Change local directory to 'path'
Next, we'll explore some of the above commands.
SFTP Shell Basic Operations
We can use most similar to the machine Shell commands to navigate through the directory structure.
First, we have to determine the current working directory, how to determine it? Shell sessions with most similar can enter the following command to get the current directory information:
Remote working directory: / home / demouser
Another familiar command - Content ls, view the current directory
Summary.txt info.txt temp.txt testDirectory
It is noteworthy that, SFTP command line does not like such ships Shell Bash feature-rich, but the selectivity achieved some important parameters:
drwxr-xr-x 5 demouser demouser 4096 Aug 13 15:11.
drwxr-xr-x 3 root root 4096 Aug 13 15:02 ..
-rw ------- 1 demouser demouser 5 Aug 13 15:04 .bash_history
-rw-r - r-- 1 demouser demouser 220 Aug 13 15:02 .bash_logout
-rw-r - r-- 1 demouser demouser 3486 Aug 13 15:02 .bashrc
drwx ------ 2 demouser demouser 4096 Aug 13 15:04 .cache
-rw-r - r-- 1 demouser demouser 675 Aug 13 15:02 .profile
Use the following command to switch to another directory:
Now we can traverse the remote file system! But if we need to access the local file system, how to do it? We just need to give these commands just add a prefix "l" (L lowercase), the operation can be realized on the local host.
So far, all we talked about the command has a corresponding local version, we can print the local host working directory:
Local working directory: / Users / demouser
Similarly, we can list the contents of the machine so that the current working directory:
Desktop local.txt test.html
Documents analysis.rtf zebra.html
Obviously, we also can easily switch back and forth in a Shell in the current working directory on the local host and the remote host:
Transfer files using SFTP
Only access the content between the two file systems can not transfer files between local and remote file systems, it is simply a cow tripod cooking chicken.
Download the remote file to the local host
If we want to download a file from a remote host, follow me do so:
Fetching / home / demouser / remoteFile to remote_file_name
/ Home / demouser / remote_file_name 100% 37KB 36.8KB / s 00:01
As you can see, by default, "get" command to download the file on the remote host to the local host and ensure that the same file name.
I want to change the name? no problem! Directly specify a different file name to copy the file to the remote:
get remote_file_name local_file_name
Here, I have to tell you, "get" command and some optional parameters. For example, we can open recursive option to recursively copy a folder inside the content:
get -r some_directory_name
We can also open the "-P" or "-p" parameter to tell SFTP maintaining access to the settings and bit file access time:
get -Pr some_directory_name
Upload a file to the remote host
Use see-known name meaning "put" command can easily upload files to a remote host:
Uploading localFile to / home / demouser / local_file_name
local_file_name 100% 7607 7.4KB / s 00:00
"Put" similar "get" parameter. In this way, you can upload an entire folder:
put -r local_directory_name
In the current release of Ubuntu system (or at least 14.04-15.10) running on a version of OpenSSH has a BUG, execute the above command error. When use of the BUG OpenSSH, in the implementation of the above command to transfer files to the server, will be reported this error: Could not canonicalise: No such file or directory
To solve this problem, you can enter mkdir local_directory_name on the remote system to the new destination directory. After this, execute the above command should not being given.
When we use to download and upload files to a well-known and useful tool - "df" command, and other command-line work in the same way. Through it, you can check whether there is enough space to complete the file you want to transfer:
Size Used Avail (root)% Capacity
19.9GB 1016MB 17.9GB 18.9GB 4%
Please note! This command has no corresponding ldf, but we can use the "!" The command to achieve the same purpose. (Here, I think LinuxStory article "at the Linux command line!" "Ten magic usage," is very good, it is recommended to not see a small partner, click here to get.)
This "!" (Exclamation-byte) command takes us back to the local Shell, where you can run all the commands available in our local system. By entering the following command to check our disk usage:
Filesystem Size Used Avail Capacity Mounted on
/ Dev / disk0s2 595Gi 52Gi 544Gi 9% /
devfs 181Ki 181Ki 0Bi 100% / dev
map -hosts 0Bi 0Bi 0Bi 100% / net
map auto_home 0Bi 0Bi 0Bi 100% / home
As such, any other local commands are as effective as you would expect. Back SFTP session, enter:
Now, you should be able to see SFTP prompt back.
In fact, you can not quit SFTP Shell command to execute the local host, if you execute the Shell command VIM inside too, I think you should know what that means.
Simple to use SFTP file operations
SFTP allows you to perform basic file maintenance type, there is useful when the file system.
For example, you can use the following command to change the owner of the file on the remote system:
chown userID file
Please note that the system and "chmod" command, this command does not accept SFTP user name and replace it with the user's UID. Unfortunately, there is no easy way to give the corresponding UID via SFTP interface.
This thorny problem solve can use this command:
get / etc / passwd
! Less passwd
root: x: 0: 0: root: / root: / bin / bash
daemon: x: 1: 1: daemon: / usr / sbin: / bin / sh
bin: x: 2: 2: bin: / bin: / bin / sh
sys: x: 3: 3: sys: / dev: / bin / sh
sync: x: 4: 65534: sync: / bin: / bin / sync
games: x: 5: 60: games: / usr / games: / bin / sh
man: x: 6: 12: man: / var / cache / man: / bin / sh
Note that the "!" Instead of "l", and before this "l" prefix is not the same, we can use it as in the same manner inside VIM local Shell command. Before it has been used on the local "df" command.
UID file will appear in the third column, defined by a colon.
Similarly, we can change the file belongs to the group:
chgrp groupID file
Again, I want to get the remote system GID is not so simple. We can use the same way to solve:
get / etc / group
! Less group
root: x: 0:
daemon: x: 1:
bin: x: 2:
sys: x: 3:
adm: x: 4:
tty: x: 5:
disk: x: 6:
lp: x: 7:
Clearly we can see the third column is the first column GID corresponding to the user's group. This is what we are looking for!
Fortunately, the "chmod" command can execute such commands on a remote file system:
chmod 777 publicFile
Changing mode on / home / demouser / publicFile
Although there is no ready-made commands to operate the local file permissions, but you can set the local umask. Thus, any files copied to the local system has the appropriate permissions.
Use "lumask" command to complete this operation:
Local umask: 022
Now all regular files download (as long as no open "-p" parameter) will have 644 permissions.
SFTP allows you to create a new directory on the local file systems and remote file systems, respectively, using "lmkdir" and "mkdir" command, this is definitely helpful!
This is the only remaining file commands for the remote file system:
These commands copy version of the basic characteristics of Shell. If you need to perform these operations on the local file system, remember that you can enter this command to switch to Shell:
Or perform in a local system plus this command prefix, such as "!":
! Chmod 644 somefile
When you have completed the SFTP session, use the "exit" or "bye" to close the connection:
to sum up
Although SFTP is a very simple tool, but use it to manage the server and between the client and server to transfer files it is very convenient.
If you are accustomed to using FTP or SCP to achieve your transmission, then you can try SFTP, which combines two of the director. Although it is not all as you like in all cases, but it is a flexible tool worth adding to your skills library!