Waarom 'n SSH afgeleë opdrag kry minder omgewing veranderlikes dan wanneer die hand loop? [Gesluit]
-
03-07-2019 - |
Vra
Ek het 'n bevel dat fyn loop as ek ssh 'n masjien en hardloop, maar misluk wanneer ek probeer om dit uit te voer met behulp van 'n afgeleë ssh opdrag soos:
ssh user@IP <command>
Die vergelyking van die opbrengs van "env" die gebruik van beide metodes resutls in verskillende omgewings. Toe ek met die hand te meld by die masjien en hardloop env, kry ek baie meer omgewing veranderlikes dan wanneer ek loop:
ssh user@IP "env"
Enige idee hoekom?
Oplossing
Daar is verskillende tipes van skulpe. Die SSH opdrag uitvoering dop is 'n nie-interaktiewe dop, terwyl jou normale dop is óf 'n login dop of 'n interaktiewe dop. Beskrywing volg, van 'n mens bash:
A login shell is one whose first character of argument zero is a -, or one started with the --login option. An interactive shell is one started without non-option arguments and without the -c option whose standard input and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option. PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state. The following paragraphs describe how bash executes its startup files. If any of the files exist but cannot be read, bash reports an error. Tildes are expanded in file names as described below under Tilde Expansion in the EXPANSION section. When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behav ior. When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists. When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. 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 ~/.bashrc. When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if the following command were executed: if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi but the value of the PATH variable is not used to search for the file name.
Ander wenke
Hoe gaan dit met die verkryging van die profiel voor loop die opdrag?
ssh user@host "source /etc/profile; /path/script.sh"
Jy kan dit die beste te vind om dit te verander na ~/.bash_profile
, ~/.bashrc
, of wat ook al.
(Soos hier (linuxquestions.org) )
Shell omgewing nie laai wanneer hy hardloop afgeleë opdrag ssh. Jy kan ssh omgewing lêer wysig:
vi ~/.ssh/environment
Die formaat is:
VAR1=VALUE1
VAR2=VALUE2
Ook, check sshd opset vir PermitUserEnvironment = Ja opsie.
Ek het soortgelyke probleem, maar op die ou end het ek uitgevind dat ~ / .bashrc was al wat ek nodig gehad het.
Maar in Ubuntu, moes ek die lyn wat tot stilstand kom kommentaar verwerking ~ / .bashrc:
#If not running interactively, don't do anything
[ -z "$PS1" ] && return
Ek het gevind dat 'n maklike oplossing vir hierdie probleem is om by te voeg bron / etc / profile om die top van die script.sh lêer het ek probeer om uit te voer op die teiken stelsel. Op die stelsels hier, dit veroorsaak die omgewingsveranderlikes wat nodig is deur script.sh word ingestel asof die loop van 'n login dop.
In een van die vorige antwoorde is daar voorgestel dat ~ / .bashr_profile ens ... gebruik. Ek het nie veel tyd te spandeer op hierdie, maar die probleem met hierdie is as jy ssh 'n ander gebruiker op die teiken stelsel as die dop op die bron stelsel waaruit jy inteken dit aan my verskyn wat dit veroorsaak dat die bron stelsel gebruiker Naam wil gebruik vir die ~.
uitvoer Net die omgewing veranderlikes wat jy wil bo die tjek vir 'n nie-interaktiewe dop in ~ / .bashrc.