su-和su --login有什么区别?


15

su的手册页:

For  backward  compatibility, su defaults to not change the current directory
and to only set the environment variables HOME and SHELL (plus USER and LOGNAME
if the target user is not root).  It is recommended to always use the 
--login option (instead of  its  shortcut -) to avoid side effects caused
by mixing environments.

...

-, -l, --login
    Start the shell as a login shell with an environment similar to a real login:

        o      clears all the environment variables except TERM

        o      initializes the environment variables HOME, SHELL, USER, LOGNAME, and PATH

        o      changes to the target user's home directory

        o      sets argv[0] of the shell to '-' in order to make the shell a login shell

很难说出-和之间--login(或据称只是-l)之间是否存在任何差异。即,手册页显示为“而不是其快捷方式-”,但是所有这些选项都组合在一起,如果完全存在,我看不到差异的解释。

UPD我检查了问题,该问题应该可以解决我的问题。问题基本上是关于su和之间的区别su -。我问的是su -和之间的区别su --login。所以不,它根本无法解决。


4
我认为这句话写得不好。应该是To avoid side effects caused by mixing environments use --login option or its equivalent -. The first form is recommended because - can be placed only just before username.
jimmij

我不知道您对重复项的定义,但我不这样认为。您提出的问题基本上是关于su和之间的区别su -。我问的是su -和之间的区别su --login。我在这里想念什么?
x-yuri

Answers:


11

Debian的手动输入似乎更具启发性:

   -, -l, --login
       Provide an environment similar to what the user would expect had the user logged
       in directly.

       When - is used, it must be specified before any username. For portability it is
       recommended to use it as last option, before any username. The other forms (-l
       and --login) do not have this restriction.

我想说他们是相辅相成的。上游的一个建议避免使用-。说到哪个,为什么要“在任何用户名之前”?
x-yuri

7
“在任何用户名之前”可以改写为“在用户名之前,如果有的话”
Mark Plotnick

@MarkPlotnick您似乎错了
x-yuri

@ x-yuri整个讨论得出的结论是Mark Plotnick是正确的,并且手册充其量在逻辑上是模糊的。(尽管我们大多数人都没有注意到)
ctrl-alt-delor

I disagree, however, with the general point that, "before any username" can be rephrased as "before the username, if any." Perhaps Mr. Plotnik meant that the man page* should have rephrased the text using his suggestion, but the two phrases are not equivalent (as I explained at the beginning of this post).意思是,Mark Plotnick知道如何su工作,但是“在任何用户名之前”!=“在用户名之前(如果有)”。我认为这就是讨论得出的结论。
x-yuri

0

没有区别,就像跑步;

echo bob | grep -v bob
echo bob | grep --invert-match bob

只是速记...


3
我认为他要求澄清以避免混合环境引起的副作用。手册页的一部分。有什么副作用?
Zachary Brady

并不是的。副作用有或多或少清楚,他们没有使用任何的申请时--l--login。我的问题是为什么不建议使用它-,而不是使用-l , and --login`。
x-yuri

可移植性的问题是,如果您正在编写脚本,我根本不建议在脚本中使用su,但是可能会有用例,无论脚本中最可移植的选项是-l还是--login
mikejonesey

为什么su在编写脚本时根本不使用?
x-yuri

为什么不以拥有数据的用户身份编写脚本?最罕见的情况是,您需要以另一个用户的身份执行操作,然后出现sudo(这对使用的命令和参数有限制)。
mikejonesey '16
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.