日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ssh连接远程主机执行脚本的环境变量问题

發(fā)布時(shí)間:2025/3/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ssh连接远程主机执行脚本的环境变量问题 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

http://www.kuqin.com/shuoit/20141113/343188.html
近日在使用ssh命令ssh user@remote ~/myscript.sh登陸到遠(yuǎn)程機(jī)器remote上執(zhí)行腳本時(shí),遇到一個(gè)奇怪的問題:

~/myscript.sh: line n: app: command not found

app是一個(gè)新安裝的程序,安裝路徑明明已通過/etc/profile配置文件加到環(huán)境變量中,但這里為何會(huì)找不到?如果直接登陸機(jī)器remote并執(zhí)行~/myscript.sh時(shí),app程序可以找到并順利執(zhí)行。但為什么使用了ssh遠(yuǎn)程執(zhí)行同樣的腳本就出錯(cuò)了呢?兩種方式執(zhí)行腳本到底有何不同?如果你也心存疑問,請(qǐng)跟隨我一起來展開分析。
目錄

問題定位 bash的四種模式interactive + login shell加載配置文件配置文件的意義non-interactive + login shellinteractive + non-login shell加載配置文件bashrc VS profilenon-interactive + non-login shell加載配置文件更為直觀的示圖典型模式總結(jié) 再次嘗試 配置文件建議 寫在結(jié)尾

說明,本文所使用的機(jī)器是:SUSE Linux Enterprise。
問題定位

這看起來像是環(huán)境變量引起的問題,為了證實(shí)這一猜想,我在這條命令之前加了一句:which app,來查看app的安裝路徑。在remote本機(jī)上執(zhí)行腳本時(shí),它會(huì)打印出app正確的安裝路徑。但再次用ssh來執(zhí)行時(shí),卻遇到下面的錯(cuò)誤:

which: no app in (/usr/bin:/bin:/usr/sbin:/sbin)

這很奇怪,怎么括號(hào)中的環(huán)境變量沒有了app程序的安裝路徑?不是已通過/etc/profile設(shè)置到PATH中了?再次在腳本中加入echo $PATH并以ssh執(zhí)行,這才發(fā)現(xiàn),環(huán)境變量仍是系統(tǒng)初始化時(shí)的結(jié)果:

/usr/bin:/bin:/usr/sbin:/sbin

這證明/etc/profile根本沒有被調(diào)用。為什么?是ssh命令的問題么?

隨后我又嘗試了將上面的ssh分解成下面兩步:

user@local > ssh user@remote # 先遠(yuǎn)程登陸到remote上 user@remote> ~/myscript.sh # 然后在返回的shell中執(zhí)行腳本

結(jié)果竟然成功了。那么ssh以這兩種方式執(zhí)行的命令有何不同?帶著這個(gè)問題去查詢了man ssh:

If command is specified, it is executed on the remote host instead of a login shell.

這說明在指定命令的情況下,命令會(huì)在遠(yuǎn)程主機(jī)上執(zhí)行,返回結(jié)果后退出。而未指定時(shí),ssh會(huì)直接返回一個(gè)登陸的shell。但到這里還是無法理解,直接在遠(yuǎn)程主機(jī)上執(zhí)行和在返回的登陸shell中執(zhí)行有什么區(qū)別?即使在遠(yuǎn)程主機(jī)上執(zhí)行不也是通過shell來執(zhí)行的么?難道是這兩種方式使用的shell有什么不同?

暫時(shí)還沒有頭緒,但隱隱感到應(yīng)該與shell有關(guān)。因?yàn)槲彝ǔJ褂玫氖莃ash,所以又去查詢了man bash,才得到了答案。
bash的四種模式

在man page的INVOCATION一節(jié)講述了bash的四種模式,bash會(huì)依據(jù)這四種模式而選擇加載不同的配置文件,而且加載的順序也有所不同。本文ssh問題的答案就存在于這幾種模式當(dāng)中,所以在我們揭開謎底之前先來分析這些模式。
interactive + login shell

