問題
當(dāng)某個(gè)軟件如java安裝在某個(gè)用戶下,軟件的環(huán)境變量放在 ~/.bash_profile
或 ~/.zshrc
.SSH 遠(yuǎn)程執(zhí)行腳本報(bào)錯(cuò)"command not found" .環(huán)境變量不生效。
login shell 和 non-login shell
Login shell : 登陸linux 系統(tǒng)的時(shí)候,要求輸入 用戶名和密碼或者 SSH key 登陸時(shí)。
Non-login shell: 用 Ansible 在目標(biāo)機(jī)器上遠(yuǎn)程執(zhí)行Shell 腳本的時(shí)候康二,就是non-login shell
linux bash 環(huán)境配置文件
終端運(yùn)行 man bash
FILES
/bin/bash
The bash executable
/etc/profile
The systemwide initialization file, executed for login shells
/etc/bash.bashrc
The systemwide per-interactive-shell startup file
/etc/bash.bash.logout
The systemwide login shell cleanup file, executed when a login shell exits
~/.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
參考 鳥哥私房菜:
- login shell加載環(huán)境變量的順序是:① /etc/profile ② ~/.bash_profile ③ ~/.bashrc ④ /etc/bashrc
- 而non-login shell加載環(huán)境變量的順序是: ① ~/.bashrc ② /etc/bashrc
non-login shell 的環(huán)境配置問題的解決
方法1: 把配置放在 non-login shell 加載下的文件
方法1:如果把配置放在 ~/.bash_profile
中峭竣,需要non-login shell 時(shí)先 source ~/.bash_profile
再執(zhí)行命令
雖然大多數(shù)情況下伊佃,在Shell開頭用#!/usr/bin/env bash比#!/bin/bash更有通用性,但是在通過SSH遠(yuǎn)程執(zhí)行Shell腳本時(shí)绩蜻,Shell開頭要用#!/bin/bash
Ansible的become進(jìn)行用戶切換
參考