# Connecting with SFTP clients

This page is for the people who connect: what an SFTP program needs for a CrushMFT server, and how to log in with a key. For the server's side, see [SFTP and SCP ports](Ports-SFTP.md).

SFTP is file transfer over SSH. It uses one port, is always encrypted, and passes every firewall that lets that port through.

## What to ask the administrator

| You need | Example |
|---|---|
| the host name | `files.example.com` |
| the port | 22, or another the administrator names, often 2222 |
| your user name | |
| a password, or the word that you may use a key | |
| the fingerprint of the server's host key | `SHA256:…` |

## The first connection

At the first connection your program shows the **host key** of the server and asks whether you trust it. Compare the fingerprint with the one the administrator gave you, then accept. The program remembers it.

If a program warns later that the host key **has changed**, stop. Either the administrator replaced the key and will tell you so, or somebody is between you and the server. Ask before you accept.

## Common programs

**WinSCP.** New Site. File protocol: "SFTP". Host name, port, user name, password.

**FileZilla.** File › Site Manager › New site. Protocol: "SFTP - SSH File Transfer Protocol". Logon Type: "Normal", or "Key file".

**Cyberduck.** Open Connection, "SFTP (SSH File Transfer Protocol)".

**OpenSSH**, on the command line of macOS, Linux and Windows:

```
sftp -P 2222 alex@files.example.com
```

then `ls`, `cd`, `get file`, `put file`, `bye`. Note the capital `-P` for the port. Without a command line of its own, for a script:

```
sftp -P 2222 -b commands.txt alex@files.example.com
```

**scp** works too, for single files:

```
scp -P 2222 report.pdf alex@files.example.com:/Incoming/
```

## Logging in with a key

A key pair replaces the password: you keep the private key, the server knows the public one. Scripts should always use keys.

1. **Make a key pair.** With OpenSSH:

   ```
   ssh-keygen -t ed25519 -f ~/.ssh/files_example
   ```

   Give it a passphrase if a person uses it; leave it empty only for an unattended script, and then protect the file. In WinSCP and PuTTY, use PuTTYgen.

2. **Send the public key** to the administrator: the file that ends in `.pub`. Never the other one. The administrator adds it to your account: [SSH public keys of a user](Users-Restrictions-SSH-Keys.md).

3. **Use the private key:**

   ```
   sftp -P 2222 -i ~/.ssh/files_example alex@files.example.com
   ```

   In WinSCP: Advanced › SSH › Authentication › Private key file. In FileZilla: Logon Type "Key file".

Some accounts need **both** a key and a password, and some ask for a one-time code after them. Your program then asks for each in turn.

## In scripts

- Use a key, and `-b` with a file of commands, or a library for your language.
- Put the server's host key into `known_hosts` once, by hand. Do not turn host key checking off.
- Check the exit code. `sftp -b` stops at the first failed command and returns an error.

## When it does not work

| What you see | Usually |
|---|---|
| "Connection refused", or a time-out | the port; a firewall; or your address is not on the server's list. Tell the administrator the address you come from. |
| "Permission denied" | name or password; the key was not added yet; you offered another key than the one you sent; the account may not use SFTP |
| "no matching key exchange method", "no matching cipher", "no matching host key type" | your program is too old for the server's settings. Update it. If you cannot, tell the administrator the exact message: there are server settings for old clients. |
| "REMOTE HOST IDENTIFICATION HAS CHANGED" | see "The first connection" above |
| it asks for a password although you have a key | the key is not accepted: the wrong file, or not added to the account |
| uploads work, the file's date is "now" | the server is set not to take dates from clients |

Run `sftp -v` to see what your side offers and what the server answers; the last lines before the failure are what the administrator needs.