第一種模式是交互式的登陸shell,這里面有兩個(gè)概念需要解釋:interactive和login:

login故名思義,即登陸,login shell是指用戶以非圖形化界面或者以ssh登陸到機(jī)器上時(shí)獲得的第一個(gè)shell,簡(jiǎn)單些說就是需要輸入用戶名和密碼的shell。因此通常不管以何種方式登陸機(jī)器后用戶獲得的第一個(gè)shell就是login shell。

interactive意為交互式,這也很好理解,interactive shell會(huì)有一個(gè)輸入提示符,并且它的標(biāo)準(zhǔn)輸入、輸出和錯(cuò)誤輸出都會(huì)顯示在控制臺(tái)上。所以一般來說只要是需要用戶交互的,即一個(gè)命令一個(gè)命令的輸入的shell都是interactive shell。而如果無需用戶交互,它便是non-interactive shell。通常來說如bash script.sh此類執(zhí)行腳本的命令就會(huì)啟動(dòng)一個(gè)non-interactive shell,它不需要與用戶進(jìn)行交互,執(zhí)行完后它便會(huì)退出創(chuàng)建的shell。

那么此模式最簡(jiǎn)單的兩個(gè)例子為:

用戶直接登陸到機(jī)器獲得的第一個(gè)shell 用戶使用ssh user@remote獲得的shell

加載配置文件

這種模式下,shell首先加載/etc/profile,然后再嘗試依次去加載下列三個(gè)配置文件之一,一旦找到其中一個(gè)便不再接著尋找:

~/.bash_profile ~/.bash_login ~/.profile

下面給出這個(gè)加載過程的偽代碼:

execute /etc/profile IF ~/.bash_profile exists THEN execute ~/.bash_profile ELSE IF ~/.bash_login exist THEN execute ~/.bash_login ELSE IF ~/.profile exist THEN execute ~/.profile END IF END IF END IF

為了驗(yàn)證這個(gè)過程,我們來做一些測(cè)試。首先設(shè)計(jì)每個(gè)配置文件的內(nèi)容如下:

1 user@remote > cat /etc/profile 2 echo @ /etc/profile 3 user@remote > cat ~/.bash_profile 4 echo @ ~/.bash_profile 5 user@remote > cat ~/.bash_login 6 echo @ ~/.bash_login 7 user@remote > cat ~/.profile 8 echo @ ~/.profile

然后打開一個(gè)login shell,注意,為方便起見,這里使用bash -l命令,它會(huì)打開一個(gè)login shell,在man bash中可以看到此參數(shù)的解釋:

-l Make bash act as if it had been invoked as a login shell

進(jìn)入這個(gè)新的login shell,便會(huì)得到以下輸出:

@ /etc/profile @ /home/user/.bash_profile

果然與文檔一致,bash首先會(huì)加載全局的配置文件/etc/profile,然后去查找~/.bash_profile,因?yàn)槠湟呀?jīng)存在,所以剩下的兩個(gè)文件不再會(huì)被查找。

接下來移除~/.bash_profile,啟動(dòng)login shell得到結(jié)果如下:

@ /etc/profile @ /home/user/.bash_login

因?yàn)闆]有了~/.bash_profile的屏蔽,所以~/.bash_login被加載,但最后一個(gè)~/.profile仍被忽略。

再次移除~/.bash_login,啟動(dòng)login shell的輸出結(jié)果為:

@ /etc/profile @ /home/user/.profile

~/.profile終于熬出頭,得見天日。通過以上三個(gè)實(shí)驗(yàn),配置文件的加載過程得到了驗(yàn)證,除去/etc/profile首先被加載外,其余三個(gè)文件的加載順序?yàn)?#xff1a;~/.bash_profile>~/.bash_login>~/.profile,只要找到一個(gè)便終止查找。

前面說過,使用ssh也會(huì)得到一個(gè)login shell,所以如果在另外一臺(tái)機(jī)器上運(yùn)行ssh user@remote時(shí),也會(huì)得到上面一樣的結(jié)論。
配置文件的意義

