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
Create the chroot directory including the user's home directory.
# mkdir -p /path/to/directory/user/homeFor example:
# mkdir -p /storage/media/myuser/homeIn 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/uploadsCreate the user and specify the newly created home directory.
# useradd -d /path/to/directory/user/home -M username
# passwd usernameFor example:
# useradd -d /storage/media/myuser/home -M myuser
# passwd myuserApply the correct
SELinuxsecurity context to the home directory.
# chcon -Rv --type=user_home_dir_t /path/to/directory/user/homeFor example:
# chcon -Rv --type=user_home_dir_t /storage/media/myuser/homeCreate a
/devdirectory in the user directory.
# mkdir /path/to/directory/userFor example:
# mkdir /storage/media/myuser/devChange the ownership and permissions of the home directory.
# chown username:username /path/to/directory/user/home
# chmod 700 /path/to/directory/user/homeFor example:
# chown myuser:myuser /storage/media/myuser/home
# chmod 700 /storage/media/myuser/homeBecause of the requirements of chroot, all directories except for the home directory need to be owned by root.
For example:
# ls -l / | grep storage
drwxr-xr-x. 4 root root 32 Sep 22 09:23 storage
# ls -l /storage/
drwxr-xr-x. 3 root root 20 Sep 22 09:23 media
# ls -l /storage/media/
drwxr-xr-x. 4 root root 29 Sep 22 09:23 myuser
# ls -l /storage/media/myuser/
drwxr-xr-x. 2 root root 6 Sep 22 09:23 dev
drwx------. 2 myuser myuser 6 Sep 22 09:23 homeIf you use a different directory besides home, the same ownership and permissions apply.
# ls -l /storage/media/myuser/
drwxr-xr-x. 2 root root 6 Sep 22 09:23 dev
drwx------. 2 myuser myuser 6 Sep 22 09:23 uploadsUse the
ssh-copy-idcommand to copy thesshkey from a client to the server.
$ ssh-copy-id user@ip_addressFor example:
$ ssh-copy-id [email protected]Apply the correct
SELinuxsecurity context to the.sshdirectory.
# chcon -Rv -t ssh_home_t /path/to/directory/username/home/.ssh/For example:
# chcon -Rv -t ssh_home_t /storage/media/myuser/home/.ssh/Create an entry in the
/etc/rsyslog.conffile to create the log file for the user.
$AddUnixListenSocket /path/to/directory/user/dev/log
input(type="imuxsock" Socket="/path/to/directory/user/dev/log" CreatePath="on")
if $programname == 'internal-sftp' then /var/log/sftp.log
& stopFor example:
$AddUnixListenSocket /storage/media/myuser/dev/log
input(type="imuxsock" Socket="/storage/media/myuser/dev/log" CreatePath="on")
if $programname == 'internal-sftp' then /var/log/sftp.log
& stopRun the bind option of the
mountcommand.
# mount -o bind /dev /path/to/directory/user/devFor example:
# mount -o bind /dev /storage/media/myuser/devApply the correct
SELinuxlabel to the log file.
# semanage fcontext -a -t devlog_t /path/to/directory/user/dev/logFor example:
# semanage fcontext -a -t devlog_t /storage/media/myuser/dev/logEdit the
/etc/ssh/sshd_configfile and add aMatchsection for the user:
Match User username
ChrootDirectory /path/to/directory/%u
ForceCommand internal-sftp -f AUTH -l VERBOSE -d /UsersSubdirectoryFor example:
Match User myuser
ChrootDirectory /storage/media/%u
ForceCommand internal-sftp -f AUTH -l VERBOSE -d /homeEnable the
SELinuxboolean for ssh chroot.
# setsebool -P ssh_chroot_rw_homedirs on
# setsebool -P selinuxuser_use_ssh_chroot onRestart
rsyslogandsshd.
# systemctl restart rsyslog
# systemctl restart sshdLast updated