简体   繁体   English

切换用户时,用于运行远程脚本的ssh命令在远程服务器上存在shell

[英]ssh command to run remote script exist shell on remote server when switching user

When I run a script such as this: 当我运行这样的脚本时:

ssh -t root@10.10.10.10 '/tmp/somescript.sh'

where the script is defined as: 脚本定义为:

#!/bin/sh
mkdir -p /data/workday/cred
chown -R myuser:myuser /data
su myuser -  # <------- NOTICE THIS ! ! ! ! 
rpm -Uvp --force --nodeps --prefix /data/place /data/RPMs/myrpm.rpm

Notice the above su command. 注意上面的su命令。

If I comment-out the su command, the script runs remotely and then my shell prompt returns to where I came from ( same server where I ran the ssh command above ) 如果我注释掉su命令,该脚本将远程运行,然后我的shell提示符将返回到我的来源(运行上述ssh命令的同一服务器)

But leaving the script as listed above, causes the script to complete successfully but the shell prompt stays on the remote server . 但是保留上面列出的脚本会导致脚本成功完成,但shell提示符仍停留在远程服务器上

How can I prevent that ? 我该如何预防呢? Making sure that the issuer of the rpm command is a different user than root just a listed ? 确保rpm命令的颁发者是不同于root用户(仅列出用户)的用户?

But leaving the script as listed above, causes the script to complete successfully but the shell prompt stays on the remote server. 但是保留上面列出的脚本会导致脚本成功完成,但Shell提示符仍停留在远程服务器上。

Not exactly. 不完全是。 The script is running up to the su command, which spawns a new subshell, and stopping there until you exit the shell. 该脚本正在运行至su命令,该命令会生成一个新的子Shell,并在那里停止直到您退出该Shell。 Until you exit that shell, the rpm command never runs, and when it does, it runs as root. 在退出该外壳之前, rpm命令永远不会运行,当运行时,它将以root用户身份运行。

If you want to run the rpm command as a non-root user, you'd need to do something a little different, like: 如果要以非root用户身份运行rpm命令,则需要做一些不同的事情,例如:

sudo -u myuser rpm -Uvp ...

在脚本末尾添加“退出”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Shell脚本中的远程ssh命令中传递变量 - Passing variables in remote ssh command in shell script 当我运行一个ssh到远程服务器的bash脚本并运行类似wget的命令时,它将保存在源服务器上而不是目标服务器上吗? - When I run a bash script that ssh's into a remote server and run a command like wget it saves on the source not the destination server? 如何使用perl脚本和ssh在远程linux服务器中运行bash或shell脚本 - How to run a bash or shell script in a remote linux server using perl script and ssh 如何在 shell 脚本中使用 ssh 在远程服务器上运行 sed(包括变量) - How to run sed on remote server using ssh inside a shell script(variables included) 使用配置文件的ssh命令在远程机器中执行shell脚本 - Execute shell script in remote machine using ssh command with config file 如何通过Shell脚本在远程服务器上连接和运行Shell脚本 - How to connect and run shell script on remote server, via shell script 当通过特定命令使用SSH时,为什么远程主机中没有Shell进程? - Why there is not a shell process in remote host when using SSH with specific command? ssh remote命令作为不同的用户 - ssh remote command as different user 在远程服务器上运行命令 - Run a command in remote server 通过代理服务器SSH到远程计算机以运行带有参数的脚本 - ssh to remote machine via proxy server to run a script with parameters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM