How to create a chroot sftp user with logging on a non-standard home directory

Environment

  • Red Hat Enterprise Linux

  • openssh

Issue

  • How to configure chroot sftp users with logging with a non-standard home directory.

Resolution

  1. Create the chroot directory including the user's home directory.

# mkdir -p /path/to/directory/user/home

For example:

# mkdir -p /storage/media/myuser/home

In this example, home was used for the name of the directory under the username, but this subdirectory can be named whatever you want.

For example:

# mkdir -p /storage/media/myuser/uploads
  1. Create the user and specify the newly created home directory.

# useradd -d /path/to/directory/user/home -M username
# passwd username

For example:

  1. Apply the correct SELinux security context to the home directory.

For example:

  1. Create a /dev directory in the user directory.

For example:

  1. Change the ownership and permissions of the home directory.

For example:

Because of the requirements of chroot, all directories except for the home directory need to be owned by root.

For example:

If you use a different directory besides home, the same ownership and permissions apply.

  1. Use the ssh-copy-id command to copy the ssh key from a client to the server.

For example:

  1. Apply the correct SELinux security context to the .ssh directory.

For example:

  1. Create an entry in the /etc/rsyslog.conf file to create the log file for the user.

For example:

  1. Run the bind option of the mount command.

For example:

  1. Apply the correct SELinux label to the log file.

For example:

  1. Edit the /etc/ssh/sshd_config file and add a Match section for the user:

For example:

  1. Enable the SELinux boolean for ssh chroot.

  1. Restart rsyslog and sshd.

Last updated