[Linux]远程SSH两级跳转用户sodu的问题--sudo: no tty present and no askpass program specified
业务场景描述:
我有100多组mysql db服务器,那么其中10台我需要建一个mysql用户下的文件,如果按照正常流程来看,我需要在跳转控制机dbcrol上面 ssh 到mysql 服务器上面,但是sudo进去的可不是mysql帐号,而是一个novaadmin帐号,我还需要执行sudo su - mysql命令跳转进入mysql用户模式下,才能执行mkdir -p /mysqlog/slow_log/a1.log。
1 我需要先测试一个在跳转控制机器dbcrol上面能远程执行ssh mkdir的命令。
[nova@dbcrol cacti]$ ssh db02703 "sudo su -u mysql 'mkdir /mysqllog/log/a1.log'"
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: 3 incorrect password attempts
[nova@dbcrol cacti]$
失败了,看来此路不通啊。
2 google,继续看看去掉‘’试试。
在一篇文章里面看到 "sudo su -u mysql 'mkdir /mysqllog/log/a1.log'"中不需要把里面的命令行用''括起来,去掉试试看。
[nova@dbcrol cacti]$ ssh db02703 "sudo su -u mysql mkdir /mysqllog/log/a1.log"
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: 3 incorrect password attempts
[nova@dbcrol cacti]$
失败了,看来此路also不通啊。
3 继续google之,有人说要去掉su
换成 "sudo -u mysql mkdir /mysqllog/log/a1.log"吧:
[nova@dbcrol cacti]$ ssh db02703 "sudo -u mysql mkdir /mysqllog/log/a1.log"
[nova@dbcrol cacti]
OK,搞定了。
4 For循环,实现批量串行mkdir
[nova@dbcrol cacti] vim create_log1_20131101.sh
for i in `cat $1`; do
echo $i;
ssh $i "sudo -u mysql mkdir /mysqllog/log/a1.log";
echo "";
done;
5 执行脚本
[nova@dbcrol cacti] sh create_log1_20131101.sh db01list;
db00101
db00201
db00301
........
db12801
至此,所有128台db执行完毕。