Pretty long title for a pretty long work, which took me more than initially thought. And because I’ve sorted out blending multiple info from multiple sites, here we go with a unified post.
Let’s start with the goal.
I wanted to have root access to my home machine via SSH/SFTP with a strong authentication system; but I also wanted to offer to a friend of mine an access to an externally connected hard drive with a simple password.
And to keep everything more secure, I wanted to have this guy chrooted into the directory he can login.
I will not cover the strong authentication setup since there are very good instructions on their site.
To enable the strong authentication only for root, I had to modify a little bit my /etc/ssh/sshd_config file as shown below.
- disable PAM integration, by putting a hash at the beginning of the line:
# UsePAM yes
This is needed since we’re going to use the Match Group directive
- inserted the following lines below the Subsystem sftp /usr/lib/openssh/sftp-server section
Match Group root
Save and exit, restart the ssh service and test that if you try to ssh the system, after you type in root username and the password something appears similar to what reported below:
$ ssh email@example.com firstname.lastname@example.org's password: Duo two-factor login for root
Enter a passcode or select one of the following options:
1. Duo Push to +XX XXX XXX 1791 2. Phone call to +XX XXX XXX 1791 3. SMS passcodes to +XX XXX XXX 1791
Passcode or option (1-3):
Once you choose (for example) 1 and confirm on your authentication device, login will complete.
To enable chromed access for my friend without forcing him to enroll to strong auth, I have created an sftp group with the command:
Then I have him to this group with the command:
usermod -G sftp <login name>
I have also disabled his shell with the command:
usermod -s /bin/bash
and set his home directory to my external disk with the command:
usermod -d /media/external/friend
Finally I have created the following entries for sftp in /etc/ssh/sshd_config file under the Subsystem sftp /usr/lib/openssh/sftp-server section as shown below.
Match Group sftp ChrootDirectory /media/external/friend AllowTCPForwarding no X11Forwarding no ForceCommand internal-sftp
NOTABENE: the directory friend must be owned by root with 700 rights. Because my friend is part of the sftp group, to allow him to upload content I needed to create a directory upload below the directory friend and had to chown such directory to his login name as shown below:
If you want to have some more background info about why you need to change ownership and set rights are mentioned, check here.
Once you complete all the editing, remember to restart the ssh service with the command
service ssh restart