Skip to content

Commit b1ee0c8

Browse files
committed
# SFTP server rewrite
* refer to change root, as chroot, in the initial definition, and then use chroot from that point on * verify ownership and permissions in the script for correct SFTP access and security * remove the sections for Rocky Linux 8.5, as anyone using 8.x at this point should not still be on 8.5
1 parent e849789 commit b1ee0c8

File tree

1 file changed

+46
-102
lines changed

1 file changed

+46
-102
lines changed

docs/guides/file_sharing/sftp.md

Lines changed: 46 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,28 @@ tags:
1515

1616
## Introduction
1717

18-
When the SSH protocol itself is secure, it may seem strange to have a document dedicated to the "secure" use of SFTP (a part of openssh-server package). But most system administrators do not want to open SSH to everyone to implement SFTP for everyone. This document describes implementing a change root jail<sup>1</sup> for SFTP while limiting SSH access.
18+
When the SSH protocol itself is secure, it may seem strange to have a document dedicated to the "secure" use of SFTP (a part of openssh-server package). But most system administrators do not want to open SSH to everyone to implement SFTP for everyone. This document describes implementing a change root (**chroot**) jail<sup>1</sup> for SFTP while limiting SSH access.
1919

20-
Many documents deal with creating an SFTP change root jail, but most do not consider a use case where the user might be accessing a web directory on a server with many websites. This document deals with that. If that is not your use case, you can quickly adapt these concepts to different situations.
20+
Many documents deal with creating an SFTP chroot jail, but most do not consider a use case where the user might be accessing a web directory on a server with many websites. This document deals with that. If that is not your use case, you can quickly adapt these concepts to different situations.
2121

22-
The author also feels that it is necessary when making the change root jail document for SFTP to also discuss the other things that you should do as a system administrator to minimize the target that you offer to the world via SSH. For this reason, division of this document is in four parts:
22+
The author also feels that it is necessary when making the chroot jail document for SFTP to also discuss the other things that you should do as a system administrator to minimize the target that you offer to the world via SSH. For this reason, division of this document is in four parts:
2323

2424
1. The first deals with the general information that you will use for the entire document.
25-
2. The second deals with the setup of the change root jail, and if you decide to stop there that is totally up to you.
25+
2. The second deals with the setup of the chroot, and if you decide to stop there that is totally up to you.
2626
3. The third part deals with setting up public/private key SSH access for your system administrators and turning off remote password-based authentication.
2727
4. This document's fourth and last section deals with turning off remote root logins.
2828

2929
All of these steps will allow you to offer secure SFTP access for your customers while minimizing the possibility that a bad actor will compromise port 22 (the one reserved for SSH access).
3030

31-
!!! Note "<sup>1</sup> Change root jails for beginners:"
31+
!!! Note "chroot jails for beginners:"
3232

33-
Change root (or chroot) jails are a way to restrict what a process and all of its various child processes can do on your computer. It allows you to choose a specific directory or folder on your machine, and make that the "root" dirtectory for any process or program.
33+
chroot jails are a way to restrict what a process and all of its various child processes can do on your computer. It allows you to choose a specific directory or folder on your machine, and make that the "root" dirtectory for any process or program.
3434

3535
From there on, that process or program can *only* access that folder and its subfolders.
3636

37-
!!! tip "Updates for Rocky Linux 8.6"
37+
!!! tip "Updates for Rocky Linux 8.x and 9.x"
3838

39-
This document has been updated to include new changes in version 8.6 that will make this procedure even safer. If you are using 8.6, specific sections in the document below have the prefixes "8.6 -". The sections specific to Rocky Linux 8.5 have the prefixes "8.5 - " for clarity. Other than those sections specifically prefixed, this document is generic for both versions of the OS.
39+
This document has been updated to include new changes in version 8.6 that will make this procedure even safer. If you are using 8.6 or newer, or any version of 9.x, this procedure should work for you. The sections specific to Rocky Linux 8.5 have been removed, as the current release of 8 (8.8 at the time of the rewrite) should be where any version of 8.x is after updating packages.
4040

4141
## Part 1: General information
4242

@@ -70,7 +70,7 @@ These are fictitious scenarios. Any resemblance to persons or sites that are rea
7070
* Steve Simpson = ssimpson
7171
* Laura Blakely = lblakely
7272

73-
## Part 2: SFTP change root jail
73+
## Part 2: SFTP chroot jail
7474

7575
### Installation
7676

@@ -84,7 +84,7 @@ dnf install openssh-server
8484

8585
#### Directories
8686

87-
* The directory path structure will be `/var/www/sub-domains/[ext.domainname]/html` and the `html` directory in this path will be the change root jail for the SFTP user.
87+
* The directory path structure will be `/var/www/sub-domains/[ext.domainname]/html` and the `html` directory in this path will be the chroot jail for the SFTP user.
8888

8989
Creating the configuration directories:
9090

@@ -319,7 +319,7 @@ Match User replaceuser
319319

320320
!!! note
321321

