[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執行完畢。