# Setup SFTP in Linux

## Steps to Configure SFTP in Linux

* Configure env vars for Setup

```bash
export SFTP_HOME_PATH=/srv/sftpuser
export SFTP_USER_GROUP=sftpgroup
```

* Update the apt

```bash
sudo apt-get update
sudo apt-get upgrade -y
```

* Install OpenSSH

```bash
sudo apt install ssh -y
```

* Configure sftp

```bash
sudo nano /etc/ssh/sshd_config
```

* Add the below content at the end of the file (replace vars)

```bash
Match Group <SFTP_USER_GROUP>
ChrootDirectory <SFTP_HOME_PATH>
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp
PasswordAuthentication yes
PermitRootLogin no
```

* Add new group

```bash
sudo groupadd $SFTP_USER_GROUP
```

* Add user to group and restrict interactive login

```bash
sudo useradd -G $SFTP_USER_GROUP -d $SFTP_HOME_PATH -s /sbin/nologin sftpuser
```

* Set password for sftp user

```bash
sudo passwd sftpuser
```

* Create directory for sftpuser

```bash
sudo mkdir -p $SFTP_HOME_PATH
```

* Set root as own for sftpuser directory

```bash
sudo chown root $SFTP_HOME_PATH
```

* Allow group read and execution perms

```bash
sudo chmod g+rx $SFTP_HOME_PATH
```

* Create Sub Directory for storing data of sftp

```bash
sudo mkdir -p $SFTP_HOME_PATH/data
```

* set sftpuser as owner for Data Directory

```bash
sudo chown sftpuser:sftpuser $SFTP_HOME_PATH/data
```

* Connect to sftp

```bash
sftp sftpuser@<IP_ADDRESS>
```

> Enter the password you've configured in above steps

### Handling Downtime

* Logging the Failures in SQL Database.
* Add a timer trigger to retry these failed cases and notify if not worked.
* Retry for 5 times, and then ignore by logging error.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.techbytes.app/devops-docs/processes/setup-sftp-in-linux.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
