如何在一行命令中将密码应用于sudo并执行su root? [英] How can I apply password to sudo in one line command and execute su root?
问题描述
我已经创建了一个具有root特权的名为samX的用户(在visudo
中附加了"samX ALL =(ALL:ALL)ALL").
I've created an user named samX with root privilege (have appended "samX ALL=(ALL:ALL) ALL" in visudo
).
我正在尝试向sudo
施加密码,然后向su root
施加密码,并在一行命令中依次执行whoami
.我当前的命令如下,但是它报错: sudo:su root; whoami:找不到命令
I'm trying to apply password to sudo
, then su root
and execute whoami
sequentially in one line command. My current command is somewhat as follows, but it complains error: sudo: su root; whoami: command not found
echo 'CbEYKFKt' | sudo -S 'su root; whoami'
其中"CbEYKFKt"是用户samX的密码.
In which, 'CbEYKFKt' is the password for user samX.
反正有解决这个问题的方法吗?非常感谢.
Is there anyway to solve this problem? Thanks a lot.
推荐答案
您确实应该在sudoers文件中添加一行,例如
You should really add a line to your sudoers file such as
samX ALL=(ALL:ALL) NOPASSWD: /sbin/su
而不是将密码保存在bash历史记录或任何其他文件中.这样,根本不需要输入密码.
instead of saving your password in the bash history or any other file. With that, there is no need to enter the password at all.
如果要执行其他命令,请添加该命令.
If you want to perform a different command, add that.
这篇关于如何在一行命令中将密码应用于sudo并执行su root?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!