sudo和sudo -i访问function

sudo和sudo -i有什么特权区别?

随着sudo

  $sudo echo "search foo.bar.baz" >> /etc/resolv.conf bash: /etc/resolv.conf: Permission denied 

…和sudo -i

  $sudo -i #echo "search foo.bar.baz" >> /etc/resolv.conf 

…有用。 特权是:

  drwxr-xr-x 166 root root 12288 2009-10-17 21:02 . -rw-r--r-- 1 root root 42 2009-10-17 20:55 /etc/resolv.conf 

这些命令有不同的行为,让我吃了一惊,是什么原因导致sudo版本失败?

在第一个示例中,redirect发生在您当前的shell中,而不是在sudo子shell中。 所以sudo正在执行echo "search foo.bar.baz"并将结果返回给当前shell,然后shell将其写入/etc/resolv.conf

您可以通过直接调用bash作为sudo命令来使第一个示例工作:

 sudo bash -c "echo 'search foo.bar.baz' >> /etc/resolv.conf" 

使用sudo您可以使用1个具有pipe理员权限的命令。
sudo -i你用他自己的shell和环境variableslogin到root帐户。
否则,你可以使用sudo -s ,用它login到root帐户,但是你留在你的shell和variables中。

事情是,与sudo -i你可能会得到另一个壳和另一个$ PATH可以解决这个问题。

从sudo手册页:

 -i The -i (simulate initial login) option runs the shell specified in the passwd(5) entry of the user that the command is being run as. The command name argument given to the shell begins with a '-' to tell the shell to run as a login shell. sudo attempts to change to that user's home directory before running the shell. It also ini‐ tializes the environment, leaving TERM unchanged, setting HOME, SHELL, USER, LOGNAME, and PATH, and unsetting all other environment variables. Note that because the shell to use is determined before the sudoers file is parsed, a runas_default setting in sudoers will specify the user to run the shell as but will not affect which shell is actually run. 

你遇到的问题是shell只将sudo应用到你构build的pipe道的第一部分。 >> etc运行您的权限,而不是根。