通过 ssh 会话执行 bash 别名 | Linux 中国

2018 年 1 月 22 日 Linux中国 译者:lujun9972
我在远程主机上上设置过一个叫做 file_repl 的 bash 别名 。当我使用 ssh 命令登录远程主机后,可以很正常的使用这个别名。然而这个 bash 别名却无法通过 ssh 来运行
-- Vivek Gite

本文导航
编译自 | https://www.cyberciti.biz/faq/use-bash-aliases-ssh-based-session/ 
 作者 | Vivek Gite
 译者 | lujun9972

我在远程主机上上设置过一个叫做 file_repl 的 bash 别名 [1]。当我使用 ssh 命令登录远程主机后,可以很正常的使用这个别名。然而这个 bash 别名却无法通过 ssh 来运行,像这样:

   
     
     
     
  1. $ ssh vivek@server1.cyberciti.biz file_repl

  2. bashfile_replcommand not found

我要怎样做才能通过 ssh 命令运行 bash 别名呢?

SSH 客户端 (ssh) 是一个登录远程服务器并在远程系统上执行 shell 命令的 Linux/Unix 命令。它被设计用来在两个非信任的机器上通过不安全的网络(比如互联网)提供安全的加密通讯。

如何用 ssh 客户端执行命令

通过 ssh 运行 free 命令或 date 命令[2] 可以这样做:

   
     
     
     
  1. $ ssh vivek@server1.cyberciti.biz date

结果为:

   
     
     
     
  1. Tue Dec 26 090250 UTC 2017

或者:

   
     
     
     
  1. $ ssh vivek@server1.cyberciti.biz free -h

结果为:

   
     
     
     
  1. total used free shared buff/cache available

  2. Mem2.0G 428M 138M 145M 1.4G 1.1G

  3. Swap0B 0B 0B

理解 bash shell 以及命令的类型

bash shell[3] 共有下面几类命令:

