> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Securely Transferring Files Between Systems

> Guide to securely transferring files and directories using SFTP over SSH, covering interactive and noninteractive commands, local versus remote operations and recursive transfers

In this lesson we'll cover how to securely transfer files and directories between systems using SFTP (Secure File Transfer Protocol). SFTP runs over SSH and provides an encrypted, authenticated channel for transferring files—making it a safer alternative than plain FTP. Compared with `scp`, SFTP is more interactive and feature-rich, offering directory listings, resume support, and both remote and local command variants.

<Callout icon="lightbulb" color="#1CB2FE">
  SFTP (part of the SSH suite) encrypts both authentication and data, protecting credentials and file contents while in transit.
</Callout>

## Starting an SFTP session

To begin an interactive SFTP session from a client machine:

```bash theme={null}
sftp user@host
```

After connecting you will see the `sftp>` prompt. Type `help` or `?` to list available commands. Commands prefixed with `l` operate on the local machine (for example, `lls`, `lcd`, `lpwd`), while commands without the `l` operate on the remote host (for example, `ls`, `cd`, `pwd`).

## Quick reference: common SFTP commands

| Command | Description | Example |
| -: | - | - |
| `ls [path]` | List files on the remote host | `ls incoming` |
| `lls [path]` | List files on the local host | `lls` |
| `cd path` | Change remote directory | `cd /var/www` |
| `lcd path` | Change local directory | `lcd ~/downloads` |
| `pwd` | Print remote working directory | `pwd` |
| `lpwd` | Print local working directory | `lpwd` |
| `put [local] [remote]` | Upload file from local to remote | `put file1` |
| `get [remote] [local]` | Download file from remote to local | `get incoming1` |
| `put -r` / `get -r` | Recursive upload/download for directories | `put -r my_photos/` |
| `bye` / `quit` | Exit the SFTP session | `bye` |

## Interactive help and local vs remote commands

Example short help output inside an SFTP session:

```sftp theme={null}
sftp> help
Usage: sftp [user@]host[:file ...]
sftp> ?
help                                Display this help text
lcd path                            Change local directory to 'path'
lls [ls-options [path]]             Display local directory listing
lpwd                                Print local working directory
ls [path]                           Display remote directory listing
mkdir path                          Create remote directory
put [-afpR] local [remote]          Upload file
get [-fpR] remote [local]           Download file
pwd                                 Display remote working directory
bye/quit                            Quit sftp
?                                   Synonym for help
```

Local vs remote listing example:

```sftp theme={null}
sftp> lls
file1  file2
sftp> ls
Desktop  Documents  Downloads  Music  Pictures  incoming
```

## Demo: preparing files locally and remotely

Create a local demo directory and two files on the control machine:

```bash theme={null}
student@control:~$ mkdir -p sftp-demo
student@control:~$ cd sftp-demo/
student@control:~/sftp-demo$ touch file1 file2
student@control:~/sftp-demo$ ls
file1  file2
```

On the remote host (`node1`) create a target directory and two remote files:

```bash theme={null}
student@node1:~$ mkdir -p incoming
student@node1:~$ touch incoming/incoming1 incoming/incoming2
student@node1:~$ ls incoming
incoming1  incoming2
```

## Interactive SFTP session: navigate, upload, and download

Connect from the control node to the remote node using SFTP:

```bash theme={null}
student@control:~/sftp-demo$ sftp student@node1
```

A sample interactive session showing local/remote navigation, uploads, and downloads:

```sftp theme={null}
Connected to node1.
sftp> lls
file1  file2
sftp> ls
Desktop  Documents  Downloads  Music  Pictures  incoming
sftp> cd incoming
sftp> ls
incoming1  incoming2
sftp> lpwd
Local working directory: /home/student/sftp-demo
sftp> pwd
Remote working directory: /home/student/incoming
```

Upload local files `file1` and `file2` to the remote `incoming` directory:

```sftp theme={null}
sftp> put file1
Uploading file1 to /home/student/incoming/file1
file1                      100%   0     0.0KB/s   00:00
sftp> put file2
Uploading file2 to /home/student/incoming/file2
file2                      100%   0     0.0KB/s   00:00
sftp> ls
file1    file2    incoming1    incoming2
```

Download remote files `incoming1` and `incoming2` to the local directory:

```sftp theme={null}
sftp> get incoming1
Fetching /home/student/incoming/incoming1 to /home/student/sftp-demo/incoming1
sftp> get incoming2
Fetching /home/student/incoming/incoming2 to /home/student/sftp-demo/incoming2
sftp> lls
file1  file2  incoming1  incoming2
```

Exit the session:

```sftp theme={null}
sftp> bye
```

## Non-interactive, one-liner transfers

SFTP also supports non-interactive one-liners to fetch or push a single file without entering the interactive prompt. Example: download a remote file directly into your local home directory:

```bash theme={null}
student@control:~/sftp-demo$ sftp student@node1:/home/student/incoming/file2 ~
Connected to node1.
Fetching /home/student/incoming/file2 to /home/student/file2
student@control:~/sftp-demo$
```

This command connects, downloads the specified remote file to `~`, and exits immediately.

<Callout icon="lightbulb" color="#1CB2FE">
  To transfer entire directories, use the `-r` option with `put` or `get` (for example, `put -r my_photos/` or `get -r /remote/path/`).
</Callout>

## Recap and next steps

* SFTP is an SSH-based secure file transfer tool that supports interactive and non-interactive workflows.
* Use `put` to upload and `get` to download. Prefix commands with `l` to operate on the local machine (e.g., `lls`, `lpwd`).
* For directories, use `-r` to transfer recursively.
* Use one-liners to script single file transfers, or open an interactive session for multi-step operations.

Further reading and references:

* OpenSSH SFTP documentation: [https://www.openssh.com/manual.html](https://www.openssh.com/manual.html)
* SSH overview and best practices: [https://www.ssh.com/academy/ssh](https://www.ssh.com/academy/ssh)

This concludes the lesson on securely transferring files with SFTP.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/red-hat-certified-system-administrator-rhcsa/module/bbf6f1f8-bab5-4888-b5f2-1fef65e24592/lesson/28ab5ad9-352c-4e96-a3c1-b57c355d9fe7" />

  <Card title="Practice Lab" icon="flask-conical" cta="Learn more" href="https://learn.kodekloud.com/user/courses/red-hat-certified-system-administrator-rhcsa/module/bbf6f1f8-bab5-4888-b5f2-1fef65e24592/lesson/990367b0-21d9-4552-afb7-9b23057069cc" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.