想要在 shell 腳本中無密碼地 su

想要在 shell 腳本中無密碼地 su

我已經編寫了一個腳本,它將運行先前的備份腳本:

# cat backup-before-shutdown
#Scritp to take backup before shutdown
#Author Ashish Karpe

echo "Taking backup to s3"
su ubuntu /bin/sh /home/ubuntu/backup.sh
/bin/sh /home/ubuntu/backup-to-s3.sh

通常,當我將使用者從 切換root到時ubuntu,它不會在切換時詢問密碼rootubuntu但會在執行腳本時詢問。那我該怎麼做才能避免腳本中出現密碼呢?

答案1

如果腳本以 root 使用者身分執行,則此sudo命令字串應該無需密碼即可執行命令。

sudo -H -u ubuntu /bin/sh /home/ubuntu/backup.sh

相關內容