sudo cp -f ~/Download/foo.txt ~/Download/bar/
或其他命令并执行它(这不是以“cp”命令为中心的问题),那么从 sudo 执行一个命令还是从 root 执行一个命令有什么区别?因为建议如果没有必要就不要从 root 执行,所以,这是在运行更复杂指令的背景下的建议吗?
答案1
该sudo
命令本身并不意味着执行某项操作的权限,而是在命令执行期间更改您的用户 ID。如果不指定用户名或 ID,您将默认在短时间内成为“root”。因此,sudo....
以 root 身份执行或登录与执行命令是一回事。
从man sudo
DESCRIPTION sudo allows a permitted user to execute a command as the superuser or an‐ other user, as specified by the security policy. The invoking user's real (not effective) user-ID is used to determine the user name with which to query the security policy.
.....
-u user, --user=user Run the command as a user other than the default target user (usually root). The user may be either a user name or a nu‐ meric user-ID (UID) prefixed with the ‘#’ character (e.g., #0 for UID 0). When running commands as a UID, many shells re‐ quire that the ‘#’ be escaped with a backslash (‘\’). Some security policies may restrict UIDs to those listed in the password database. The sudoers policy allows UIDs that are not in the password database as long as the targetpw option is not set. Other security policies may not support this.