Eagertolearn
Eagertolearn

Reputation: 11

Not able to execute the commands in same sudo session

Not able to execute below touch command in same session of user - weblogic. Instead below touch command is executing on user - djaiswa2 and is failing [Because it is trying to access the path whose owner is 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

Upvotes: 0

Views: 192

Answers (1)

Arnaud Valmary
Arnaud Valmary

Reputation: 2327

You need to use su command with -c option to specify the commands to be executed.

for exampe:

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

Upvotes: 1

Related Questions