Files
2026-07-23 20:36:13 +08:00

65 lines
9.6 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
## [群晖 sudo 免密码配置](https://blog.csdn.net/e891377/article/details/132848703)
https://blog.csdn.net/e891377/article/details/132848703
https://www.cnblogs.com/ourener/p/19067474
在群晖 NAS 上配置 **sudo 免密码**,可以避免每次执行需要管理员权限的命令时输入密码,从而提升运维效率。以下步骤基于 SSH 登录和 _sudoers_ 配置完成。
步骤 1:开启 SSH 并获取 root 权限
- 登录群晖 **控制面板 → 终端机和 SNMP**,勾选 **启动 SSH 功能**
- 使用 SSH 工具(如 FinalShell、PuTTY)连接群晖:
ssh 用户名@群晖IP
![复制](data:image/svg+xml,%EF%BB%BF%3Csvg%20width%3D%2218%22%20height%3D%2219%22%20viewBox%3D%220%200%2018%2019%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%0D%0A%20%20%20%20%3Cpath%20d%3D%22M4.11363%203.54174L4.11153%205.16645V13.2054C4.11153%2014.579%205.19925%2015.6926%206.54102%2015.6926L12.982%2015.6929C12.751%2016.3615%2012.1281%2016.8406%2011.3958%2016.8406H6.54102C4.57998%2016.8406%202.99023%2015.2131%202.99023%2013.2054V5.16645C2.99023%204.41591%203.45927%203.77752%204.11363%203.54174ZM13.2688%201.53125C14.1977%201.53125%2014.9508%202.30219%2014.9508%203.25319V13.2022C14.9508%2014.1531%2014.1977%2014.9241%2013.2688%2014.9241H6.54102C5.6121%2014.9241%204.85907%2014.1531%204.85907%2013.2022V3.25319C4.85907%202.30219%205.6121%201.53125%206.54102%201.53125H13.2688ZM13.2688%202.67921H6.54102C6.23138%202.67921%205.98037%202.93619%205.98037%203.25319V13.2022C5.98037%2013.5192%206.23138%2013.7761%206.54102%2013.7761H13.2688C13.5784%2013.7761%2013.8295%2013.5192%2013.8295%2013.2022V3.25319C13.8295%202.93619%2013.5784%202.67921%2013.2688%202.67921Z%22%20fill%3D%22%23767676%22%20%2F%3E%0D%0A%3C%2Fsvg%3E)
- 切换到 root
sudo -i
![复制](data:image/svg+xml,%EF%BB%BF%3Csvg%20width%3D%2218%22%20height%3D%2219%22%20viewBox%3D%220%200%2018%2019%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%0D%0A%20%20%20%20%3Cpath%20d%3D%22M4.11363%203.54174L4.11153%205.16645V13.2054C4.11153%2014.579%205.19925%2015.6926%206.54102%2015.6926L12.982%2015.6929C12.751%2016.3615%2012.1281%2016.8406%2011.3958%2016.8406H6.54102C4.57998%2016.8406%202.99023%2015.2131%202.99023%2013.2054V5.16645C2.99023%204.41591%203.45927%203.77752%204.11363%203.54174ZM13.2688%201.53125C14.1977%201.53125%2014.9508%202.30219%2014.9508%203.25319V13.2022C14.9508%2014.1531%2014.1977%2014.9241%2013.2688%2014.9241H6.54102C5.6121%2014.9241%204.85907%2014.1531%204.85907%2013.2022V3.25319C4.85907%202.30219%205.6121%201.53125%206.54102%201.53125H13.2688ZM13.2688%202.67921H6.54102C6.23138%202.67921%205.98037%202.93619%205.98037%203.25319V13.2022C5.98037%2013.5192%206.23138%2013.7761%206.54102%2013.7761H13.2688C13.5784%2013.7761%2013.8295%2013.5192%2013.8295%2013.2022V3.25319C13.8295%202.93619%2013.5784%202.67921%2013.2688%202.67921Z%22%20fill%3D%22%23767676%22%20%2F%3E%0D%0A%3C%2Fsvg%3E)
步骤 2:编辑 sudoers 文件
- 打开 _sudoers_ 配置:
visudo
![复制](data:image/svg+xml,%EF%BB%BF%3Csvg%20width%3D%2218%22%20height%3D%2219%22%20viewBox%3D%220%200%2018%2019%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%0D%0A%20%20%20%20%3Cpath%20d%3D%22M4.11363%203.54174L4.11153%205.16645V13.2054C4.11153%2014.579%205.19925%2015.6926%206.54102%2015.6926L12.982%2015.6929C12.751%2016.3615%2012.1281%2016.8406%2011.3958%2016.8406H6.54102C4.57998%2016.8406%202.99023%2015.2131%202.99023%2013.2054V5.16645C2.99023%204.41591%203.45927%203.77752%204.11363%203.54174ZM13.2688%201.53125C14.1977%201.53125%2014.9508%202.30219%2014.9508%203.25319V13.2022C14.9508%2014.1531%2014.1977%2014.9241%2013.2688%2014.9241H6.54102C5.6121%2014.9241%204.85907%2014.1531%204.85907%2013.2022V3.25319C4.85907%202.30219%205.6121%201.53125%206.54102%201.53125H13.2688ZM13.2688%202.67921H6.54102C6.23138%202.67921%205.98037%202.93619%205.98037%203.25319V13.2022C5.98037%2013.5192%206.23138%2013.7761%206.54102%2013.7761H13.2688C13.5784%2013.7761%2013.8295%2013.5192%2013.8295%2013.2022V3.25319C13.8295%202.93619%2013.5784%202.67921%2013.2688%202.67921Z%22%20fill%3D%22%23767676%22%20%2F%3E%0D%0A%3C%2Fsvg%3E)
- 在文件末尾添加:
用户名 ALL=(ALL) NOPASSWD: ALL
![复制](data:image/svg+xml,%EF%BB%BF%3Csvg%20width%3D%2218%22%20height%3D%2219%22%20viewBox%3D%220%200%2018%2019%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%0D%0A%20%20%20%20%3Cpath%20d%3D%22M4.11363%203.54174L4.11153%205.16645V13.2054C4.11153%2014.579%205.19925%2015.6926%206.54102%2015.6926L12.982%2015.6929C12.751%2016.3615%2012.1281%2016.8406%2011.3958%2016.8406H6.54102C4.57998%2016.8406%202.99023%2015.2131%202.99023%2013.2054V5.16645C2.99023%204.41591%203.45927%203.77752%204.11363%203.54174ZM13.2688%201.53125C14.1977%201.53125%2014.9508%202.30219%2014.9508%203.25319V13.2022C14.9508%2014.1531%2014.1977%2014.9241%2013.2688%2014.9241H6.54102C5.6121%2014.9241%204.85907%2014.1531%204.85907%2013.2022V3.25319C4.85907%202.30219%205.6121%201.53125%206.54102%201.53125H13.2688ZM13.2688%202.67921H6.54102C6.23138%202.67921%205.98037%202.93619%205.98037%203.25319V13.2022C5.98037%2013.5192%206.23138%2013.7761%206.54102%2013.7761H13.2688C13.5784%2013.7761%2013.8295%2013.5192%2013.8295%2013.2022V3.25319C13.8295%202.93619%2013.5784%202.67921%2013.2688%202.67921Z%22%20fill%3D%22%23767676%22%20%2F%3E%0D%0A%3C%2Fsvg%3E)
步骤 3:保存并退出
- 在 _visudo_ 中按 **ESC** → 输入 _:wq_ 保存退出。
步骤 4:验证免密码效果
执行:
sudo ls /root
![复制](data:image/svg+xml,%EF%BB%BF%3Csvg%20width%3D%2218%22%20height%3D%2219%22%20viewBox%3D%220%200%2018%2019%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%0D%0A%20%20%20%20%3Cpath%20d%3D%22M4.11363%203.54174L4.11153%205.16645V13.2054C4.11153%2014.579%205.19925%2015.6926%206.54102%2015.6926L12.982%2015.6929C12.751%2016.3615%2012.1281%2016.8406%2011.3958%2016.8406H6.54102C4.57998%2016.8406%202.99023%2015.2131%202.99023%2013.2054V5.16645C2.99023%204.41591%203.45927%203.77752%204.11363%203.54174ZM13.2688%201.53125C14.1977%201.53125%2014.9508%202.30219%2014.9508%203.25319V13.2022C14.9508%2014.1531%2014.1977%2014.9241%2013.2688%2014.9241H6.54102C5.6121%2014.9241%204.85907%2014.1531%204.85907%2013.2022V3.25319C4.85907%202.30219%205.6121%201.53125%206.54102%201.53125H13.2688ZM13.2688%202.67921H6.54102C6.23138%202.67921%205.98037%202.93619%205.98037%203.25319V13.2022C5.98037%2013.5192%206.23138%2013.7761%206.54102%2013.7761H13.2688C13.5784%2013.7761%2013.8295%2013.5192%2013.8295%2013.2022V3.25319C13.8295%202.93619%2013.5784%202.67921%2013.2688%202.67921Z%22%20fill%3D%22%23767676%22%20%2F%3E%0D%0A%3C%2Fsvg%3E)
如果不再提示输入密码,则配置成功。
💡 **最佳实践与注意事项**
- 建议结合 **SSH 公钥免密登录**,进一步减少输入密码的次数。
- 修改 _/etc/ssh/sshd_config_ 确保启用:
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
![复制](data:image/svg+xml,%EF%BB%BF%3Csvg%20width%3D%2218%22%20height%3D%2219%22%20viewBox%3D%220%200%2018%2019%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%0D%0A%20%20%20%20%3Cpath%20d%3D%22M4.11363%203.54174L4.11153%205.16645V13.2054C4.11153%2014.579%205.19925%2015.6926%206.54102%2015.6926L12.982%2015.6929C12.751%2016.3615%2012.1281%2016.8406%2011.3958%2016.8406H6.54102C4.57998%2016.8406%202.99023%2015.2131%202.99023%2013.2054V5.16645C2.99023%204.41591%203.45927%203.77752%204.11363%203.54174ZM13.2688%201.53125C14.1977%201.53125%2014.9508%202.30219%2014.9508%203.25319V13.2022C14.9508%2014.1531%2014.1977%2014.9241%2013.2688%2014.9241H6.54102C5.6121%2014.9241%204.85907%2014.1531%204.85907%2013.2022V3.25319C4.85907%202.30219%205.6121%201.53125%206.54102%201.53125H13.2688ZM13.2688%202.67921H6.54102C6.23138%202.67921%205.98037%202.93619%205.98037%203.25319V13.2022C5.98037%2013.5192%206.23138%2013.7761%206.54102%2013.7761H13.2688C13.5784%2013.7761%2013.8295%2013.5192%2013.8295%2013.2022V3.25319C13.8295%202.93619%2013.5784%202.67921%2013.2688%202.67921Z%22%20fill%3D%22%23767676%22%20%2F%3E%0D%0A%3C%2Fsvg%3E)
- 权限必须正确,否则免密无效:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
![复制](data:image/svg+xml,%EF%BB%BF%3Csvg%20width%3D%2218%22%20height%3D%2219%22%20viewBox%3D%220%200%2018%2019%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%0D%0A%20%20%20%20%3Cpath%20d%3D%22M4.11363%203.54174L4.11153%205.16645V13.2054C4.11153%2014.579%205.19925%2015.6926%206.54102%2015.6926L12.982%2015.6929C12.751%2016.3615%2012.1281%2016.8406%2011.3958%2016.8406H6.54102C4.57998%2016.8406%202.99023%2015.2131%202.99023%2013.2054V5.16645C2.99023%204.41591%203.45927%203.77752%204.11363%203.54174ZM13.2688%201.53125C14.1977%201.53125%2014.9508%202.30219%2014.9508%203.25319V13.2022C14.9508%2014.1531%2014.1977%2014.9241%2013.2688%2014.9241H6.54102C5.6121%2014.9241%204.85907%2014.1531%204.85907%2013.2022V3.25319C4.85907%202.30219%205.6121%201.53125%206.54102%201.53125H13.2688ZM13.2688%202.67921H6.54102C6.23138%202.67921%205.98037%202.93619%205.98037%203.25319V13.2022C5.98037%2013.5192%206.23138%2013.7761%206.54102%2013.7761H13.2688C13.5784%2013.7761%2013.8295%2013.5192%2013.8295%2013.2022V3.25319C13.8295%202.93619%2013.5784%202.67921%2013.2688%202.67921Z%22%20fill%3D%22%23767676%22%20%2F%3E%0D%0A%3C%2Fsvg%3E)
- 出于安全考虑,仅对可信用户开启 _NOPASSWD_,并限制外网访问 SSH。
这样配置后,你在群晖上执行 _sudo_ 命令将无需输入密码,同时配合 SSH 公钥可实现全程免密管理。
验证:
使用某个用户登录后输入:
sudo -n true
没有提示就成功了,是:sudo: a password is required 没有成功
https://zhuanlan.zhihu.com/p/632737024