How to use SFTP to interact with a remote server
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
l
We 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.
Related Articles
SSH key-based authentication setup from openSSH to SSH2
Publish Date:2025/04/07 Views:128 Category:OPERATING SYSTEM
-
Previous articles ( openSSH to openSSH setup , SSH2 to SSH2 setup ) explained how to set up key-based authentication on the same version of ssh to perform ssh and scp without entering a password. This article explains how to set up SSH key-
SSH2 key-based authentication setup
Publish Date:2025/04/07 Views:78 Category:OPERATING SYSTEM
-
I have previously explained how to perform SSH and SCP on openSSH without entering a password . In this article, I will explain how to set up key-based authentication on SSH2 and perform SSH/SCP without entering a password using the followi
SSH and SCP without password on openSSH
Publish Date:2025/04/07 Views:61 Category:OPERATING SYSTEM
-
SSH key-based authentication has two levels of security. In order for us to log in, we need both the private key and the password. Even if one of them is compromised, the attacker still won’t be able to log into our account because both o
How to SSH and SCP from SSH2 to OpenSSH without a password
Publish Date:2025/04/07 Views:195 Category:OPERATING SYSTEM
-
In our previous article, we discussed how to set up ssh key-based authentication to perform ssh and scp without a password in the following three scenarios: OpenSSH to OpenSSH OpenSSH to SSH2 SSH2 to SSH2 In this article, I will explain how
How to Set Up a Reverse SSH Tunnel on Linux
Publish Date:2025/04/07 Views:197 Category:OPERATING SYSTEM
-
Reverse SSH is a technique that can be used to access a system (behind a firewall) from the outside. As we all know, SSH is a network protocol that supports encrypted communication between network nodes. Using this protocol, we can do secur
3 Steps to Perform Passwordless SSH Login Using ssh-keygen and ssh-copy-id
Publish Date:2025/04/07 Views:135 Category:OPERATING SYSTEM
-
As explained in this article, we can use ssky-keygen and ssh-copy-id to log in to a remote Linux server without a password in 3 simple steps. ssh-keygen Create public and private keys. ssh-copy-id Copy the local host's public key to the rem
Linux iptables: How to add firewall rules (taking SSH as an example)
Publish Date:2025/04/07 Views:193 Category:OPERATING SYSTEM
-
This article explains how to iptables -A add iptables firewall rules using the (append) command. -A for appending. If it makes it easier for us to remember -A as adding rules (rather than appending rules), then that's fine. However, remembe
SSH to the remote server
Publish Date:2025/04/05 Views:73 Category:OPERATING SYSTEM
-
This article demonstrates how to use to ssh securely connect to a remote Linux server. Securely access remote Linux servers using SSH (Secure Socket Shell) SSH is the abbreviation of Secure Socket Shell. It is a protocol used to securely ac
Git push using SSH keys
Publish Date:2025/04/01 Views:95 Category:Git
-
SSH stands for Secure Shell. It is the key that provides us with the credentials to access the SSH network protocol. It provides access to remote servers between engines on an unsecured open network. It is used for transferring data, files,