davidhq
davidhq

Reputation: 4760

Cannot use alias while executing a command via SSH

I commented out this line in .bashrc:

# [ -z "$PS1" ] && return

and now the alias gets read, but I still cannot execute it... :/

We can ask the server if the alias has been defined:

$ ssh server "cd /tmp && alias backup_tb"
alias backup_tb='pg_dump -U david tb > tb.sql'

But it is not expanded:

$ ssh server "cd /tmp && backup_tb"
bash: backup_tb: command not found

Any ideas?

Upvotes: 8

Views: 8942

Answers (2)

yekirihi
yekirihi

Reputation: 1

# WARNING!!!! Just effective in openssh-server.!!!!!!!

# cd your loacl user .ssh path 
# if you want to make all user has the same alias you should go to 
#'/etc/ssh' touch a file 'sshrc'
# what is 'sshrc'? he has the same function as 'rc.local',just a shell 
# script when you first login.  

# The following is a configuration of the current user .
cd ~/.ssh

touch sshrc  

chmod +x sshrc

#edit sshrc and type 

alias ll="ls -lah --color"

#Apply changes

source sshrc 

Upvotes: -1

stanleyxu2005
stanleyxu2005

Reputation: 8231

Quoted from the man page of bash: Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt ...

So the simplest way IMO is to put the following lines at the top of your /home/<user>/.bashrc file:

# comment out the original line
# [ -z "$PS1" ] && return

if [ -z "$PS1" ]; then
  shopt -s expand_aliases
  # alias ls='ls --color=always'
  # return
fi

Save and exit. Now you can run ssh user@host "your_alias" successfully.

Upvotes: 16

Related Questions