xiaoyanbot
V2EX  ›  问与答

单独使用 [sudo -s] 后面没有其他命令,会得到一个交互的 root 的 shell。请教下,-s 参数,是否会加载环境变量定义文件?

  •  
  •   xiaoyanbot · Oct 7, 2022 · 1642 views
    This topic created in 1320 days ago, the information mentioned may be changed or developed.

    -i 参数(即 sudo -i ),在 sudo 的 man page 里指明了,会加载 [.profile,.bash_profile or .login will be read by the shell.] , 同时呢,会跳到 /root 家目录。

    那使用 sudo -s 的时候,我是知道,不会跳转 /root 家目录,会留在当前目录。 我想问下, -s 参数 [ sudo -s ] 是否也会加载 [.profile,.bash_profile ] 这类环境变量定义文件呢? 在 man page 里没有说明,

    请教从哪里可以验证呢? 要去看源码吗?

    7 replies    2022-10-10 15:02:33 +08:00
    arch9999
        1
    arch9999  
       Oct 7, 2022
    eason1874
        2
    eason1874  
       Oct 7, 2022
    .profile 也是 shell 脚本文件,要验证你往里面加一行记录代码就好了,比如

    echo 'echo $(date) >> /root/logtest.txt' >> /root/.profile

    这样,每次被引用都会往 /root/logtest.txt 写一行时间
    pennai
        3
    pennai  
       Oct 7, 2022
    你在 profile 里 touch 新建文件或者像楼上 echo 试试不就行了
    xiaoyanbot
        4
    xiaoyanbot  
    OP
       Oct 8, 2022
    好滴,谢谢各位大佬的思路!
    julyclyde
        5
    julyclyde  
       Oct 8, 2022
    -s 不会执行 profile 类
    会执行 rc

    要 profile 的话需要 sudo -i
    xiaoyanbot
        6
    xiaoyanbot  
    OP
       Oct 10, 2022
    @julyclyde 请教下 : [不会执行 profile 类] , 指的是 不会加载 /etc/profile 吗?
    julyclyde
        7
    julyclyde  
       Oct 10, 2022
    @xiaoyanbot /etc/profile 和 ~/.bash_profile
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5722 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 70ms · UTC 02:20 · PVG 10:20 · LAX 19:20 · JFK 22:20
    ♥ Do have faith in what you're doing.