問題:
一個腳本a.sh(必須root用戶執行),在本地可以運行,通過pssh -h ip_file "cd /home/byte/a.sh"不能執行。
原因:
分析應該是ssh沒有獲取到root權限,可能原因是操作系統安裝時有多個用戶且默認遠程用戶非root。通過查看可知是ssh默認登錄用戶不是root
Linux系統設置涉及到兩種啟動shell的方式一個是本地登錄另一個是遠程登錄,
本地直接登錄shell終端環境調用過程是 ~/.bash_profile->~/.bashrc
ssh登錄時直接調用~/.bashrc,導致只是以普通用戶登錄,沒有獲取到root權限
解決辦法:
1.修改命令,先執行root的.bash_profile來獲取root權限
pssh -h ip_file "source /root/.bash_profile;cd /home/byte/a.sh"
2.修改.ssh配置文件
vi /etc/.ssh/enviroment 添加所需環境變量,格式如下
VAR1=VALUE1
VAR2=VALUE2
比如添加 USER=root
並修改/etc/ssh/sshd_config 文件中的PermitUserEnvironment=yes(會帶來安全問題,具體請看備注1鏈接)
下次登錄就直接獲取root權限了
備注:
1. 安全問題
PermitUserEnvironment 指定是否允許 sshd(8) 處理 ~/.ssh/environment 以及 ~/.ssh/authorized_keys 中的 environment= 選項。
默認值是"no"。如果設為"yes"可能會導致用戶有機會使用某些機制(比如 LD_PRELOAD)繞過訪問控
參考連接 http://www.2cto.com/os/201401/272738.html
http://serverfault.com/questions/527638/security-risks-of-permituserenvironment-in-ssh
2.相關文件解釋
/bin/bash The bash executable /etc/profile The systemwide initialization file, executed for login shells ~/.bash_profile The personal initialization file, executed for login shells ~/.bashrc The individual per-interactive-shell startup file ~/.bash_logout The individual login shell cleanup file, executed when a login shell exits ~/.inputrc Individual readline initialization file
參考:http://serverfault.com/questions/527638/security-risks-of-permituserenvironment-in-ssh
http://stackoverflow.com/questions/415403/whats-the-difference-between-bashrc-bash-profile-and-environment