在 QNAP 上部署 Gitea 并启用原生 SSH

安装好 Gitea 之后(安装过程略过,添加 https://www.myqnap.org/ 源后直接安装即可),正常情况下 SSH 是不工作的(即使你已经在 Gitea 中添加了相应的 SSH Key)。主要原因是 QNAP 默认并不支持非管理员账号登录 SSH。此时一般的解决方案是启用 Gitea 的自带 SSH 服务(通常会开在另一个非 22 的端口上),但这种解决方法实在丑陋。大概研究了一下,找到了一个更优雅的解决方案。

总的来说,QNAP 上存储 sshd 服务的配置文件在 /etc/config/ssh/sshd_config 中,其中的 AllowUsers 配置项决定了哪些用户可以通过 SSH 登录。默认情况下,这个配置项只包含了管理员账号,但你直接修改这个配置文件是不生效的,原因是控制 sshd 服务的脚本 /etc/init.d/login.sh 在启动时会覆盖这个配置文件。因此这里需要做两件事情

  1. 修改 /etc/config/ssh/sshd_config 文件,添加 giteaAllowUsers 中。
  2. 修改 /etc/init.d/login.sh 代码,把 sbin/mksshdconf 命令注释掉,这样这个脚本就不会覆盖我们修改的配置文件
1
2
3
4
5
6
7
8
9
10
11
--- /etc/init.d/login.sh.bak
+++ /etc/init.d/login.sh
@@ -82,7 +82,7 @@

update_sshd_config()
{
- /sbin/mksshdconf
+ #/sbin/mksshdconf
ENABLED_SFTP=`/sbin/getcfg LOGIN "SFTP Enable" -u -d TRUE`

if [ "x${ENABLED_SFTP}" = "xTRUE" ]; then

完成上面两步,并通过 /etc/init.d/login.sh restart 重启登录服务,你会发现还是登录不了

1
2
3
❯ ssh gitea@nas.lan
PTY allocation request failed on channel 0
Connection to nas.lan closed.

原因是 /etc/passwd 中的 gitea 用户设置的登录shell是 /bin/false。虽然根据标准来说,/bin/false 应该是合法的 shell,并且QNAP实际也有 /bin/false 这个文件,但QNAP的 sshd 似乎必须要你设置一个可以执行命令的shell才能工作,所以还需要把 gitea 用户的 shell 改成 /bin/sh 或者 /bin/bash

1
2
3
4
5
--- /etc/passwd.bak
+++ /etc/passwd
@@ -11,4 +11,4 @@
-gitea:x:500:500:gitea:/tmp:/bin/false
+gitea:x:500:500:gitea:/tmp:/bin/sh

请注意上述的uuid可能有所不同

另外,由于 gitea 会主动在 authorized_keys 中添加相应的 command,因此也不需要担心改成 /bin/sh 之后会真的拿到 shell

1
2
3
4
5
❯ ssh gitea@nas.lan
PTY allocation request failed on channel 0
Hi there, KaitoHH! You've successfully authenticated with the key named ed25519, but Gitea does not provide shell access.
If this is unexpected, please log in with password and setup Gitea under another user.
Connection to nas.lan closed.

完成!成品 NAS 大家还是要慎用啊,谁知道厂商冷不丁下次又给你搞个什么东西。

在 QNAP 上部署 Gitea 并启用原生 SSH

https://kaitohh.com/deploy-gitea-with-native-ssh-on-qnap/

作者

KaitoHH

发布于

2025-07-22

更新于

2025-07-22

许可协议

评论