221 lines
8.9 KiB
Markdown
221 lines
8.9 KiB
Markdown
|
|
|||
|
SFTP stands for Secure File Transfer Protocol.
|
|||
|
|
|||
|
![[Pasted image 20230810210354.png]]
|
|||
|
File transfer protocols allow users to transfer data between remote systems over the Internet. SFTP is one such protocol, offering users a secure way to send and receive files and folders.
|
|||
|
## How to Use SFTP?
|
|||
|
|
|||
|
Using SFTP requires setting up a connection between the client system and the SFTP server using SSH. A commonly used option is to [connect the two systems using an SSH public key](https://phoenixnap.com/kb/ssh-with-key).
|
|||
|
|
|||
|
Since SFTP comes as a standard part of SSH, no additional installation is necessary. To start using the SFTP interface to transfer files, [connect to the remote server](https://phoenixnap.com/kb/ssh-to-connect-to-remote-server-linux-or-windows) by providing a username and remote hostname or IP address:
|
|||
|
|
|||
|
```
|
|||
|
sftp [username]@[remote hostname or IP address]
|
|||
|
```
|
|||
|
|
|||
|
In the example below, we access the _192.168.100.5_ IP address using the _phoenixnap_ username:
|
|||
|
|
|||
|
```
|
|||
|
sftp phoenixnap@192.168.100.5
|
|||
|
```
|
|||
|
|
|||
|
![An example of the SFTP interface](https://phoenixnap.com/kb/wp-content/uploads/2021/11/what-is-sftp-02-connect.png)
|
|||
|
|
|||
|
The SFTP interface, indicated by **`sftp>`**, allows you to request file transfers and execute [SFTP commands](https://phoenixnap.com/kb/sftp-commands).
|
|||
|
|
|||
|
Once you are done using SFTP, end the current connection with:
|
|||
|
|
|||
|
```
|
|||
|
exit
|
|||
|
```
|
|||
|
|
|||
|
Conclusion
|
|||
|
|
|||
|
SFTP offers a reliable way to transfer files and directories, with the added security of SSH encryption. To learn, read our article on [how to use SFTP to transfer files](https://phoenixnap.com/kb/how-to-use-sftp).
|
|||
|
|
|||
|
|
|||
|
## How to Transfer Files Using SFTP
|
|||
|
|
|||
|
Using SFTP allows you to transfer files from a remote server to a local system and vice versa.
|
|||
|
|
|||
|
### Transfer Remote Files to a Local System
|
|||
|
|
|||
|
Use the **`get`** command in the SFTP interface to transfer a file from a remote server to your local system:
|
|||
|
|
|||
|
```
|
|||
|
get [path to file]
|
|||
|
```
|
|||
|
|
|||
|
For example, to transfer a file called _example_document.txt_ from the remote system's _Home_ directory to the local system, use:
|
|||
|
|
|||
|
```
|
|||
|
get example_document.txt
|
|||
|
```
|
|||
|
|
|||
|
![Transferring a file from the remote server to the local system](https://phoenixnap.com/kb/wp-content/uploads/2021/11/sftp-transfer-files-02-copy-file-remote-to-local.png)
|
|||
|
|
|||
|
By default, SFTP transfers files to the local system's _Home_ directory. To transfer files to a different directory, append the path to the directory to the end of the **`get`** command:
|
|||
|
|
|||
|
```
|
|||
|
get example_document.txt Downloads
|
|||
|
```
|
|||
|
|
|||
|
![Transferring a file from the remote server to a specific directory on the local system](https://phoenixnap.com/kb/wp-content/uploads/2021/11/sftp-transfer-files-03-copy-file-remote-to-local-change-destination.png)
|
|||
|
|
|||
|
To change the filename on the local system, append the new file name to the end of the **`get`** command.
|
|||
|
|
|||
|
```
|
|||
|
get example_document.txt sample01.txt
|
|||
|
```
|
|||
|
|
|||
|
![Transferring a file from the remote server to the local system and changing the file name](https://phoenixnap.com/kb/wp-content/uploads/2021/11/sftp-transfer-files-04-copy-file-remote-to-local-rename.png)
|
|||
|
|
|||
|
In the example above, the **`get`** command fetches the _example_document.txt_ file and saves it as _sample01.txt_ on the local system.
|
|||
|
|
|||
|
SFTP also allows the transfer of an entire directory from the remote system by using the **`-r`** flag, indicating a recursive transfer of all files in the directory:
|
|||
|
|
|||
|
```
|
|||
|
get -r Example_Directory
|
|||
|
```
|
|||
|
|
|||
|
![Transferring a directory from the remote server to the local system](https://phoenixnap.com/kb/wp-content/uploads/2021/11/sftp-transfer-files-05-copy-directory-remote-to-local.png)
|
|||
|
|
|||
|
Add the **`-P`** flag to the **`get`** command to transfer the file or directory while preserving permissions and access times:
|
|||
|
|
|||
|
```
|
|||
|
get -Pr Example_Directory
|
|||
|
```
|
|||
|
|
|||
|
Use the **`ls`** command to verify the transfer to the local system:
|
|||
|
|
|||
|
```
|
|||
|
ls -l
|
|||
|
```
|
|||
|
|
|||
|
![Verifying that the files were transferred to the local system](https://phoenixnap.com/kb/wp-content/uploads/2021/11/sftp-transfer-files-06-copy-remote-to-local-check.png)
|
|||
|
|
|||
|
### Transfer Local Files to a Remote Server
|
|||
|
|
|||
|
To transfer files from a local system to a remote server, use the **`put`** command. The **`put`** command uses the same syntax and options as the **`get`** command.
|
|||
|
|
|||
|
```
|
|||
|
put [path to file]
|
|||
|
```
|
|||
|
|
|||
|
For example, to transfer an _example01.txt_ file to the remote server, use:
|
|||
|
|
|||
|
```
|
|||
|
put example01.txt
|
|||
|
```
|
|||
|
|
|||
|
![Transferring a file from the local system to the remote server](https://phoenixnap.com/kb/wp-content/uploads/2021/11/sftp-transfer-files-07-copy-file-local-to-remote.png)
|
|||
|
|
|||
|
To transfer the file to a specific directory on the remote server, append the path to the directory to the end of the **`put`** command.
|
|||
|
|
|||
|
```
|
|||
|
put example01.txt Example_Directory
|
|||
|
```
|
|||
|
|
|||
|
![Transferring a file from the local system to a specific directory on the remote server](https://phoenixnap.com/kb/wp-content/uploads/2021/11/sftp-transfer-files-08-copy-file-local-to-remote-change-dest.png)
|
|||
|
|
|||
|
Appending a new filename to the end of the **`put`** command changes the name of the transferred file on the remote server.
|
|||
|
|
|||
|
```
|
|||
|
put example01.txt text_sample.txt
|
|||
|
```
|
|||
|
|
|||
|
![Transferring a file from the local system to the remote server and changing the file name](https://phoenixnap.com/kb/wp-content/uploads/2021/11/sftp-transfer-files-09-copy-file-local-to-remote-rename.png)
|
|||
|
|
|||
|
Transferring an entire directory requires the **`-r`** flag.
|
|||
|
|
|||
|
```
|
|||
|
put -r Test_Directory
|
|||
|
```
|
|||
|
|
|||
|
![Transferring a directory from the local system to the remote server](https://phoenixnap.com/kb/wp-content/uploads/2021/11/sftp-transfer-files-10-copy-directory-local-to-remote-rename.png)
|
|||
|
|
|||
|
Add the **`-P`** flag to the **`put`** command to preserve file permissions:
|
|||
|
|
|||
|
```
|
|||
|
put -Pr Test_Directory
|
|||
|
```
|
|||
|
|
|||
|
Verify the file transfer by using the **`ls`** command on the remote system:
|
|||
|
|
|||
|
![Verifying that the files were transferred to the remote server](https://phoenixnap.com/kb/wp-content/uploads/2021/11/sftp-transfer-files-11-copy-local-to-remote-rename-check.png)
|
|||
|
|
|||
|
|
|||
|
---
|
|||
|
|
|||
|
## File Maintenance Using SFTP
|
|||
|
|
|||
|
SFTP supports basic file maintenance. For example, use SFTP to modify file and directory permissions on a remote system.
|
|||
|
|
|||
|
The **`chown`** command changes the file ownership similar to the [chmod command](https://phoenixnap.com/kb/chmod-recursive):
|
|||
|
|
|||
|
```
|
|||
|
chown [user ID] [path to file]
|
|||
|
```
|
|||
|
|
|||
|
Unlike the **`chmod`** command, **`chown`** accepts user IDs only and not usernames. Finding the UIDs for the remote server using the SFTP interface requires you to transfer and access the [/etc/passwd](https://phoenixnap.com/kb/etc-passwd) file:
|
|||
|
|
|||
|
```
|
|||
|
get /etc/passwd
|
|||
|
!less passwd
|
|||
|
```
|
|||
|
|
|||
|
The UID for each user can be found in the third column, as separated by the colons:
|
|||
|
|
|||
|
![Using the passwd file to find user IDs for the remote server](https://phoenixnap.com/kb/wp-content/uploads/2021/11/sftp-transfer-files-12-UID.png)
|
|||
|
|
|||
|
The **`chmod`** command works the same as in the standard shell:
|
|||
|
|
|||
|
```
|
|||
|
chmod [permission] [path to file]
|
|||
|
```
|
|||
|
|
|||
|
Another option is to change group file ownership with the **`chgrp`** command:
|
|||
|
|
|||
|
```
|
|||
|
chgrp [group ID] [path to file]
|
|||
|
```
|
|||
|
|
|||
|
Same as with the UIDs, the group IDs are found in the third column of the _/etc/group_ file on the remote server:
|
|||
|
|
|||
|
```
|
|||
|
get /etc/group
|
|||
|
!less group
|
|||
|
```
|
|||
|
|
|||
|
![Using the group file to find group IDs for the remote server](https://phoenixnap.com/kb/wp-content/uploads/2021/11/sftp-transfer-files-13-group-ID.png)
|
|||
|
|
|||
|
SFTP allows you to set a local umask, which changes the default file permission for the files transferred to the local system.
|
|||
|
|
|||
|
For instance:
|
|||
|
|
|||
|
```
|
|||
|
lumask 022
|
|||
|
```
|
|||
|
|
|||
|
**Note:** Learn more about permission masking in our [guide to the umask command](https://phoenixnap.com/kb/what-is-umask).
|
|||
|
|
|||
|
The command above changes the local umask to **022**. Every file transferred after you set this [umask](https://phoenixnap.com/kb/what-is-umask) now has the **644** permission by default. You can still preserve the original permission by using the **`-p`** flag.
|
|||
|
|
|||
|
Another way to change local file permissions is to use SFTP to replicate the behavior of shell commands. To do this, add an exclamation point (**!**) before the command name.
|
|||
|
|
|||
|
For instance, using the **`chmod`** command on the local system:
|
|||
|
|
|||
|
```
|
|||
|
!chmod [permission] [path to file]
|
|||
|
```
|
|||
|
|
|||
|
Conclusion
|
|||
|
|
|||
|
After reading this tutorial, you should be able to use SFTP to transfer files and directories between remote systems.
|
|||
|
|
|||
|
|
|||
|
---
|
|||
|
|
|||
|
##### Connecting to an SFTP server via [ExpanDrive](https://www.expandrive.com/sftp-drive-mac-windows/)
|
|||
|
|
|||
|
SFTP is often confused with FTP. They are in fact, quite [different](https://www.goanywhere.com/blog/2016/11/23/sftp-vs-ftps-the-key-differences). SFTP is a protocol that is provided by an [SSH server](https://en.wikipedia.org/wiki/OpenSSH), which is built into all Linux and Mac installations and is also available for windows. It is an extremely secure and popular protocol for remote access and file transfer. ExpanDrive supports both SFTP and FTP, but we strongly recommend using SFTP whenever possible because the protocol is more rigorously defined, which generally means much stronger compatibility.
|