> For the complete documentation index, see [llms.txt](https://gitbook.techbytes.app/devops-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.techbytes.app/devops-docs/processes/setup-sftp-in-linux.md).

# 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.
