linux 无法在同一sudo会话中执行命令[重复]

y0u0uwnf  于 2023-08-03  发布在  Linux
关注(0)|答案(1)|浏览(115)

此问题在此处已有答案

Why can't I use 'sudo su' within a shell script? How to make a shell script run with sudo automatically(6个答案)
How to sudo su; then run command(4个答案)
Why can't I use 'sudo su' within a shell script? How to make a shell script run with sudo automatically(1个答案)
Run sudo -s inside shell script(4个答案)
4天前关闭。
无法在用户的同一会话中执行以下触摸命令- weblogic。相反,下面的touch命令正在用户-djaiswa 2上执行,并且失败[因为它试图访问所有者为weblogic的路径]。

[djaiswa2@cdcesb01 tmp]$ cat test.sh
#!/bin/bash
sudo su - weblogic;

touch /opt/middleware/.ssh/authorized_keys;
chmod 755 /opt/middleware/.ssh/authorized_keys;

[djaiswa2@cdcesb01 tmp]$ sh -x test.sh
+ sudo su - weblogic
Last login: Thu Aug 26 00:38:06 EDT 2021 on pts/0

-bash-4.2$ exit
logout
+ touch /opt/middleware/.ssh/authorized_keys
touch: cannot touch ‘/opt/middleware/.ssh/authorized_keys’: Permission denied
+ chmod 755 /opt/middleware/.ssh/authorized_keys
chmod: cannot access ‘/opt/middleware/.ssh/authorized_keys’: No such file or directory

字符串

ha5z0ras

ha5z0ras1#

您需要使用su命令和-c选项来指定要执行的命令。
例如:

sudo su - weblogic -c "touch /opt/middleware/.ssh/authorized_keys; chmod +x /opt/middleware/.ssh/authorized_keys"

字符串

相关问题