Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ssh command execution doesn't consider .bashrc | .bash_login | .ssh/rc? [duplicate]

Tags:

bash

ssh

I am trying to execute a command remotely over ssh, example:

ssh <user>@<host> <command>

The command which needs to be executed is an alias, which is defined in .bashrc, e.g.

alias ll='ls -al'

So what in the end the following command should get executed:

ssh user@host "ll"

I already found out that .bashrc only gets sourced with interactive shell, so in .bash_login I put:

if [ -f ~/.bashrc ]; then
  . ~/.bashrc
fi

and I also tried to define the alias directly in .bash_login.

I also tried to put the alias definition / sourcing of .bashrc in .bash_profile and also in .ssh/rc. But nothing of this works. Note that I am not able to change how the ssh command is invoked since this is a part of some binary installation script. The only thing I can modify is the environment. Is there any other possibility to get this alias sourced when the ssh command is executed? Is there some ssh configuration which has to be adapted?

like image 883
blackicecube Avatar asked Jul 29 '09 06:07

blackicecube


3 Answers

From the man pages of bash:

Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt

There are a couple ways to do this, but the simplest is to just add the following line to your .bashrc file:

shopt -s expand_aliases
like image 110
Joey Mazzarelli Avatar answered Oct 22 '22 17:10

Joey Mazzarelli


Instead of:

ssh user@host "bash -c ll"

try:

ssh user@host "bash -ic ll"

to force bash to use an "interactive shell".

like image 33
Fred Stluka Avatar answered Oct 22 '22 18:10

Fred Stluka


EDIT:

As pointed out here about non-interactive shells..


 # If not running interactively, don't do anything
[ -z "$PS1" ] && return
 # execution returns after this line

Now, for every alias in your bashrc file say i have:


alias ll="ls -l"
alias cls="clear;ls" 

Create a file named after that alias say for ll:

user@host$ vi ssh_aliases/ll
#inside ll,write
ls -l
user@host$ chmod a+x ll

Now edit .bashrc to include:


 # If not running interactively, don't do anything
[ -z "$PS1" ] && export $PATH=$PATH:~/ssh_aliases

This does the job.. although I am not sure if it is the best way to do so
EDIT(2)
You only need to do this for aliases, other commands in bashrc will be executed as pointed out by David "you must have executable for ssh to run commands".

like image 10
sud03r Avatar answered Oct 22 '22 16:10

sud03r