那么,為什么bash要弄得這么復(fù)雜?每個(gè)配置文件存在的意義是什么?

/etc/profile很好理解,它是一個(gè)全局的配置文件。后面三個(gè)位于用戶主目錄中的配置文件都針對(duì)用戶個(gè)人,也許你會(huì)問為什么要有這么多,只用一個(gè)~/.profile不好么?究竟每個(gè)文件有什么意義呢?這是個(gè)好問題。

Cameron Newham和Bill Rosenblatt在他們的著作《Learning the bash Shell, 2nd Edition》的59頁解釋了原因:

bash allows two synonyms for .bash_profile: .bash_login, derived from the C shell’s file named .login, and .profile, derived from the Bourne shell and Korn shell files named .profile. Only one of these three is read when you log in. If .bash_profile doesn’t exist in your home directory, then bash will look for .bash_login. If that doesn’t exist it will look for .profile.One advantage of bash’s ability to look for either synonym is that you can retain your .profile if you have been using the Bourne shell. If you need to add bash-specific commands, you can put them in .bash_profile followed by the command source .profile. When you log in, all the bash-specific commands will be executed and bash will source .profile, executing the remaining commands. If you decide to switch to using the Bourne shell you don’t have to modify your existing files. A similar approach was intended for .bash_login and the C shell .login, but due to differences in the basic syntax of the shells, this is not a good idea.

原來一切都是為了兼容,這么設(shè)計(jì)是為了更好的應(yīng)付在不同shell之間切換的場(chǎng)景。因?yàn)閎ash完全兼容Bourne shell,所以.bash_profile和.profile可以很好的處理bash和Bourne shell之間的切換。但是由于C shell和bash之間的基本語法存在著差異,作者認(rèn)為引入.bash_login并不是個(gè)好主意。所以由此我們可以得出這樣的最佳實(shí)踐:

應(yīng)該盡量杜絕使用.bash_login,如果已經(jīng)創(chuàng)建,那么需要?jiǎng)?chuàng)建.bash_profile來屏蔽它被調(diào)用 .bash_profile適合放置bash的專屬命令,可以在其最后讀取.profile,如此一來,便可以很好的在Bourne shell和bash之間切換了

non-interactive + login shell

第二種模式的shell為non-interactive login shell,即非交互式的登陸shell,這種是不太常見的情況。一種創(chuàng)建此shell的方法為:bash -l script.sh,前面提到過-l參數(shù)是將shell作為一個(gè)login shell啟動(dòng),而執(zhí)行腳本又使它為non-interactive shell。

對(duì)于這種類型的shell,配置文件的加載與第一種完全一樣,在此不再贅述。
interactive + non-login shell

第三種模式為交互式的非登陸shell,這種模式最常見的情況為在一個(gè)已有shell中運(yùn)行bash,此時(shí)會(huì)打開一個(gè)交互式的shell,而因?yàn)椴辉傩枰顷?#xff0c;因此不是login shell。
加載配置文件

對(duì)于此種情況,啟動(dòng)shell時(shí)會(huì)去查找并加載/etc/bash.bashrc和~/.bashrc文件。

為了進(jìn)行驗(yàn)證,與第一種模式一樣,設(shè)計(jì)各配置文件內(nèi)容如下:

1 user@remote > cat /etc/bash.bashrc 2 echo @ /etc/bash.bashrc 3 user@remote > cat ~/.bashrc 4 echo @ ~/.bashrc

然后我們啟動(dòng)一個(gè)交互式的非登陸shell,直接運(yùn)行bash即可,可以得到以下結(jié)果:

@ /etc/bash.bashrc @ /home/user/.bashrc

由此非常容易的驗(yàn)證了結(jié)論。
bashrc VS profile

從剛引入的兩個(gè)配置文件的存放路徑可以很容易的判斷,第一個(gè)文件是全局性的,第二個(gè)文件屬于當(dāng)前用戶。在前面的模式當(dāng)中,已經(jīng)出現(xiàn)了幾種配置文件,多數(shù)是以profile命名的,那么為什么這里又增加兩個(gè)文件呢?這樣不會(huì)增加復(fù)雜度么?我們來看看此處的文件和前面模式中的文件的區(qū)別。

首先看第一種模式中的profile類型文件,它是某個(gè)用戶唯一的用來設(shè)置全局環(huán)境變量的地方, 因?yàn)橛脩艨梢杂卸鄠€(gè)shell比如bash, sh, zsh等, 但像環(huán)境變量這種其實(shí)只需要在統(tǒng)一的一個(gè)地方初始化就可以, 而這個(gè)地方就是profile,所以啟動(dòng)一個(gè)login shell會(huì)加載此文件,后面由此shell中啟動(dòng)的新shell進(jìn)程如bash,sh,zsh等都可以由login shell中繼承環(huán)境變量等配置。

接下來看bashrc,其后綴rc的意思為Run Commands,由名字可以推斷出,此處存放bash需要運(yùn)行的命令,但注意,這些命令一般只用于交互式的shell,通常在這里會(huì)設(shè)置交互所需要的所有信息,比如bash的補(bǔ)全、alias、顏色、提示符等等。

所以可以看出,引入多種配置文件完全是為了更好的管理配置,每個(gè)文件各司其職,只做好自己的事情。
non-interactive + non-login shell

最后一種模式為非交互非登陸的shell,創(chuàng)建這種shell典型有兩種方式:

bash script.sh ssh user@remote command

這兩種都是創(chuàng)建一個(gè)shell,執(zhí)行完腳本之后便退出,不再需要與用戶交互。
加載配置文件

對(duì)于這種模式而言,它會(huì)去尋找環(huán)境變量BASH_ENV,將變量的值作為文件名進(jìn)行查找,如果找到便加載它。

同樣,我們對(duì)其進(jìn)行驗(yàn)證。首先,測(cè)試該環(huán)境變量未定義時(shí)配置文件的加載情況,這里需要一個(gè)測(cè)試腳本:

1 user@remote > cat ~/script.sh 2 echo Hello World

然后運(yùn)行bash script.sh,將得到以下結(jié)果:

Hello World

從輸出結(jié)果可以得知,這個(gè)新啟動(dòng)的bash進(jìn)程并沒有加載前面提到的任何配置文件。接下來設(shè)置環(huán)境變量BASH_ENV:

1 user@remote > export BASH_ENV=~/.bashrc

再次執(zhí)行bash script.sh,結(jié)果為:

@ /home/user/.bashrc Hello World

果然,~/.bashrc被加載,而它是由環(huán)境變量BASH_ENV設(shè)定的。
更為直觀的示圖

至此,四種模式下配置文件如何加載已經(jīng)講完,因?yàn)樯婕暗呐渲梦募行┒?#xff0c;我們?cè)僖詢蓚€(gè)圖來更為直觀的進(jìn)行描述:

第一張圖來自這篇文章,bash的每種模式會(huì)讀取其所在列的內(nèi)容,首先執(zhí)行A,然后是B,C。而B1,B2和B3表示只會(huì)執(zhí)行第一個(gè)存在的文件:

+—————-+——–+———–+—————+ | | login |interactive|non-interactive| | | |non-login |non-login | +—————-+——–+———–+—————+ |/etc/profile | A | | | +—————-+——–+———–+—————+ |/etc/bash.bashrc| | A | | +—————-+——–+———–+—————+ |~/.bashrc | | B | | +—————-+——–+———–+—————+ |~/.bash_profile | B1 | | | +—————-+——–+———–+—————+ |~/.bash_login | B2 | | | +—————-+——–+———–+—————+ |~/.profile | B3 | | | +—————-+——–+———–+—————+ |BASH_ENV | | | A | +—————-+——–+———–+—————+

上圖只給出了三種模式,原因是第一種login實(shí)際上已經(jīng)包含了兩種,因?yàn)檫@兩種模式下對(duì)配置文件的加載是一致的。

