This question already has an answer here:
这个问题在这里已有答案:
- Changing to root user inside shell script 3 answers
- 在shell脚本3中更改为root用户答案
In one section of a bash script, I need to ssh into a remote box, change to root, then deploy an rpm. My code is getting stuck after it changes to root and I'm not sure why. Any help would be greatly appreciated. Ideally I would like it to ssh in, switch to root, deploy the rpm, then exit the ssh session.
在bash脚本的一个部分中,我需要ssh到一个远程框中,更改为root,然后部署rpm。我的代码在更改为root后卡住了,我不知道为什么。任何帮助将不胜感激。理想情况下,我希望它ssh in,切换到root,部署rpm,然后退出ssh会话。
user="user"
host="hostname"
echo "Enter password: "
read -s pw
ssh -t "$user"@"$host" "sudo su; $pw; rpm -Uvh rpmtodeploy.rpm;"
This is what I get currently:
这是我目前得到的:
user@localhost:~$ bash rpm.sh
user @ localhost:〜$ bash rpm.sh
Enter password:
输入密码:
[root@hostname /home/user]#
[root @ hostname / home / user]#
1 个解决方案
#1
1
Unless you background a command, bash waits for it to exit before executing the next command. Since su
is spawning an interactive shell, it will continue to run until you close it. You should use sudo directly:
除非你后台命令,否则bash在执行下一个命令之前等待它退出。由于su正在生成一个交互式shell,它将继续运行直到你关闭它。你应该直接使用sudo:
sudo rpm -Uvh rpmtodeploy.rpm
If you have sudo
access to su
but not to rpm
, use the -c
option:
如果你有su访问su而不是rpm,请使用-c选项:
sudo su -c 'rpm -Uvh rpmtodeploy.rpm'
#1
1
Unless you background a command, bash waits for it to exit before executing the next command. Since su
is spawning an interactive shell, it will continue to run until you close it. You should use sudo directly:
除非你后台命令,否则bash在执行下一个命令之前等待它退出。由于su正在生成一个交互式shell,它将继续运行直到你关闭它。你应该直接使用sudo:
sudo rpm -Uvh rpmtodeploy.rpm
If you have sudo
access to su
but not to rpm
, use the -c
option:
如果你有su访问su而不是rpm,请使用-c选项:
sudo su -c 'rpm -Uvh rpmtodeploy.rpm'