This seems simple, but how do I restrict user to their home?
All the users I am creating are just for their sites, so their home dir is /srv/www/domain.tld, but they can go anywhere they want and view everything.
Thanks
11 Replies
Try this.
With SFTP, the user just needs access to their data, since the SFTP server is embedded within the SSH server. However, just chrooting and running bash will present a number of challenges, since the user won't have access to /bin, /etc, /lib, /usr/bin, /usr/lib, etc, etc. This means they won't be able to use things like /bin/ls or be able to map uids to usernames via /etc/passwd.
Something like
I modified /etc/ssh/sshd_config
I added the group filetransfer
I then did the usermod -G filetransfer username on all 7 usernames.
Now here's where I varied from the instructions. Since they already had the home dirs, I skipped chown'ing root:root to the home/username, and skipped making the docs and public_html folders (again, since they already had home dirs).
So I went on to the last step
chown username:username * for each username in their respective home directories.
With all that said, I can successfully login as root in shell, and I can also login to sftp using the root username and password, however when the username/password is sent for any other user, FileZilla returns the following:
Error: Server unexpectedly closed network connection
Error: Could not connect to server
So thats where I am at. It seems it is blocking the usernames from logging into anything… I will say this. I went back and looked at the group setups, and some didn't look like, so I modified all the 7 users to ONLY be in group filetransfer. Any others that were in a group that matched their username, I simply removed them from that group… was that my mistake?
Worked like a charm. Ive learned that:
chown'ing root /home/username != chown'ing /hom/username/*
Thanks!
User (loj in this case) was only in filetransfer group, so I added their username to the group that is named the same as their username (loj:loj)
I tried it both with filetransfer and loj as their "primary" group
Still cannot write. Only root can write at this moment.
You need to create a subdirectory, and make the user owner of that subdirectory.
He'll never be able to write to the "top" directory he's chrooted into.
If you're doing web hosting, a
/site1.com
/site2.com
/private_files
layout may be good.
Also, mind, these must be real directories, not sym-links to outside of the chroot. Latter won't work. You'll need to point your web server config to the websites which reside in subdirs of your users' homedirs.
Their directories are /srv/www/legionofjeepers.com and /srv/www/teamslow.com
Following the guide, I first did
chown root:root /srv/www/legionofjeepers.com
chown root:root /srv/www/teamslow.com
Then
chown loj:loj /srv/www/legiomofjeepers.com/*
chown teamslow:teamslow /srv/www/teamslow.com/*
There are various other folders within those directories, such as the public_html folder for storing the site, log folders etc… Should I have done this differently? I have done the same thing to all 7 of my hosted sites and their users/home dirs.
Edit: After posting this, I re-read your reply and did confirm user loj could write to /public_html, /log etc… if I wanted them to be able to write to the root of /srv/www/legionofjeepers.com, would I then need to just do
chown root:root /srv/www
chown loj:loj /srv/www/legionofjeepers.com
chown teamslow:teamslow /srv/www/teamslow.com
???
1. The folder you're using as the chroot must be owned by root. If this is not the case, you will not be able to connect at all with sftp for that user.
2. There must be a folder inside the chroot owned by the user who is being jailed. This is because the user cannot create files inside a folder owned by root. If this is not the case, you will be able to connect with sftp, but the user won't actually be able to do anything useful.
So, if you want to keep your two domains separate, I recommend a setup like this:
srv/
srv/legionofjeepers.com/
srv/legionofjeepers.com/public_html/
srv/teamslow.com/
srv/teamslow.com/public_html/
Each user's home directory should be "srv/legionofjeepers.com" and "srv/teamslow.com" respectively, and the should be jailed to those directories. Chown root to those two, and then chown the publichtml files to their respective owners. Then setup your web server to point to the publichtml files.
That way each user sftp's into their own contained folder, puts their site in the public_html folder, and it's served from there.
My last question regarding this… I suppose you can only have one user with write access to the directories (excluding root user of course), correct?
/srv/userA/site1.com
/srv/userA/site2.com
/srv/userA/private # for non-site files
/srv/userB/site3.com
/srv/userB/site4.com
/srv/userB/private # for non-site files
but it's a matter of preference.
You can semi-easily have one user have access to all those sites - don't chroot him (or chroot to /srv), make sure he belongs to all the others' "user groups" (by default each user has its own group) and make sure all the files in the site subdirs are group-writeable. I'm not sure if you can force umask 002 in SFTP, if not, a cronjob (or manual chmod -R g+w by root) might work.
(edit: Ack, 11pm grammar)