# Setup Chroot SFTP in CentOS

First version AT 2018-08-25 09:39:36 Updated AT 2019-11-08 14:36:29 for user separate syslog configuration.

Set up an account that will be used only to transfer files(and not to ssh to the system), you should setup SFTP Chroot Jail as explained in this article.

> If you want to give sftp access on your system to outside vendors to transfer files, you should not use standard sftp. Instead, you should setup Chroot SFTP Jail as explained below.

## Chroot SFTP Environment

`chroot` A chroot on Unix operating systems is an operation that changes the apparent root directory for the current running process and its children. Chroot SFTP means that the user can sftp to the system, and view only the directory that you’ve designated to perform sftp.

## Setup in CentOS

Test environment at CentOS 6.5.

### Create a New Group

```bash
groupadd sftpusers
```

### Create Users

`useradd -g sftpusers -s /sbin/nologin sftpuser`

`passwd sftpuser`

### Setup sftp-server Subsystem in sshd\_config

```bash
Subsystem  sftp  internal-sftp
Match Group sftpusers
    ChrootDirectory /sftp/%u
    X11Forwarding no
    AllowTCPForwarding no
    PasswordAuthentication yes
```

### Create sftp Home Directory

```bash
mkdir -p /sftp/sftpuser/home
chown sftpuser:sftpusers /sftp/sftpuser/home
```

### Restart sshd and Test Chroot SFTP

`service sshd restart`

## Script for Setup Chroot SFTP in CentOS 6.5

```bash
#!/bin/bash
#@Junyangz AT 2018-08-25 09:41:50 for configure sftp and add sftp users.

#@Configure part
#########################
sed -i "s/^Subsystem/#Subsystem/g" /etc/ssh/sshd_config
#sed -i "/^#Subsystem/aSubsystem sftp internal-sftp" /etc/ssh/sshd_config
groupadd sftpusers
#add follow to /etc/ssh/sshd_config
cat >>/etc/ssh/sshd_config <<EOF
Subsystem sftp internal-sftp
KexAlgorithms curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
Match Group sftpusers
        ChrootDirectory /data1/sftp/%u
        ForceCommand internal-sftp -f LOCAL0 -l INFO
        X11Forwarding no
        AllowTCPForwarding no
        PasswordAuthentication yes
EOF
service sshd restart
#@Add user part
#########################
# create an sftp user and jail them, using username, password and CHROOT path provided as script args.
if [ -z "$2" ]; then
    echo "Usage: add-sftp-user.sh username password /data1"
    echo "Create sftp user with chroot at /data1/sftp/username and work directory at home."
    exit 1
fi
username=$1 # get from script params
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
    echo "$username exists!"
    exit 1
else
password=$2 # get this from script params
#CHROOT_DIR=$3 # get this from script params
#useradd -g sftpusers -d $CHROOT_DIR/sftp/$username -s /sbin/nologin $username
useradd -g sftpusers -M -s /sbin/nologin $username
[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
echo "$username:$password" | chpasswd
usermod -d /upload $username
mkdir -p /data1/sftp/$username/upload
chown -R $username:sftpusers /data1/sftp/$username/upload
#chmod 755 $CHROOT_DIR/sftp/$username/upload
fi
```

## Log internal-sftp chroot jailed users

* add to `/etc/ssh/sshd_config`

```
# Subsystem sftp internal-sftp -l VERBOSE -f LOCAL0
# only need in Match group block
ForceCommand internal-sftp -f LOCAL0 -l INFO
```

* add to `/etc/rsyslog.d/sftp.conf`

```
# add sftp chroot log
# %u represent sftp username
# local0.* /var/log/sftp.log
# $AddUnixListenSocket /data1/sftp/%u/dev/log

#===above is old config===#

input(type="imuxsock" HostName="sftp_username" Socket="/data1/sftp/sftp_username/dev/log" CreatePath="on")
if $fromhost == 'sftp_username' then /var/log/sftp/sftp_username.log
& stop
```

* mkdir for socket file and touch sftp log

```bash
#%u represent sftp username
mkdir /data1/sftp/%u/dev
chown %u:sftpusers /data1/sftp/%u/dev
mkdir /var/log/sftp/
touch /var/log/sftp/sftp_username.log
```

* restart sshd and rsyslog

```bash
service sshd restart
service rsyslog restart
```

## Reference

1. [How to Setup Chroot SFTP in Linux (Allow Only SFTP, not SSH)](https://www.thegeekstuff.com/2012/03/chroot-sftp-setup)
2. [SFTP chroot](https://wiki.archlinux.org/index.php/SFTP_chroot)
3. [How to configure an sftp server with restricted chroot users with ssh keys](https://access.redhat.com/solutions/2399571)
4. [SFTP Guide - IBM](ftp://public.dhe.ibm.com/software/commerce/doc/sb2bi/gis42/GIS42_SFTP.pdf)
5. [SFTP log](https://access.redhat.com/discussions/672633)


---

# 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://docs.junyangz.com/ops/setup-chroot-sftp-in-centos.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.
