通过 ssh 会话执行 bash 别名
2018-03-02 00:00
162 查看
我在远程主机上上设置过一个叫做 file_repl 的 bash 别名 。当我使用 ssh 命令登录远程主机后,可以很正常的使用这个别名。然而这个 bash 别名却无法通过 ssh 来运行,像这样:
SSH 客户端 (ssh) 是一个登录远程服务器并在远程系统上执行 shell 命令的 Linux/Unix 命令。它被设计用来在两个非信任的机器上通过不安全的网络(比如互联网)提供安全的加密通讯。
别名,比如
关键字,比如
函数 (用户自定义函数,比如
内置命令,比如
外部文件,比如
type 命令 和 command 命令 可以用来查看命令类型:
bash shell 的选项:
总之,要运行一个名叫
下面是我的一个 shell 脚本的例子:
via: https://www.cyberciti.biz/faq/use-bash-aliases-ssh-based-session/
我要怎样做才能通过 ssh 命令运行 bash 别名呢?$ ssh vivek@server1.cyberciti.biz file_replbash:file_repl:command not found
SSH 客户端 (ssh) 是一个登录远程服务器并在远程系统上执行 shell 命令的 Linux/Unix 命令。它被设计用来在两个非信任的机器上通过不安全的网络(比如互联网)提供安全的加密通讯。
如何用 ssh 客户端执行命令
通过 ssh 运行free命令或 date 命令 可以这样做:
结果为:$ ssh vivek@server1.cyberciti.biz date
或者:Tue Dec 26 09:02:50 UTC 2017
结果为:$ ssh vivek@server1.cyberciti.biz free -h
total used free shared buff/cache availableMem:2.0G 428M 138M 145M 1.4G 1.1GSwap:0B 0B 0B
理解 bash shell 以及命令的类型
bash shell 共有下面几类命令:别名,比如
ll
关键字,比如
if
函数 (用户自定义函数,比如
genpasswd)
内置命令,比如
pwd
外部文件,比如
/bin/date
type 命令 和 command 命令 可以用来查看命令类型:
$ type -a datedate is /bin/date$ type -a freefree is /usr/bin/free$ command -V pwdpwd is a shell builtin$ type -a file_replis aliased to `sudo -i /shared/takes/master.replication'
date和
free都是外部命令,而
file_repl是
sudo -i /shared/takes/master.replication的别名。你不能直接执行像
file_repl这样的别名:
$ ssh user@remote file_repl
在 Unix 系统上无法直接通过 ssh 客户端执行 bash 别名
要解决这个问题可以用下面方法运行 ssh 命令:$ ssh -t user@remote /bin/bash -ic 'your-alias-here'$ ssh -t user@remote /bin/bash -ic 'file_repl'
ssh命令选项:
-t:强制分配伪终端。可以用来在远程机器上执行任意的 基于屏幕的程序,有时这非常有用。当使用
-t时你可能会收到一个类似 “bash: cannot set terminal process group (-1): Inappropriate ioctl for device. bash: no job control in this shell .” 的错误。
bash shell 的选项:
-i:运行交互 shell,这样 shell 才能运行 bash 别名。
-c:要执行的命令取之于第一个非选项参数的命令字符串。若在命令字符串后面还有其他参数,这些参数会作为位置参数传递给命令,参数从
$0开始。
总之,要运行一个名叫
ll的 bash 别名,可以运行下面命令:
结果为:$ ssh -t vivek@server1.cyberciti.biz -ic 'll'
下面是我的一个 shell 脚本的例子:
#!/bin/bashI="tags.deleted.410"O="/tmp/https.www.cyberciti.biz.410.url.conf"box="vivek@server1.cyberciti.biz"[!-f "$I" ] && { echo "$I file not found。"; exit 10; }>$Ocat "$I" | sort | uniq | while read -r udo uu="${u##https://www.cyberciti.biz}" echo "~^$uu 1;" >>"${O}"doneecho "Config file created at ${O} and now updating remote nginx config file"scp "${O}" ${box}:/tmp/ssh ${box} /usr/bin/lxc file push /tmp/https.www.cyberciti.biz.410.url.conf nginx-container/etc/nginx/ssh -t ${box} /bin/bash -ic 'push_config_job'
相关资料
更多信息请输入下面命令查看 OpenSSH 客户端 和 bash 的 man 帮助 :$ man ssh$ man bash$ help type$ help command
via: https://www.cyberciti.biz/faq/use-bash-aliases-ssh-based-session/
相关文章推荐
- 通过 ssh 会话执行 bash 别名
- 通过 ssh 会话执行 bash 别名 | Linux 中国
- bash 中 while读取文件并通过 ssh执行命令出现的问题及解决方法
- TortoiseGit通过SSH执行git clone报错SSH2_MSG_UNIMPLEMENTED
- 无需密码通过ssh执行rsync来同步文件
- C#通过SharpSSH库与Linux服务器建立SSH连接并执行命令
- 无需密码通过ssh执行rsync来同步文件
- 通过ssh 执行远端服务器命令
- 通过Ssh协议连接到服务器执行执行的指令
- java通过ssh登陆执行linux命令
- 无需密码通过ssh执行rsync来同步文件
- 无密码通过ssh执行rsync
- 教你如何通过ssh控制远程主机,远程执行命令
- 用shell脚本为ubuntu自动添加用户,并限定通过ssh连接该用户只能执行特定某个(或某几个)程序
- TASKCTL中不使用代理,通过ssh免密连接执行远程脚本配置(SSH插件扩展)
- 纯Java通过SSH执行Linux命令的方法及代码
- 非法ip通过ssh成功登录,自动结束会话
- 无需密码通过ssh执行rsync来同步文件的方法
- 客户端通过SSH免密码登录及执行服务器命令
- bash deploy.sh 通过bash命令 执行scp -r 命令将本地文件拷贝到服务器