# SFTP-auth-pubkey

现有两台主机A、B。主机A使用用户名sftp-users通过sftp登录主机B上传文件。

配置公钥登录可以按照如下流程来操作：

1. 主机A生成公私钥对`id_rsa、id_rsa.pub` ｜可用命令`ssh-keygen -t rsa`生成
2. 主机B在sftp-users用户的主页目录下创建`~/.ssh/authorized_keys`文件
3. 主机B将主机A生成的`id_rsa.pub`的内容拷贝到上诉文件中，并将authorized\_keys文件owner属性设定为sftp-users，权限设定为600。｜可以尝试使用 `ssh-copy-id`进行密钥拷贝。
4. 主机A使用命令sftp登录主机B进行测试。

如果设定为仅密钥登录，主机B则可以在sshd\_config配置文件中屏蔽密码登录：

```bash
PasswordAuthentication yes

Match Group sftpusers
        ChrootDirectory /sftp/%u
        PasswordAuthentication no
```

使用`Match Group`可让某些选项只对该用户组生效，上诉配置设定了除了`sftpusers`用户组外，其他用户都可以用密码登录。

Debug小注解：如果使用公钥登录失败首先查看sftp-users的home目录是否设定正确，可通过/etc/passwd查看，如不正确可使用`usermod -d /sftp/sftpusers/home sftpusers`修改，必须要保证`.ssh/authorized_keys`的上级目录为sftp-users的home目录。示例中的认证文件绝对路径为`/sftp/sftpusers/home/.ssh/authorized_keys`

参考文档：

<https://wiki.archlinux.org/index.php/SFTP_chroot>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.junyangz.com/ops/sftp-auth-pubkey.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
