[英]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.