JIYIK CN >

Current Location:Home > Learning > NETWORK >

How to use SFTP to interact with a remote server

Author:JIYIK Last Updated:2025/03/17 Views:

FTP , or File Transfer Protocol, is a popular, unencrypted method of transferring files between two remote systems. As of 2022, it has been deprecated by most modern software due to its lack of security, and is mostly only used for legacy applications.

SFTP is Secure File Transfer Protocol, a separate protocol built into SSH that implements FTP commands over a secure connection. Generally, it can be used directly in any environment where an FTP server is still required.

In almost all cases, SFTP is more reliable than FTP due to its underlying security features and ability to piggyback on SSH connections. FTP is an insecure protocol that should only be used in limited circumstances or on networks you trust.

Although SFTP is integrated into many graphical tools, we will demonstrate how to use it through its interactive command line interface.

How to connect to SFTP

By default, SFTP uses the SSH protocol to authenticate and establish a secure connection. Therefore, you can use the same authentication methods as SSH.

Although we can use passwords for authentication by default, we recommend that you create SSH keys and transfer your public key to any system you need to access. This is more secure and can save time in the long run. For more information about setting up SSH keys, please refer to our How to Create Git SSH Keys.

Although we can use passwords for authentication by default, we recommend that you create SSH keys and transfer your public key to any system you need access to. This is much more secure and will save time in the long run.

If you can connect to your computer using SSH, you have completed all the necessary requirements to manage files using SFTP. Test SSH access using the following command:

$ ssh username@your_server_ip_or_remote_hostname

If you can connect, you can exit by entering the following command:

$ exit

Now we can establish an SFTP session by using the following command:

$ sftp username@your_server_ip_or_remote_hostname

This will allow us to connect to the remote system and the terminal prompt will change to the SFTP prompt.

sftp> 

If you are using a custom SSH port (not the default port 22), you can open an SFTP session as follows:

$ sftp -oPort=custom_port username@your_server_ip_or_remote_hostname

This will connect to the remote system via the port we specified.

Getting help with SFTP

The first and most useful command to learn is the help command. We can use this command to see how other SFTP commands are used.

sftp> help

or

sftp> ?

This will display a list of available commands:

Available commands:
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'
. . .

We will introduce some commands in three parts.

Browsing the file system using SFTP

We can browse the file hierarchy of a remote system using a number of commands that function similarly to their shell counterparts.

First, let's orient ourselves by finding out which directory we are currently in on the remote system. Just like in a typical shell session, we can get the current directory with the following command:

sftp> pwd

The output is as follows

Remote working directory: /home/demouser

We can view the contents of the remote system's current directory with another familiar command:

sftp> ls

The output is as follows

Summary.txt     info.html       temp.txt        testDirectory

Note that the commands available in the SFTP interface are not fully functional as those in shell syntax and are not as feature-rich. However, they do implement some of the more important options, such as adding -la to ls to see more file metadata and permissions:

sftp> ls -la

The output is as follows

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

To move into another directory, we can use the following command:

sftp> cd testDirectory

lWe can now traverse the remote file system, but what if we need to access the local file system? We can direct the command to the local file system by prefixing it with a .

All the commands discussed so far have local equivalents. We can print the local working directory:

sftp> lpwd

The output is as follows

Local working directory: /Users/demouser

We can list the contents of the current directory on our local machine:

sftp> lls

The output is as follows

Desktop         local.txt       test.html
Documents       analysis.rtf        zebra.html

We can also change the directory we want to interact with on our local system:

sftp> lcd Desktop

In the next article we will introduce how to transfer files with remote servers using SFTP.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to use SFTP to transfer files to a remote server

Publish Date:2025/03/17 Views:179 Category:NETWORK

In the article "How to use SFTP to interact with a remote server" , we introduced how to connect and interact with a server using SFTP. Now let's take a look at how to perform the main operation - file transfer - after the connection is est

用 C# 发送一个简单的 SSH 命令

Publish Date:2024/02/02 Views:219 Category:编程语言

本教程讨论如何在 C# 中发送简单的 SSH 命令。指南将讨论如何在 C# 中发送简单的 SSH 命令。在 C# 中发送一个简单的 SSH 命令

在 Java 中创建 SSH 连接

Publish Date:2023/08/02 Views:298 Category:Java

本文讨论如何打开 SSH 连接,并演示如何使用 JSch 和 Apache Mina SSHD 创建 SSH 连接。在 Java 中创建 SSH 连接

在 Python 中创建 SFTP 功能

Publish Date:2023/06/20 Views:245 Category:Python

本文向您展示如何在 Python 中使用 SFTP 来移动数据和文件。使用 pysftp 在 Python 中创建 SFTP 功能

在 Git 中使用 SSH 密钥克隆仓库或分支

Publish Date:2023/04/04 Views:462 Category:Git

你可以在本教程中使用 SSH 密钥进行 Git 克隆 - 在 Git 中设置 SSH,在 GitHub 中更新 SSH 公钥,并使用选项 - 仅克隆特定分支、选择的位置或仅克隆最近的提交。

Git 远程添加 SSH

Publish Date:2023/03/31 Views:217 Category:Git

本篇文章介绍了在 Git 中通过不同的 SSH 端口远程添加 SSH 的过程。

Git push 使用 SSH 密钥

Publish Date:2023/03/30 Views:224 Category:Git

SSH 代表 Secure Shell。 它是为我们提供 SSH 网络协议访问证书的密钥。它提供对无保证开放网络上引擎之间的远程服务器的访问。 它用于传输数据、文件和网络管理,并提供从源头访问远程服务器

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial