问题原文
原帖链接 · jma · CC BY-SA 4.0
I have a linux host with openssh that permits users to login with public key.
AuthenticationMethods publickey
But now I have some users who will need to access the host as a file drop using sftp. In addition, these users will user passwords rather than key pairs.
I make all of these users belong to the group sftp_users.
[email omitted]:~$ id
uid=1005(an_sftp_user) gid=1007(an_sftp_user) groups=1007(an_sftp_user),1006(sftp_users)
[email omitted]:~$ groups
an_sftp_user sftp_users
[email omitted]:~$
Now I create the file /etc/ssh/sshd_config.d/sftp_users.conf owned by root, 0644:
Match Group sftp_users
X11Forwarding no
AllowTcpForwarding no
AuthorizedKeysCommand /bin/true
AuthenticationMethods keyboard-interactive
PasswordAuthentication yes
ChrootDirectory /sftp-incoming
What I want this to do is to permit users in the group sftp_users to be able to connect with password and access /sftp-incoming.
What actually happens is that I see a message server side in auth.log that says merely that the connection was closed and, client side, I see (using sftp -v) that it tries the certificates it finds in $HOME/.ssh (I've disabled my ssh-agent for these tests) but never tries password login.
Indeed, one line is this:
debug1: Authentications that can continue: publickey
So somehow my Match is not being satisfied or not being seen. But I'm unclear why.
Any suggestions where I've gone wrong?
Update
The solution from @Gogowitsch largely answers this, especially the note that sftp-internal is, well, internal to openssh and so doesn't need a binary to be available in my chroot jail. Also important was to discover that chroot requires that the chain of directories be 0755 root all the way down, so I had to sort some permissions.
His suggestion to put the config all in the one sshd_config file was also helpful. Indeed, once it was working, moving that block back to a config file and using Include to include it fails without error. The block simply isn't used. I'm using 8.2:
OpenSSH_8.2p1 Ubuntu-4ubuntu0.1, OpenSSL 1.1.1f 31 Mar 2020
which claims to support Include (present since 7.3p1 and apparently committed on 1 Feb 2020 (prior to 31 Mar 2020).
已采纳解答
原帖链接 · Gogowitsch · CC BY-SA 4.0
I see these immediate issues:
- The order of directives. Try putting the
Match Group sftp_usersat the very end of the file/etc/ssh/sshd_serverinstead of a file in thesshd_server.d. This is because the lastAuthenticationMethodsseems to win. - The
AuthenticationMethodsshould containpassword. This is apparently what allows the password login. - When using the
ChrootDirectorydirective, you also have to use the following line within yourMatch Group sftp_usersblock:
This is because theForceCommand internal-sftp/sftp-incomingbecomes the actual root of the file system to all commands. By default, the SSH server attempts to run a binary such as/usr/lib/openssh/sftp-server. From the perspective of the chroot'ed filesystem, there probably isn't any such file.
Potentially, you'll also have to run the following command to make sure that the /sftp-incoming directory cannot be accessed by 'other' (the last digit in the octal 0750):
chmod 0750 /sftp-incoming
As an experimental setup, I used the following Dockerfile:
FROM ubuntu
RUN apt-get update && \
apt-get install -yq openssh-sftp-server openssh-server
# Create two users, first has no password, second has the password "1".
# This will also create the home directory for the sftp user.
RUN echo | adduser only-passwordless-user && \
echo | adduser only-with-password-user --home /sftp-incoming/only-with-password-user && \
echo only-with-password-user:1 | chpasswd && \
addgroup sftp_users && \
adduser only-with-password-user sftp_users
# These are needed so that sshd is happy. Otherwise,
# it likes to say “bad ownership or modes for chroot directory”
RUN mkdir -p -m0755 /var/run/sshd && \
chown root:root /sftp-incoming && \
chmod 0750 /sftp-incoming
RUN echo AuthenticationMethods publickey >> /etc/ssh/sshd_config && \
printf "Match Group sftp_users \n\
ForceCommand internal-sftp \n\
X11Forwarding no \n\
AllowTcpForwarding no \n\
AuthorizedKeysCommand /bin/true \n\
AuthenticationMethods keyboard-interactive password \n\
PasswordAuthentication yes \n\
ChrootDirectory /sftp-incoming \n\
" >> /etc/ssh/sshd_config # cannot be ".d/sftp_users.conf"
# Start a single-connection debug server.
# Remove the -d parameter to make it long-lived but quiet.
ENTRYPOINT /usr/sbin/sshd -d
You can start a sshd running on port 2200 using this command:
docker build . -t ssh-test && docker run -it --name ssh-test -p2200:22 --rm ssh-test bash
To connect to it using sftp, run this:
sshpass -p1 sftp -P 2200 [email omitted]
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
