You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* 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
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.
19
19
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.
21
21
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:
23
23
24
24
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.
26
26
3. The third part deals with setting up public/private key SSH access for your system administrators and turning off remote password-based authentication.
27
27
4. This document's fourth and last section deals with turning off remote root logins.
28
28
29
29
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).
30
30
31
-
!!! Note "<sup>1</sup> Change root jails for beginners:"
31
+
!!! Note "chroot jails for beginners:"
32
32
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.
34
34
35
35
From there on, that process or program can *only* access that folder and its subfolders.
36
36
37
-
!!! tip "Updates for Rocky Linux 8.6"
37
+
!!! tip "Updates for Rocky Linux 8.x and 9.x"
38
38
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.
40
40
41
41
## Part 1: General information
42
42
@@ -70,7 +70,7 @@ These are fictitious scenarios. Any resemblance to persons or sites that are rea
70
70
* Steve Simpson = ssimpson
71
71
* Laura Blakely = lblakely
72
72
73
-
## Part 2: SFTP change root jail
73
+
## Part 2: SFTP chroot jail
74
74
75
75
### Installation
76
76
@@ -84,7 +84,7 @@ dnf install openssh-server
84
84
85
85
#### Directories
86
86
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.
88
88
89
89
Creating the configuration directories:
90
90
@@ -319,7 +319,7 @@ Match User replaceuser
319
319
320
320
!!! note
321
321
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.
323
323
324
324
You want a directory for your user files that you will create from the template too:
#### 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!!
334
334
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`.
336
336
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:
338
338
339
-
To start with, create that directory:
340
-
341
-
```
342
-
mkdir /etc/ssh/sftp
343
-
```
339
+
```
340
+
mkdir /etc/ssh/sftp
341
+
```
344
342
345
-
Now make a backup copy of the `sshd_config`:
343
+
Now make a backup copy of the `sshd_config`:
346
344
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
+
```
350
348
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:
352
350
353
-
```bash
354
-
Include /etc/ssh/sftp/sftp_config
355
-
```
351
+
```bash
352
+
Include /etc/ssh/sftp/sftp_config
353
+
```
356
354
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.
358
356
359
-
```
360
-
vi /usr/local/sbin/webuser
361
-
```
357
+
```
358
+
vi /usr/local/sbin/webuser
359
+
```
362
360
363
-
And put this code in it:
361
+
And put this code in it:
364
362
365
-
```
366
-
#!/bin/bash
363
+
```
364
+
#!/bin/bash
367
365
# script to populate the SSHD configuration for web users.
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
+
```
478
422
479
423
### Final changes and script notes
480
424
481
425
!!! tip
482
426
483
427
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.
484
428
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.
486
430
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.
488
432
489
433
!!! warning "Script Compatibility"
490
434
@@ -512,7 +456,7 @@ If you *do* receive that message, the next thing is to test SFTP access. For eas
512
456
***Host:** sftp://hostname_or_IP_of_the_server
513
457
***Username:** (Example: myfixed)
514
458
***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
516
460
517
461
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.
0 commit comments