I have an array of users who need to just upload files to their set homedirs. I think sftp would suffice, but I don’t want them to login via shell. So is it possible?
My platform is centos 7, user’s homedirs are stored lets say /personal/$user
I created user with these settings
useradd -m -d /personal/user1 -s /sbin/nologin
assigned user a passwd, then when I use sftp to login to the machine, it says cannot connect.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I like the following setup for managing SSH access, which I’ve used to manage a group of users on small fleets of servers. Security and ease of management is high on the list of my priorities.
Its key features are easily managing SSH rights through Unix group membership, having tightly defined permissions, and being secure by default.
Setting up
Install software (optional but useful):
yum install members # or apt install members
Add groups:
addgroup --system allowssh addgroup --system sftponly
In /etc/ssh/sshd_config, ensure that the following to settings are No:
PermitRootLogin no PubkeyAuthentication no PasswordAuthentication no
And at the end of /etc/ssh/sshd_config, add these two stanzas:
Match Group allowssh
PubkeyAuthentication yes
Match Group sftponly
ChrootDirectory %h
DisableForwarding yes
ForceCommand internal-sftp
(don’t forget to restart SSH after editing the file)
Explanation
So, what does all this do?
- It always disables root logins, as an extra security measure.
- It always disables password-based logins (weak passwords are a big risk for servers running sshd).
- It only allows (pubkey) login for users in the
allowsshgroup. - Users in the
sftponlygroup cannot get a shell over SSH, only SFTP.
Managing who has access is then simply done by managing group membership (group membership changes take effect immediately, no SSH restart required; but note that existing sessions are not affected). members allowssh will show all users that are allowed to log in over SSH, and members sftponly will show all users that are limited to SFTP.
# adduser marcelm allowssh # members allowssh marcelm # deluser marcelm allowssh # members allowssh #
Note that your sftp users need to be members of both sftponly (to ensure they won’t get a shell), and of allowssh (to allow login in the first place).
Further information
-
Please note that this configuration does not allow password logins; all accounts need to use public key authentication. This is probably the single biggest security win you can get with SSH, so I argue it’s worth the effort even if you have to start now.
If you really don’t want this, then also add
PasswordAuthentication yesto theMatch Group allowsshstanza. This will allow both pubkey and password auth forallowsshusers. Alternatively, you can add another group (andMatch Groupstanza) to selectively grant users password-based logins. -
This configuration limits any
sftponlyuser to their home directory. If you do not want that, remove theChrootDirectory %hdirective.If you do want the chrooting to work, it’s important that the user’s home directory (and any directory above it) is owned by
root:rootand not writable by group/other. It’s OK for subdirectories of the home directory to be user-owned and/or writable.Yes, the user’s home directory must be root-owned and unwritable to the user. Sadly, there are good reasons for this limitation. Depending on your situation,
ChrootDirectory /homemight be a good alternative. -
Setting the shell of the
sftponlyusers to/sbin/nologinis neither necessary nor harmful for this solution, because SSH’sForceCommand internal-sftpoverrides the user’s shell.Using
/sbin/nologinmay be helpful to stop them logging in via other ways (physical console, samba, etc) though. -
This setup does not allow direct
rootlogins over SSH; this forms an extra layer of security. If you really do need direct root logins, change thePermitRootLogindirective. Consider setting it toforced-commands-only,prohibit-password, and (as a last resort)yes. -
For bonus points, have a look at restricting who can
suto root; add a system group calledwheel, and add/enableauth required pam_wheel.soin/etc/pam.d/su.
Method 2
Edit your /etc/ssh/sshd_config to contain:
Match User [SFTP user] ForceCommand internal-sftp
Restart sshd. If you have multiple users put them all on the match user line separated by commas like so:
Match User User1,User2,User3
The key to configuring sftp to not allow shell access is to limit users via the ForceCommand option.
Method 3
just change their default shell to /sbin/nologin. Assuming most varieties of Linux:
# usermod -s /sbin/nologin username
Method 4
you can use tftp.
anything over ssh will require a some auth (key|pass).
while tftp can be secured, it may be worth revisiting the decision to provide access to anything without authentication.
http://manpages.ubuntu.com/manpages/bionic/man1/tftp.1.html
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0