我希望能够使用 SFTP 来编辑需要 root 权限的文件。
我正在使用基于 SSH 密钥的身份验证 - 智能卡上的 rsa 密钥。
如果系统需要 sudo 来执行根级命令,我该如何解决这个问题?
我可以创建一种仅针对 SFTP 绕过 sudo 的方法吗?
有没有办法保持 sudo 和密钥身份验证。
我正在使用 Windows 连接到 Ubuntu。我也需要它与连接到 Ubuntu 的 Mac 一起工作。
我了解如何使用 SSH 隧道来管理系统服务。目前,我直接使用root用户登录,但密码登录被禁用。我不明白如何同时使用 sudo 和 SFTP。最好的做法是要求以非 root 用户身份登录,然后要求使用 sudo,因为日志将记录谁被授予每个命令的升级权限。
在使用基于密钥的身份验证时,我是否应该关注这一点,或者这是安全/日志记录方面的微不足道的区别?似乎基于密钥的身份验证在日志中记录了用户的序列号,并且您可以为 root 用户设置多个密钥来识别每个用户。这似乎与对我使用 sudo 的效果相同。我错了吗?
小智 17
用 sudo 调用子系统对我有用。
以 Ubuntu 主机为例:
sftp -s "sudo /usr/lib/openssh/sftp-server" targethost.fqdn
Run Code Online (Sandbox Code Playgroud)
SFTP 是命令访问文件操作,受您使用的帐户的限制。您必须使用 ssh 进行更多管理操作,从而无法同时使用 sudo 和 SFTP。如果您需要使用 SFTP 不受限制地访问整个磁盘,请使用 root 帐户执行此操作。无论如何,您可以同时在 sftp 和 ssh 上使用 root 登录,当然,使用两个不同的会话。
安全键提高了安全性并使日志记录更容易,不需要键盘输入。仅有助于登录,您可以为每个帐户用户设置多个密码并具有相同的效果。
编辑:我忘记了:如果您将用户 ID 指定为 0,您可以创建另一个与 root 具有相同效果的帐户,但没有任何意义,同样危险。如果有人尝试像 root 一样登录,可能会造成一些混淆,但除此之外,没有多大意义。
除了@MartinVonWittich 在上面的评论中建议的内容之外,您还可以为此活动设置一个专用的 SSH 密钥对,并将它们添加到 root 用户的/root/.ssh/authorized_keys
文件中,将它们的范围限制为一个命令。
# User backup's $HOME/.ssh/authorized_keys file
command="/usr/libexec/openssh/sftp-server" ssh-dss AAAAC8ghi9ldw== backup@host
Run Code Online (Sandbox Code Playgroud)
这将允许具有该对对应密钥的另一个系统以 root 身份通过 SFTP 进入该系统。您仍然会在您的syslog
和/或secure.log
文件中记录此连接(假设您的发行版提供此级别的日志记录)。
注意:使用此方法访问服务器的任何人都将拥有全权访问权限,因此请明智地使用它。最好继续阅读并将此功能与 chroot 和只读访问结合起来,以构建更严格的限制和以 root 身份对特定位置的有针对性的访问。
您可以在此处利用的另一种技术是限制 SFTP 连接,以便根据使用的 SSH 密钥将其以 root 身份进入特定位置。有关更多详细信息,请参阅我对此 U&L 问答的回答:“使用 SFTP 限制无密码备份”。
您还可以sftp-server
通过其开关-R
和 进行控制-d
。
-d start_directory
specifies an alternate starting directory for users. The pathname
may contain the following tokens that are expanded at runtime: %%
is replaced by a literal '%', %h is replaced by the home directory
of the user being authenticated, and %u is replaced by the user?
name of that user. The default is to use the user's home
directory. This option is useful in conjunction with the
sshd_config(5) ChrootDirectory option.
-R Places this instance of sftp-server into a read-only mode.
Attempts to open files for writing, as well as other operations
that change the state of the filesystem, will be denied.
Run Code Online (Sandbox Code Playgroud)