☉ 别名,比如  ll
☉ 关键字,比如  if
☉ 函数 (用户自定义函数,比如  genpasswd
☉ 内置命令,比如  pwd
☉ 外部文件,比如  /bin/date

type 命令[4] 和 command 命令[5] 可以用来查看命令类型:

   
     
     
     
  1. $ type -a date

  2. date is /bin/date

  3. $ type -a free

  4. free is /usr/bin/free

  5. $ command -V pwd

  6. pwd is a shell builtin

  7. $ type -a file_repl

  8. is aliased to `sudo -i /shared/takes/master.replication'

date 和 free 都是外部命令,而 file_repl 是 sudo -i /shared/takes/master.replication 的别名。你不能直接执行像 file_repl 这样的别名:

   
     
     
     
  1. $ ssh user@remote file_repl

在 Unix 系统上无法直接通过 ssh 客户端执行 bash 别名

要解决这个问题可以用下面方法运行 ssh 命令:

   
     
     
     
  1. $ ssh -t user@remote /bin/bash -ic 'your-alias-here'

  2. $ ssh -t user@remote /bin/bash -ic 'file_repl'

ssh 命令选项:

◈  -t强制分配伪终端。可以用来在远程机器上执行任意的 [6] 基于屏幕的程序,有时这非常有用。当使用  -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 别名,可以运行下面命令:

   
     
     
     
  1. $ ssh -t vivek@server1.cyberciti.biz -ic 'll'

结果为:

Running bash aliases over ssh based session when using Unix or Linux ssh cli

下面是我的一个 shell 脚本的例子:

   
     
     
     
  1. #!/bin/bash

  2. I="tags.deleted.410"

  3. O="/tmp/https.www.cyberciti.biz.410.url.conf"

  4. box="vivek@server1.cyberciti.biz"

  5. [!-f "$I" ] && { echo "$I file not found。"; exit 10; }

  6. >$O

  7. cat "$I" | sort | uniq | while read -r u

  8. do

  9.    uu="${u##https://www.cyberciti.biz}"

  10.    echo "~^$uu 1;" >>"${O}"

  11. done

  12. echo "Config file created at ${O} and now updating remote nginx config file"

  13. scp "${O}" ${box}:/tmp/

  14. ssh ${box} /usr/bin/lxc file push /tmp/https.www.cyberciti.biz.410.url.conf nginx-container/etc/nginx/

  15. ssh -t ${box} /bin/bash -ic 'push_config_job'

相关资料

更多信息请输入下面命令查看 OpenSSH 客户端[7] 和 bash 的 man 帮助 [8]

   
     
     
     
  1. $ man ssh

  2. $ man bash

  3. $ help type

  4. $ help command


via: https://www.cyberciti.biz/faq/use-bash-aliases-ssh-based-session/

作者:Vivek Gite[10] 译者:lujun9972 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

LCTT 译者
lujun9972 🌟 🌟 🌟 🌟
共计翻译: 75 篇
贡献时间:55 天

推荐文章

< 左右滑动查看相关文章 >

点击图片、输入文章 ID 或识别二维码直达




登录查看更多
0

相关内容

Secure Shell(SSH)为创建在应用层和传输层基础上的安全协议。
算法与数据结构Python,369页pdf
专知会员服务
160+阅读 · 2020年3月4日
TensorFlow Lite指南实战《TensorFlow Lite A primer》,附48页PPT
专知会员服务
68+阅读 · 2020年1月17日
【新书】Python中的经典计算机科学问题,224页PDF
专知会员服务
51+阅读 · 2019年12月31日
【电子书】Flutter实战305页PDF免费下载
专知会员服务
20+阅读 · 2019年11月7日
机器学习相关资源(框架、库、软件)大列表
专知会员服务
37+阅读 · 2019年10月9日
MIT新书《强化学习与最优控制》
专知会员服务
270+阅读 · 2019年10月9日
通过Docker安装谷歌足球游戏环境
CreateAMind
11+阅读 · 2019年7月7日
谷歌足球游戏环境使用介绍
CreateAMind
31+阅读 · 2019年6月27日
Pupy – 全平台远程控制工具
黑白之道
43+阅读 · 2019年4月26日
Linux挖矿病毒的清除与分析
FreeBuf
14+阅读 · 2019年4月15日
I2P - 适用于黑客的Android应用程序
黑白之道
28+阅读 · 2019年3月6日
百度开源项目OpenRASP快速上手指南
黑客技术与网络安全
5+阅读 · 2019年2月12日
DiscuzX 3.4 Phar反序列化漏洞
黑客工具箱
8+阅读 · 2019年1月4日
如何用GitLab本地私有化部署代码库?
Python程序员
9+阅读 · 2018年12月29日
Next Item Recommendation with Self-Attention
Arxiv
5+阅读 · 2018年8月25日
Neural Arithmetic Logic Units
Arxiv
5+阅读 · 2018年8月1日
Arxiv
6+阅读 · 2018年5月18日
VIP会员
相关资讯
通过Docker安装谷歌足球游戏环境
CreateAMind
11+阅读 · 2019年7月7日
谷歌足球游戏环境使用介绍
CreateAMind
31+阅读 · 2019年6月27日
Pupy – 全平台远程控制工具
黑白之道
43+阅读 · 2019年4月26日
Linux挖矿病毒的清除与分析
FreeBuf
14+阅读 · 2019年4月15日
I2P - 适用于黑客的Android应用程序
黑白之道
28+阅读 · 2019年3月6日
百度开源项目OpenRASP快速上手指南
黑客技术与网络安全
5+阅读 · 2019年2月12日
DiscuzX 3.4 Phar反序列化漏洞
黑客工具箱
8+阅读 · 2019年1月4日
如何用GitLab本地私有化部署代码库?
Python程序员
9+阅读 · 2018年12月29日
Top
微信扫码咨询专知VIP会员