我知道/etc/profile
並且是用於登入伺服器~/.profile
時使用的啟動腳本之一- 即與伺服器參數一起使用。但是,我僅使用伺服器+命令參數的場景似乎沒有選擇相同的啟動腳本。ssh
ssh
ssh
假設我將 bash 作為預設 shell。如果.bashrc
存在,我知道它會在這種情況下被拾取。但如果.bashrc
不存在並且不存在其他個人資料,那麼還有什麼可以運行呢?
是否有另一個提供環境變數的全域腳本?
答案1
此頁麵包含的內容超越您可能想了解的內容,但它是一個非常簡潔的資源:
基本上,當您透過 ssh 連接到電腦時,您並沒有執行「登入」shell,因此會取得不同的檔案。簡而言之,如果您的預設 shell 是 /bin/bash (Ubuntu 上預設),您將~/.bashrc
在您的主目錄中取得 a。檢查FILES
bash 手冊頁 ( man bash
) 靠近末尾的部分,以了解其他起作用的文件。
.bashrc
另外,請確保您在不使用「互動式」shell 的情況下留意退出的行。解決這個問題的一個好方法是將列印放入你的模型中.bashrc
並不斷進行實驗,直到你弄清楚為止。
像這樣:
# ~/.bashrc:
echo "HERE I WAS!"
# Exit here unless an interactive session.
[ -z "$PS1" ] && return
echo "HERE I AM!"
答案2
無需命令,SSH 就會執行登入 shell。對於bash
,這涉及採購.profile
(在 Ubuntu 上,為 來源.bashrc
)(以及/etc/profile
, 來源/etc/bash.bashrc
)。還有其他檔案可以替代,例如.bash_profile
,但預設的 Ubuntu 設定只有.profile
.
$ grep bashrc /etc/profile .profile
/etc/profile: # The file bash.bashrc already sets the default PS1.
/etc/profile: if [ -f /etc/bash.bashrc ]; then
/etc/profile: . /etc/bash.bashrc
.profile: # include .bashrc if it exists
.profile: if [ -f "$HOME/.bashrc" ]; then
.profile: . "$HOME/.bashrc
當使用命令運行時,SSH 不會運行登入 shell,因此,根據man bash
(部分INVOCATION
):
When an interactive shell that is not a login shell is started, bash
reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if
these files exist. This may be inhibited by using the --norc option.
The --rcfile file option will force bash to read and execute commands
from file instead of /etc/bash.bashrc and ~/.bashrc.
但是,使用命令時,bash
不會以互動方式運行。那為什麼要.bashrc
採購呢?再次,來自man bash
:
Bash attempts to determine when it is being run with its standard input
connected to a network connection, as when executed by the remote shell
daemon, usually rshd, or the secure shell daemon sshd. If bash
determines it is being run in this fashion, it reads and executes
commands from ~/.bashrc and ~/.bashrc, if these files exist and are
readable. It will not do this if invoked as sh. The --norc option may
be used to inhibit this behavior, and the --rcfile option may be used
to force another file to be read, but neither rshd nor sshd generally
invoke the shell with those options or allow them to be specified.
其他檔案可以透過 SSH 讀取(來自man ssh
, 部分FILES
):
~/.ssh/rc
Commands in this file are executed by ssh when the user logs in,
just before the user's shell (or command) is started. See the
sshd(8) manual page for more information.
/etc/ssh/sshrc
Commands in this file are executed by ssh when the user logs in,
just before the user's shell (or command) is started. See the
sshd(8) manual page for more information.
對於環境變量,(來自man ssh
,部分ENVIRONMENT
):
Additionally, ssh reads ~/.ssh/environment, and adds lines of the format
“VARNAME=value” to the environment if the file exists and users are
allowed to change their environment. For more information, see the
PermitUserEnvironment option in sshd_config(5).
此pam_env
模組已啟用 SSH:
$ grep pam_env /etc/pam.d/sshd
# /etc/security/pam_env.conf.
session required pam_env.so # [1]
session required pam_env.so user_readenv=1 envfile=/etc/default/locale
/etc/environment
因此,和中的變數~/.pam_environment
也被設定(並且/etc/default/locale
, 因為envfile
被設定)。但是,這些檔案不是按原樣取得.profile
的,因此您不能在此處使用 shell 命令。