322-
The `PasswordAuthentication yes` would not normally be required for the change root jail. However, you will be turning off `PasswordAuthentication` later on for everyone else, so having this line in the template is essential.
322+
The `PasswordAuthentication yes` would not normally be required for the chroot jail. However, you will be turning off `PasswordAuthentication` later on for everyone else, so having this line in the template is essential.
323323

324324
You want a directory for your user files that you will create from the template too:
325325

@@ -328,42 +328,40 @@ mkdir /usr/local/sbin/templates
328328
```
329329

330330

331-
=== "8.6 & 9.0"
331+
#### The script and `sshd_config` changes
332332

333-
#### 8.6 & 9.0 - The script and `sshd_config` changes
333+
With the releases of Rocky Linux 8.6 and 9.0, a new option for the `sshd_config` file that allows for drop-in configurations. This is a **GREAT** change. What this means is that for these versions you will make a single additional change to the `sshd_config` file, and then our script will build out sftp changes in a separate configuration file. This new change makes things even safer. Safety is good!!
334334

335-
With the releases of Rocky Linux 8.6 and 9.0, a new option for the `sshd_config` file that allows for drop-in configurations. This is a **GREAT** change. What this means is that for these versions you will make a single additional change to the `sshd_config` file, and then our script will build out sftp changes in a separate configuration file. This new change makes things even safer. Safety is good!!
335+
Because of the changes allowed for the `sshd_config` file in Rocky Linux 8.6 and 9.0, our script will use a new drop-in configuration file: `/etc/ssh/sftp/sftp_config`.
336336

337-
Because of the changes allowed for the `sshd_config` file in Rocky Linux 8.6 and 9.0, our script will use a new drop-in configuration file: `/etc/ssh/sftp/sftp_config`.
337+
To start with, create that directory:
338338

339-
To start with, create that directory:
340-
341-
```
342-
mkdir /etc/ssh/sftp
343-
```
339+
```
340+
mkdir /etc/ssh/sftp
341+
```
344342

345-
Now make a backup copy of the `sshd_config`:
343+
Now make a backup copy of the `sshd_config`:
346344

347-
```
348-
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
349-
```
345+
```
346+
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
347+
```
350348

351-
And finally edit the `sshd_config` file, scroll to the very bottom of the file, and add this line:
349+
And finally edit the `sshd_config` file, scroll to the very bottom of the file, and add this line:
352350

353-
```bash
354-
Include /etc/ssh/sftp/sftp_config
355-
```
351+
```bash
352+
Include /etc/ssh/sftp/sftp_config
353+
```
356354

357-
Save your changes and exit the file. You will need to restart `sshd` but our script will do that for us after you update `sftp_config` file, so create the script and run it.
355+
Save your changes and exit the file. You will need to restart `sshd` but our script will do that for us after you update `sftp_config` file, so create the script and run it.
358356

359-
```
360-
vi /usr/local/sbin/webuser
361-
```
357+
```
358+
vi /usr/local/sbin/webuser
359+
```
362360

363-
And put this code in it:
361+
And put this code in it:
364362

365-
```
366-
#!/bin/bash
363+
```
364+
#!/bin/bash
367365
# script to populate the SSHD configuration for web users.
368366
369367
# Set variables
@@ -391,6 +389,15 @@ mkdir /usr/local/sbin/templates
391389
/usr/bin/sed -i "s,replaceuser,$sftpuser,g" /usr/local/sbin/templates/$dom.txt
392390
/usr/bin/sed -i "s,replacedirectory,$dompath$dom,g" /usr/local/sbin/templates/$dom.txt
393391
/usr/bin/chown -R $sftpuser.apache $dompath$dom/html
392+
# Ensure directory permissions are correct
393+
# The root user owns all directories except the chroot, which is owned by the sftpuser
394+
# when connecting, you will end up one directory down, and you must actually change to the html directory
395+
# With a graphical SFTP client, this will be visible to you, you just need to double-click on the html
396+
# directory before attmpting to drop in files.
397+
chmod 755 $dompath
398+
chmod 755 $dompath$dom
399+
chmod 755 $dompath$dom/html
400+
chmod 744 -R $dompath$dom/html/
394401
fi
395402
396403
## Make a backup of /etc/ssh/sftp/sftp_config
@@ -411,80 +418,17 @@ mkdir /usr/local/sbin/templates
411418
echo "Please check the status of sshd with systemctl status sshd."
412419
echo "You can verify that your information was added by doing a more of the sftp_config"
413420
echo "A backup of the working sftp_config was created when this script was run: sftp_config.bak"
414-
```
415-
=== "8.5"
416-
417-
#### 8.5 - The script
418-
419-
Create your script:
420-
421-
```
422-
vi /usr/local/sbin/webuser
423-
```
424-
425-
And put this code in it:
426-
427-
```
428-
#!/bin/bash
429-
# script to populate the SSHD configuration for web users.
430-
431-
# Set variables
432-
433-
tempfile="/usr/local/sbin/sshd_template"
434-
dompath="/var/www/sub-domains/"
435-
436-
# Prompt for user and domain in reverse (ext.domainname):
437-
438-
clear
439-
440-
echo -n "Enter the web sftp user: "
441-
read sftpuser
442-
echo -n "Enter the domain in reverse. Example: com.domainname: "
443-
read dom
444-
echo -n "Is all of this correct: sftpuser = $sftpuser and domain = $dom (Y/N)? "
445-
read yn
446-
if [ "$yn" = "n" ] || [ "$yn" = "N" ]
447-
then
448-
exit
449-
fi
450-
if [ "$yn" = "y" ] || [ "$yn" = "Y" ]
451-
then
452-
/usr/bin/cat $tempfile > /usr/local/sbin/templates/$dom.txt
453-
/usr/bin/sed -i "s,replaceuser,$sftpuser,g" /usr/local/sbin/templates/$dom.txt
454-
/usr/bin/sed -i "s,replacedirectory,$dompath$dom,g" /usr/local/sbin/templates/$dom.txt
455-
/usr/bin/chown -R $sftpuser.apache $dompath$dom/html
456-
fi
457-
458-
## Make a backup of /etc/ssh/sshd_config
459-
460-
/usr/bin/rm -f /etc/ssh/sshd_config.bak
461-
462-
/usr/bin/cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
463-
464-
## Now append our new user information to the file
465-
466-
cat /usr/local/sbin/templates/$dom.txt >> /etc/ssh/sshd_config
467-
468-
## Restart sshd
469-
470-
/usr/bin/systemctl restart sshd
471-
472-
echo " "
473-
echo "Please check the status of sshd with systemctl status sshd."
474-
echo "You can verify that your information was added to the sshd_config by doing a more of the sshd_config"
475-
echo "A backup of the working sshd_config was created when this script was run: sshd_config.bak"
476-
```
477-
421+
```
478422

479423
### Final changes and script notes
480424

481425
!!! tip
482426

483427
If you take a look at either of the scripts above, you will note that you have changed the delimiter that `sed` uses by default from `/` to `,`. `sed` allows you to use any single-byte character as a delimiter. What you are searching for in the file has a bunch of "/" characters in it, and you would have had to escape each one (add a "\" in front of them) to search and replace these strings. Changing the delimiter makes this infinitely easier to do because it eliminates the need to do those escapes.
484428

485-
A couple of things to know about the script and about an SFTP change root in general. First, you prompt for the needed information and echo it back to the user for verification. The script bails and does nothing if you answer "N" to the confirmation question. The script for 8.5 makes a backup of `sshd_config` (`/etc/ssh/sshd_config.bak`) the way it was prior to our running of the script. The 8.6 or 9.0 script does the same for the `sftp_config` file (`/etc/ssh/sftp/sftp_config.bak`). In this way, if you make errors in an entry, you can restore the appropriate backup file and restart `sshd` to get things working again.
429+
A couple of things to know about the script and about an SFTP chroot in general. First, you prompt for the needed information and echo it back to the user for verification. The script bails and does nothing if you answer "N" to the confirmation question. The script for 8.5 makes a backup of `sshd_config` (`/etc/ssh/sshd_config.bak`) the way it was prior to our running of the script. The 8.6 or 9.0 script does the same for the `sftp_config` file (`/etc/ssh/sftp/sftp_config.bak`). In this way, if you make errors in an entry, you can restore the appropriate backup file and restart `sshd` to get things working again.
486430

487-
The SFTP change root requires that the path given in the `sshd_config` has root ownership. For this reason, you do not need the `html` directory added to the end of the path. Once the user is authenticated, the change root will switch the user's home directory, in this case the `../html` directory, to whichever domain you are entering. Your script has appropriately changed the owner of the `../html` directory to the sftpuser and the apache group.
431+
The SFTP chroot requires that the path given in the `sshd_config` has root ownership. For this reason, you do not need the `html` directory added to the end of the path. Once the user is authenticated, the chroot will switch the user's home directory, in this case the `../html` directory, to whichever domain you are entering. Your script has appropriately changed the owner of the `../html` directory to the sftpuser and the apache group.
488432

489433
!!! warning "Script Compatibility"
490434

@@ -512,7 +456,7 @@ If you *do* receive that message, the next thing is to test SFTP access. For eas
512456
* **Host:** sftp://hostname_or_IP_of_the_server
513457
* **Username:** (Example: myfixed)
514458
* **Password:** (the password of the SFTP user)
515-
* **Port:** (You should not need to enter one, if you use SSH and SFTP on the default port 22)
459+
* **Port:** If you use SSH and SFTP on the default port 22, enter that port
516460

517461
Once filled in, you can click the "Quickconnect" (Filezilla) button and you will connect to the `../html` directory of the appropriate site. Double-click on the "html" directory to put yourself inside it and try to drop a file into the directory. If you are successful, everything is working correctly.
518462

0 commit comments

Comments
 (0)