How to configure chrooted users with SFTP-only access.

Environment

  • Red Hat Enterprise Linux

Issue

  • Learn how to set up chrooted users with SFTP-only access, using SSH keys.

Resolution

Create a chroot sftp user.

Raw

# useradd testuser

Create an sftp group.

Raw

# groupadd sftpusers

Add the chroot user to the sftp group.

Raw

# usermod -aG sftpusers testuser

Make a root directory for the chroot users.

Raw

# mkdir /sftp

Create the user's chroot directory.

Raw

# mkdir /sftp/testuser

Configure the correct permissions and ownership for the chroot directory.

Raw

# chown testuser:testuser /sftp/testuser
# chmod 700 /sftp/testuser

Ensure that the user is able to access the directory:

Raw

# sudo -u testuser ls /sftp/testuser

If it fails, add the explore permission for others for all directories until HOME:

Raw

chmod o+x /sftp/

Create an .ssh directory with an authorized_keys file in the user's /home/directory.

Raw

# mkdir /home/testuser/.ssh
# touch /home/testuser/.ssh/authorized_keys
# chmod 700 /home/testuser/.ssh
# chmod 600 /home/testuser/.ssh/authorized_keys

Copy and paste the contents of the .ssh/id_rsa.pub file from the client into the authorized_keys file that you just created.

Configure the correct ownership of the .ssh directory and the authorized_keys file.

Raw

# chown testuser:testuser /home/testuser/.ssh
# chown testuser:testuser /home/testuser/.ssh/authorized_keys

Change the Subsystem line in the /etc/ssh/sshd_config file.

Raw

Subsystem   sftp    internal-sftp

Add a Match block at the end of the /etc/ssh/sshd_config file.

Raw

Match Group sftpusers
ChrootDirectory /sftp/
ForceCommand internal-sftp -d /%u

Restart the sshd service.

Red Hat Enterprise Linux 6

Raw

# service sshd restart

Red Hat Enterprise Linux 7 or newer

Raw

# systemctl restart sshd
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值