通过SSH执行命令时无法使用别名

时间:2014-03-20 15:29:27

标签: bash ssh

我在.bashrc中注释掉了这一行:

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

现在别名被读取,但我仍然无法执行...:/

我们可以询问服务器是否定义了别名:

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

但它没有扩大:

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

有什么想法吗?

2 个答案:

答案 0 :(得分:16)

引自bash的手册页:当shell不是交互式时,不会展开别名,除非使用shopt设置expand_aliases shell选项...

因此,最简单的方法是将以下行放在/home/<user>/.bashrc文件的顶部:

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

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

保存并退出。现在,您可以成功运行ssh user@host "your_alias"

答案 1 :(得分:-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 
相关问题