另外一篇文章給出了一個(gè)更直觀的圖:

Bash加載文件順序

上圖的情況稍稍復(fù)雜一些,因?yàn)樗褂昧藥讉€(gè)關(guān)于配置文件的參數(shù):–login,–rcfile,–noprofile,–norc,這些參數(shù)的引入會(huì)使配置文件的加載稍稍發(fā)生改變,不過總體來說,不影響我們前面的討論,相信這張圖不會(huì)給你帶來更多的疑惑。
典型模式總結(jié)

為了更好的理清這幾種模式,下面我們對(duì)一些典型的啟動(dòng)方式各屬于什么模式進(jìn)行一個(gè)總結(jié):

登陸機(jī)器后的第一個(gè)shell:login + interactive 新啟動(dòng)一個(gè)shell進(jìn)程,如運(yùn)行bash:non-login + interactive 執(zhí)行腳本,如bash script.sh:non-login + non-interactive 運(yùn)行頭部有如#!/usr/bin/env bash的可執(zhí)行文件,如./executable:non-login + non-interactive 通過ssh登陸到遠(yuǎn)程主機(jī):login + interactive 遠(yuǎn)程執(zhí)行腳本,如ssh user@remote script.sh:non-login + non-interactive 遠(yuǎn)程執(zhí)行腳本,同時(shí)請(qǐng)求控制臺(tái),如ssh user@remote -t 'echo $PWD':non-login + interactive 在圖形化界面中打開terminal: Linux上: non-login + interactive Mac OS X上: login + interactive

相信你在理解了login和interactive的含義之后,應(yīng)該會(huì)很容易對(duì)上面的啟動(dòng)方式進(jìn)行歸類。
再次嘗試

在介紹完bash的這些模式之后,我們?cè)倩仡^來看文章開頭的問題。ssh user@remote ~/myscript.sh屬于哪一種模式?相信此時(shí)你可以非常輕松的回答出來:non-login + non-interactive。對(duì)于這種模式,bash會(huì)選擇加載$BASH_ENV的值所對(duì)應(yīng)的文件,所以為了讓它加載/etc/profile,可以設(shè)定:

1 user@local > export BASH_ENV=/etc/profile

然后執(zhí)行上面的命令,但是很遺憾,發(fā)現(xiàn)錯(cuò)誤依舊存在。這是怎么回事?別著急,這并不是我們前面的介紹出錯(cuò)了。仔細(xì)查看之后才發(fā)現(xiàn)腳本myscript.sh的第一行為#!/usr/bin/env sh,注意看,它和前面提到的#!/usr/bin/env bash不一樣,可能就是這里出了問題。我們先嘗試把它改成#!/usr/bin/env bash,再次執(zhí)行,錯(cuò)誤果然消失了,這與我們前面的分析結(jié)果一致。

第一行的這個(gè)語句有什么用?設(shè)置成sh和bash有什么區(qū)別?帶著這些疑問,再來查看man bash:

If the program is a file beginning with #!, the remainder of the first line specifies an interpreter for the program.

它表示這個(gè)文件的解釋器,即用什么程序來打開此文件,就好比Windows上雙擊一個(gè)文件時(shí)會(huì)以什么程序打開一樣。因?yàn)檫@里不是bash,而是sh,那么我們前面討論的都不復(fù)有效了,真糟糕。我們來看看這個(gè)sh的路徑:

1 user@remote > ll which sh 2 lrwxrwxrwx 1 root root 9 Apr 25 2014 /usr/bin/sh -> /bin/bash

原來sh只是bash的一個(gè)軟鏈接,既然如此,BASH_ENV應(yīng)該是有效的啊,為何此處無效?還是回到man bash,同樣在INVOCATION一節(jié)的下部看到了這樣的說明:

If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well. When invoked as an interactive login shell, or a non-interactive shell with the –login option, it first attempts to read and execute commands from /etc/profile and ~/.profile, in that order. The –noprofile option may be used to inhibit this behavior. When invoked as an interactive shell with the name sh, bash looks for the variable ENV, expands its value if it is defined, and uses the expanded value as the name of a file to read and execute. Since a shell invoked as sh does not attempt to read and execute commands from any other startup files, the –rcfile option has no effect. A non-interactive shell invoked with the name sh does not attempt to read any other startup files. When invoked as sh, bash enters posix mode after the startup files are read.

簡(jiǎn)而言之,當(dāng)bash以是sh命啟動(dòng)時(shí),即我們此處的情況,bash會(huì)盡可能的模仿sh,所以配置文件的加載變成了下面這樣:

interactive + login: 讀取/etc/profile和~/.profile non-interactive + login: 同上 interactive + non-login: 讀取ENV環(huán)境變量對(duì)應(yīng)的文件 non-interactive + non-login: 不讀取任何文件

這樣便可以解釋為什么出錯(cuò)了,因?yàn)檫@里屬于non-interactive + non-login,所以bash不會(huì)讀取任何文件,故而即使設(shè)置了BASH_ENV也不會(huì)起作用。所以為了解決問題,只需要把sh換成bash,再設(shè)置環(huán)境變量BASH_ENV即可。

另外,其實(shí)我們還可以設(shè)置參數(shù)到第一行的解釋器中,如#!/bin/bash –login,如此一來,bash便會(huì)強(qiáng)制為login shell,所以/etc/profile也會(huì)被加載。相比上面那種方法,這種更為簡(jiǎn)單。
配置文件建議

回顧一下前面提到的所有配置文件,總共有以下幾種:

/etc/profile ~/.bash_profile ~/.bash_login ~/.profile /etc/bash.bashrc ~/.bashrc $BASH_ENV $ENV

不知你是否會(huì)有疑問,這么多的配置文件,究竟每個(gè)文件里面應(yīng)該包含哪些配置,比如PATH應(yīng)該在哪?提示符應(yīng)該在哪配置?啟動(dòng)的程序應(yīng)該在哪?等等。所以在文章的最后,我搜羅了一些最佳實(shí)踐供各位參考。(這里只討論屬于用戶個(gè)人的配置文件)

~/.bash_profile:應(yīng)該盡可能的簡(jiǎn)單,通常會(huì)在最后加載.profile和.bashrc(注意順序) ~/.bash_login:在前面討論過,別用它 ~/.profile:此文件用于login shell,所有你想在整個(gè)用戶會(huì)話期間都有效的內(nèi)容都應(yīng)該放置于此,比如啟動(dòng)進(jìn)程,環(huán)境變量等 ~/.bashrc:只放置與bash有關(guān)的命令,所有與交互有關(guān)的命令都應(yīng)該出現(xiàn)在此,比如bash的補(bǔ)全、alias、顏色、提示符等等。特別注意:別在這里輸出任何內(nèi)容(我們前面只是為了演示,別學(xué)我哈)

寫在結(jié)尾

至此,我們?cè)敿?xì)的討論完了bash的幾種工作模式,并且給出了配置文件內(nèi)容的建議。通過這些模式的介紹,本文開始遇到的問題也很容易的得到了解決。以前雖然一直使用bash,但真的不清楚里面包含了如此多的內(nèi)容。同時(shí)感受到Linux的文檔的確做得非常細(xì)致,在完全不需要其它安裝包的情況下,你就可以得到一個(gè)非常完善的開發(fā)環(huán)境,這也曾是Eric S. Raymond在其著作《UNIX編程藝術(shù)》中提到的:UNIX天生是一個(gè)非常完善的開發(fā)機(jī)器。本文幾乎所有的內(nèi)容你都可以通過閱讀man page得到。最后,希望在這樣一個(gè)被妖魔化的特殊日子里,這篇文章能夠?yàn)槟銕ヒ唤z幫助。

總結(jié)

以上是生活随笔為你收集整理的ssh连接远程主机执行脚本的环境变量问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。