UNIX bash切换用户来回

时间:2017-08-24 04:07:05

标签: bash unix sudo

我测试是否可以以user1运行脚本,然后sudo as user2以用户身份执行脚本的一部分,然后再恢复为user1。

显然我的脚本在第一个sudo之后卡住了,并且没有执行后续的行。怎么解决这个问题?

道歉,因为我在shell脚本编写方面确实没有经验

#!/bin/bash
whoami
ls -lrt /myfolder
sudo -i -u user2
whoami

sftp ${othersvr} <<EOF
cd /tgtpath
lcd /myfolder
get -p *.txt
exit
EOF

sudo -i -u user1
whoami
ls -lrt /myfolder

1 个答案:

答案 0 :(得分:1)

你应该找到一种更好的方法来做你想做的事情,比如使用ACL或者什么。

但是,如果您想这样做,请考虑sudo可以接收命令

sudo -i -u user2 'whoami; ls -lrt /myfolder'
sudo -i -u user1 'whoami; ls -lrt /myfolder'
相关问题