为什么当 运行 作为不同用户执行命令时性能会急剧下降

why drastic performance degradation when running a command as different user

我有一个你好世界C例子./a.out

现在我使用 time 测量以下命令的执行时间

time  ./a.out
Hello World
real    0m0.001s
user    0m0.000s
sys     0m0.002s

time runuser -l root -c './a.out'
real    0m0.017s
user    0m0.004s
sys     0m0.011s

time su -s /bin/bash -c "./a.out" root 
Hello World
real    0m0.080s ---> 80 times slower
user    0m0.005s
sys     0m0.071s

为什么第三个命令比第一个命令慢 80 倍?

环境 -- Redhat 7

对于第二个和第三个命令,time 命令还会对 runusersubash 的启动进行计时,这也需要一些时间。

如果你这样做应该不会有太大区别:

$ runuser -l root -c 'time ./a.out'

和:

$ su -s /bin/bash -c "time ./a.out" root