一个系统管理员可能会同时管理着多台服务器,这些服务器也许会放在不同的地方。要亲自一台一台的去访问来管理它们显然不是最好的方法,通过远程控制的方法应该是最有效的。
linux系统的远程管理工具大概有几种:telnet,ssh,vnc等,其中ssh是最常用的管理方法,采用密文的传输方式,简单安全。
secure shell缩写是ssh, 由ietf的网络工作小组(network working group)所制定,ssh是一项创建在应用层和传输层基础上的安全协议,为计算机的shell提供安全的传输和使用环境。
下面我们来介绍 ssh 的 7 大用法。
1. 基本用法
最简单的用法就是不带参数,仅输入 ssh 再加上主机地址,比如:
ssh 192.168.0.116
这种形式登陆主机,会默认使用当前用户进行登录。第一次连接的时候,ssh 会确认目标主机的真实性,如果没有问题的话,输入 yes 即可。
如果我们想要以指定用户名来登录主机,有两种方法:
a. 使用 -l 选项
ssh -l alvin 192.168.0.116
b. 使用 user@hostname 格式
ssh alvin@192.168.0.116
这两种方法,其中第二种尤为常用。
2. 指定端口登录
ssh 默认使用的端口号是 22。大多现代的 linux 系统 22 端口都是开放的。如果你运行 ssh 程序而没有指定端口号,它直接就是通过 22 端口发送请求的。
如果我们不想通过 22 端口登录,那么我们可以使用 -p 选项来指定端口。
ssh 192.168.0.116 -p 1234
引申话题:如何修改端口号?
只需修改 /etc/ssh/ssh_config ,修改如下一行:
port 22
3. 对所有数据请求压缩
使用 -c 选项,所有通过 ssh 发送或接收的数据将会被压缩,并且任然是加密的。
ssh -c 192.168.0.116
但是,这个选项在网速不是很快的时候比较有用,而当网速较快的时候,使用压缩反而会降低效率,所以要视情况使用。
4. 打开调试模式
因为某些原因,我们想要追踪调试我们建立的 ssh 连接情况。ssh 提供的 -v 选项参数正是为此而设的。其可以看到在哪个环节出了问题。
[alvin.alvin-computer] ➤ ssh -v pi@192.168.0.116 openssh_7.1p2, openssl 1.0.1g 7 apr 2014 debug1: reading configuration data /etc/ssh_config debug1: connecting to 192.168.0.116 [192.168.0.116] port 22. debug1: connection established. debug1: key_load_public: no such file or directory debug1: enabling compatibility mode for protocol 2.0 debug1: local version string ssh-2.0-openssh_7.1 debug1: remote protocol version 2.0, remote software version openssh_7.4p1 raspbian-10+deb9u4 debug1: match: openssh_7.4p1 raspbian-10+deb9u4 pat openssh* compat 0x04000000 debug1: authenticating to 192.168.0.116:22 as 'pi' debug1: ssh2_msg_kexinit sent debug1: ssh2_msg_kexinit received
5. 绑定源地址
如果你的客户端有多于两个以上的 ip 地址,你就不可能分得清楚在使用哪一个 ip 连接到 ssh 服务器。为了解决这种情况,我们可以使用 -b 选项来指定一个ip 地址。这个 ip 将会被使用做建立连接的源地址。
[alvin.alvin-computer] ➤ ssh -b 192.168.0.105 pi@192.168.0.116 linux raspberrypi 4.14.71-v7+ #1145 smp fri sep 21 15:38:35 bst 2018 armv7l the programs included with the debian gnu/linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. debian gnu/linux comes with absolutely no warranty, to the extent permitted by applicable law. last login: sun feb 24 08:52:29 2019 from 192.168.0.105
6. 远程执行命令
如果我们想在目标主机执行一条命令,我们通常的做法是,先登录到目标主机,执行命令,再退出来。这样做当然是可以,但是比较麻烦。
如果我们仅仅是想远程执行一条命令,可以直接在后面跟上命令就好,如下:
[alvin.alvin-computer] ➤ ssh pi@192.168.0.116 ls -l desktop documents downloads magpi music
7. 挂载远程文件系统
另外一个很赞的基于 ssh 的工具叫 sshfs。 sshfs 可以让你在本地直接挂载远程主机的文件系统。它的使用格式如下:
sshfs -o idmap=user user@hostname:/home/user ~/remote
比如:
sshfs -o idmap=user pi@192.168.0.116:/home/pi ~/pi
以上所述是小编给大家介绍的linux ssh命令详解整合,希望对大家有所帮助
您可能感兴趣的文章:
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!
网友评论