ftp:setup_chroot_sftp
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
ftp:setup_chroot_sftp [2016/10/18 14:09] – [7. Restart sshd and Test Chroot SFTP] peter | ftp:setup_chroot_sftp [2019/11/29 14:30] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== FTP - Setup chroot SFTP ====== | ||
- | |||
- | Setup a SFTP Chroot Jail that will be used only to transfer files (and not to ssh to the system). | ||
- | |||
- | In a typical sftp scenario (when chroot sftp is not setup), if you use sftp, you can see root’s file as shown below. | ||
- | |||
- | 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. | ||
- | |||
- | |||
- | ===== Non-Chroot SFTP Environment ===== | ||
- | |||
- | In the following example (a typical sftp environment), | ||
- | |||
- | <code bash> | ||
- | sftp john@example.com | ||
- | john@example.com password: | ||
- | |||
- | sftp> pwd | ||
- | Remote working directory: /home/john | ||
- | |||
- | sftp> ls | ||
- | projects | ||
- | |||
- | sftp> cd /etc | ||
- | sftp> ls -l passwd | ||
- | -rw-r--r-- | ||
- | |||
- | sftp> get passwd | ||
- | Fetching /etc/passwd to passwd | ||
- | / | ||
- | </ | ||
- | |||
- | |||
- | ===== Chroot SFTP Environment ===== | ||
- | |||
- | In the following example, john can sftp to the system, and view only the directory that you’ve designated for john to perform sftp (i.e /incoming). | ||
- | |||
- | When john tries to perform ‘cd /etc’, it will give an error message. | ||
- | |||
- | <code bash> | ||
- | # sftp john@example.com | ||
- | john@example.com password: | ||
- | |||
- | sftp> pwd | ||
- | Remote working directory: /home/john | ||
- | |||
- | sftp> ls | ||
- | sftp> cd /etc | ||
- | Couldn' | ||
- | </ | ||
- | |||
- | |||
- | Now that you know what Chroot SFTP environment is, let us see how to set this up. | ||
- | |||
- | |||
- | ===== 1. Create a New Group ===== | ||
- | |||
- | Create a group called sftpusers. | ||
- | |||
- | <code bash> | ||
- | groupadd sftpusers | ||
- | </ | ||
- | |||
- | |||
- | ===== 2. Create Users (or Modify Existing User) ===== | ||
- | |||
- | Let us say you want to create an user guestuser who should be allowed only to perform SFTP in a chroot environment, | ||
- | |||
- | The following command creates guestuser, assigns this user to sftpusers group, make /incoming as the home directory, set / | ||
- | |||
- | <code bash> | ||
- | useradd -g sftpusers -d /incoming -s / | ||
- | passwd guestuser | ||
- | </ | ||
- | |||
- | Verify that the user got created properly. | ||
- | |||
- | <code bash> | ||
- | grep guestuser /etc/passwd | ||
- | guestuser: | ||
- | </ | ||
- | |||
- | If you want to modify an existing user and make him an sftp user only and put him in the chroot sftp jail, do the following: | ||
- | |||
- | <code bash> | ||
- | usermod -g sftpusers -d /incoming -s / | ||
- | </ | ||
- | |||
- | On a related note, if you have to transfer files from windows to Linux, use any one of the sftp client mentioned in this [[http:// | ||
- | |||
- | |||
- | ===== 3. Setup sftp-server Subsystem in sshd_config ===== | ||
- | |||
- | You should instruct sshd to use the internal-sftp for sftp (instead of the default sftp-server). | ||
- | |||
- | Modify the the / | ||
- | |||
- | <file bash / | ||
- | # | ||
- | </ | ||
- | |||
- | Next, add the following line to the / | ||
- | |||
- | <file bash / | ||
- | Subsystem | ||
- | </ | ||
- | |||
- | Check that the change is correct: | ||
- | |||
- | < | ||
- | grep sftp / | ||
- | # | ||
- | Subsystem | ||
- | </ | ||
- | |||
- | |||
- | ===== 4. Specify Chroot Directory for a Group ===== | ||
- | |||
- | You want to put only certain users (i.e users who belongs to sftpusers group) in the chroot jail environment. Add the following lines at the end of / | ||
- | |||
- | <code bash> | ||
- | # tail / | ||
- | Match Group sftpusers | ||
- | ChrootDirectory /sftp/%u | ||
- | ForceCommand internal-sftp | ||
- | </ | ||
- | |||
- | In the above: | ||
- | |||
- | * Match Group sftpusers – This indicates that the following lines will be matched only for users who belong to group sftpusers | ||
- | * ChrootDirectory /sftp/%u – This is the path that will be used for chroot after the user is authenticated. %u indicates the user. So, for john, this will be /sftp/john. | ||
- | * ForceCommand internal-sftp – This forces the execution of the internal-sftp and ignores any command that are mentioned in the ~/.ssh/rc file. | ||
- | |||
- | |||
- | ===== 5. Create sftp Home Directory ===== | ||
- | |||
- | Since we’ve specified /sftp as ChrootDirectory above, create this directory (which is the equivalent of your typical /home directory). | ||
- | |||
- | <code bash> | ||
- | mkdir /sftp | ||
- | </ | ||
- | |||
- | Now, under /sftp, create the individual directories for the users who are part of the sftpusers group. i.e the users who will be allowed only to perform sftp and will be in chroot environment. | ||
- | |||
- | <code bash> | ||
- | mkdir / | ||
- | </ | ||
- | |||
- | So, / | ||
- | |||
- | So, under this directory / | ||
- | |||
- | <code bash> | ||
- | mkdir / | ||
- | </ | ||
- | |||
- | |||
- | ===== 6. Setup Appropriate Permission ===== | ||
- | |||
- | For chroot to work properly, you need to make sure appropriate permissions are setup properly on the directory you just created above. | ||
- | |||
- | Set the ownership to the user, and group to the sftpusers group as shown below. | ||
- | |||
- | <code bash> | ||
- | chown guestuser: | ||
- | </ | ||
- | |||
- | The permission will look like the following for the incoming directory. | ||
- | |||
- | <code bash> | ||
- | ls -ld / | ||
- | drwxr-xr-x 2 guestuser sftpusers 4096 Dec 28 23:49 / | ||
- | </ | ||
- | |||
- | The permission will look like the following for the / | ||
- | |||
- | <code bash> | ||
- | ls -ld / | ||
- | drwxr-xr-x 3 root root 4096 Dec 28 23:49 / | ||
- | |||
- | # ls -ld /sftp | ||
- | drwxr-xr-x 3 root root 4096 Dec 28 23:49 /sftp | ||
- | </ | ||
- | |||
- | |||
- | ===== 7. Restart sshd and Test Chroot SFTP ===== | ||
- | |||
- | Restart sshd: | ||
- | |||
- | <code bash> | ||
- | service sshd restart | ||
- | </ | ||
- | |||
- | |||
- | Test chroot sftp environment. As you see below, when gusetuser does sftp, and does “cd /”, they’ll only see incoming directory. | ||
- | |||
- | <code bash> | ||
- | sftp guestuser@example.com | ||
- | guestuser@example.com password: | ||
- | |||
- | sftp> pwd | ||
- | Remote working directory: /incoming | ||
- | |||
- | sftp> cd / | ||
- | sftp> ls | ||
- | incoming | ||
- | </ | ||
- | |||
- | |||
- | When guestuser transfers any files to the /incoming directory from the sftp, they’ll be really located under / | ||
- | |||
ftp/setup_chroot_sftp.1476799762.txt.gz · Last modified: 2020/07/15 09:30 (external edit)