|
| 1 | +# SFTP |
| 2 | + |
| 3 | +Easy to use SFTP ([SSH File Transfer Protocol](https://en.wikipedia.org/wiki/SSH_File_Transfer_Protocol)) server with [OpenSSH](https://en.wikipedia.org/wiki/OpenSSH). |
| 4 | + |
| 5 | +# Usage |
| 6 | + |
| 7 | +- Define users in (1) command arguments, (2) `SFTP_USERS` environment variable |
| 8 | + or (3) in file mounted as `/etc/sftp/users.conf` (syntax: |
| 9 | + `user:pass[:e][:uid[:gid[:dir1[,dir2]...]]] ...`, see below for examples) |
| 10 | + - Set UID/GID manually for your users if you want them to make changes to |
| 11 | + your mounted volumes with permissions matching your host filesystem. |
| 12 | + - Directory names at the end will be created under user's home directory with |
| 13 | + write permission, if they aren't already present. |
| 14 | +- Mount volumes |
| 15 | + - The users are chrooted to their home directory, so you can mount the |
| 16 | + volumes in separate directories inside the user's home directory |
| 17 | + (/home/user/**mounted-directory**) or just mount the whole **/home** directory. |
| 18 | + Just remember that the users can't create new files directly under their |
| 19 | + own home directory, so make sure there are at least one subdirectory if you |
| 20 | + want them to upload files. |
| 21 | + - For consistent server fingerprint, mount your own host keys (i.e. `/etc/ssh/ssh_host_*`) |
| 22 | + |
| 23 | +# Examples |
| 24 | + |
| 25 | +## Simplest docker run example |
| 26 | + |
| 27 | +``` |
| 28 | +docker run -p 22:22 -d yieldstudio/sftp foo:pass:::upload |
| 29 | +``` |
| 30 | + |
| 31 | +User "foo" with password "pass" can login with sftp and upload files to a folder called "upload". No mounted directories or custom UID/GID. Later you can inspect the files and use `--volumes-from` to mount them somewhere else (or see next example). |
| 32 | + |
| 33 | +## Sharing a directory from your computer |
| 34 | + |
| 35 | +Let's mount a directory and set UID: |
| 36 | + |
| 37 | +``` |
| 38 | +docker run \ |
| 39 | + -v <host-dir>/upload:/home/foo/upload \ |
| 40 | + -p 2222:22 -d yieldstudio/sftp \ |
| 41 | + foo:pass:1001 |
| 42 | +``` |
| 43 | + |
| 44 | +### Using Docker Compose: |
| 45 | + |
| 46 | +``` |
| 47 | +sftp: |
| 48 | + image: yieldstudio/sftp |
| 49 | + volumes: |
| 50 | + - <host-dir>/upload:/home/foo/upload |
| 51 | + ports: |
| 52 | + - "2222:22" |
| 53 | + command: foo:pass:1001 |
| 54 | +``` |
| 55 | + |
| 56 | +### Logging in |
| 57 | + |
| 58 | +The OpenSSH server runs by default on port 22, and in this example, we are forwarding the container's port 22 to the host's port 2222. To log in with the OpenSSH client, run: `sftp -P 2222 foo@<host-ip>` |
| 59 | + |
| 60 | +## Store users in config |
| 61 | + |
| 62 | +``` |
| 63 | +docker run \ |
| 64 | + -v <host-dir>/users.conf:/etc/sftp/users.conf:ro \ |
| 65 | + -v mySftpVolume:/home \ |
| 66 | + -p 2222:22 -d yieldstudio/sftp |
| 67 | +``` |
| 68 | + |
| 69 | +<host-dir>/users.conf: |
| 70 | + |
| 71 | +``` |
| 72 | +foo:123:1001:100 |
| 73 | +bar:abc:1002:100 |
| 74 | +baz:xyz:1003:100 |
| 75 | +``` |
| 76 | + |
| 77 | +## Providing your own SSH host key (recommended) |
| 78 | + |
| 79 | +This container will generate new SSH host keys at first run. To avoid that your users get a MITM warning when you recreate your container (and the host keys changes), you can mount your own host keys. |
| 80 | + |
| 81 | +``` |
| 82 | +docker run \ |
| 83 | + -v <host-dir>/ssh_host_ed25519_key:/etc/ssh/ssh_host_ed25519_key \ |
| 84 | + -v <host-dir>/ssh_host_rsa_key:/etc/ssh/ssh_host_rsa_key \ |
| 85 | + -v <host-dir>/share:/home/foo/share \ |
| 86 | + -p 2222:22 -d yieldstudio/sftp \ |
| 87 | + foo::1001 |
| 88 | +``` |
| 89 | + |
| 90 | +You can also setup the host keys with `SSH_ED25519_KEY` and `SSH_RSA_KEY` environment variables (base64 encoded). |
| 91 | + |
| 92 | +``` |
| 93 | +docker run \ |
| 94 | + -e SSH_ED25519_KEY=$(cat <host-dir>/ssh_host_ed25519_key | base64) \ |
| 95 | + -e SSH_RSA_KEY=$(cat <host-dir>/ssh_host_rsa_key | base64) \ |
| 96 | + -v <host-dir>/share:/home/foo/share \ |
| 97 | + -p 2222:22 -d yieldstudio/sftp \ |
| 98 | + foo::1001 |
| 99 | +``` |
| 100 | + |
| 101 | +Tip: you can generate your keys with these commands: |
| 102 | + |
| 103 | +``` |
| 104 | +ssh-keygen -t ed25519 -f ssh_host_ed25519_key < /dev/null |
| 105 | +ssh-keygen -t rsa -b 4096 -f ssh_host_rsa_key < /dev/null |
| 106 | +``` |
| 107 | + |
| 108 | +**NOTE:** Using `mount` requires that your container runs with the `CAP_SYS_ADMIN` capability turned on. [See this answer for more information](https://github.com/yieldstudio/sftp/issues/60#issuecomment-332909232). |
| 109 | + |
| 110 | +## S3FS Mounting |
| 111 | + |
| 112 | +You can mount an S3 bucket using s3fs. You need to provide your AWS credentials and the bucket name as environment variables. You also need to install s3fs in the container. You can do this by creating a custom Dockerfile that extends the yieldstudio/sftp image. |
| 113 | + |
| 114 | +``` |
| 115 | +docker run \ |
| 116 | + -e ENABLE_S3FS=true \ |
| 117 | + -e AWS_S3_BUCKET=<your-bucket-name> \ |
| 118 | + -e AWS_S3_ACCESS_KEY_ID=<your-access-key-id> \ |
| 119 | + -e AWS_S3_SECRET_ACCESS_KEY=<your-secret-access-key> \ |
| 120 | + -p 2222:22 -d yieldstudio/sftp \ |
| 121 | + foo::1001 |
| 122 | +``` |
| 123 | + |
| 124 | +### Scaleway S3 |
| 125 | + |
| 126 | +If you are using Scaleway S3, you need to provide the endpoint as well. |
| 127 | + |
| 128 | +``` |
| 129 | +docker run \ |
| 130 | + -e ENABLE_S3FS=true \ |
| 131 | + -e AWS_S3_BUCKET=<your-bucket-name> \ |
| 132 | + -e AWS_S3_ACCESS_KEY_ID=<your-access-key-id> \ |
| 133 | + -e AWS_S3_SECRET_ACCESS_KEY=<your-secret-access-key> \ |
| 134 | + -e AWS_S3_URL="https://s3.<region>.scw.cloud" \ |
| 135 | + -e S3FS_ARGS="allow_other,use_path_request_style,nocopyapi,parallel_count=15,multipart_size=128" \ |
| 136 | + -e SFTP_USERS="foo::1001" \ |
| 137 | + -p 2222:22 -d yieldstudio/sftp |
| 138 | +``` |
0 commit comments