Linux commands (126) - ssh command

1. Introduction command

ssh (Secure SHell) command is OpenSSH part of the suite, it is SSH remote login service client program used to log on the remote host.

ssh is used to log into a remote machine and program commands are executed on the remote machine. It is intended to replace rlogin and rsh, and provide secure encrypted communications between two hosts on an untrusted network insecure. X11 connections and arbitrary TCP ports can also be forwarded over the secure channel.

2. Command Format

ssh [OPTIONS] [-p PORT] [USER@]HOSTNAME [COMMAND]

ssh most commonly used functions are logged on the remote host, choose what users are connected which machine, and then enter the password.

3. Option Description

-1
    强制只使用协议第一版
-2
    强制只使用协议第二版
-4
    强制只使用 IPv4 地址.
-6
    强制只使用 IPv6 地址
-A
    允许转发认证代理的连接。可以在配置文件中对每个主机单独设定这个参数
-a
    禁止转发认证代理的连接
-b BIND_ADDRESS
    在拥有多个地址的本地机器上,指定连接的源地址
-C
	压缩所有数据。压缩算法与 gzip(1) 使用的相同
-c {blowfish | 3des | des}
    选择会话的密码算法。3des 是默认算法
-c CIPHER_SPEC
    另外, 对于协议第二版,这里可以指定一组用逗号隔开、按优先顺序排列的加密算法
-D [BIND_ADDRESS:]PORT
	指定一个本地主机动态的应用程序级的转发端口。工作原理是这样的,本地机器上分配了一个 socket 侦听 port 端口,一旦这个端口上有了连接,该连接就经过安全通道转发出去,根据应用程序的协议可以判断出远程主机将和哪里连接。目前支持 SOCKS4 和 SOCKS5 协议,而 ssh 将充当 SOCKS 服务器. 只有 root 才能转发特权端口。可以在配置文件中指定动态端口的转发
-e ESCAPE_CHAR
	设置 pty 会话的转义字符,默认为字符 ~。转义字符只在行首有效,转义字符后面跟一个点表示结束连接,后跟一个 control-Z 表示挂起连接,跟转义字符自己表示输出转义字符自身。把转义字符设为 none 则禁止 转义功能,使会话完全透明
-F CONFIGFILE
	指定 ssh 指令的配置文件,将忽略系统级配置文件 /etc/ssh/ssh_config 和用户级配置文件 ~/.ssh/config
-f 
    ssh 在执行命令前退至后台
-g
    允许远端主机连接本地的转发端口
-I SMARTCARD_DEVICE
    指定智能卡设备。智能卡里面存储了用户的 RSA 私钥
-i IDENTITY_FILE
    指定一个 RSA 或 DSA 认证所需的身份(私钥)文件。协议第一版的默认文件是 ~/.ssh/identity 以及协议第二版的 ~/.ssh/id_rsa 和 ~/.ssh/id_dsa 文件。可以同时使用多个 -i 选项,也可以在配置文件中指定多个身份文件
-K
	启用基于 GSSAPI 的身份验证和向服务器转发 GSSAPI 凭据
-k
   禁用向服务器转发 GSSAPI 凭据
-L [BIND_ADDRESS:]PORT:HOST:HOSTPORT
	将本地主机的地址和端口接收到的数据通过安全通道转发给远程主机的地址和端口
-l LOGIN_NAME
    指定登录远程主机的用户。可以在配置文件中对每个主机单独设定这个参数
-M
	将 ssh 客户端置于主模式进行连接共享。多个 -M 选项将 ssh 置于主模式,并在接受从连接之前进行确认
-m MAC_SPEC
	对于协议第二版,可以指定一组用逗号隔开,按优先顺序排列的 MAC (message authentication code) 算法
-N
    不执行远程命令,用于转发端口。仅限协议第二版
-n
	把 stdin 重定向到 /dev/null,防止从 stdin 读取数据。在后台运行时一定会用到这个选项
-O CTL_CMD
	控制主动连接多路复用主进程。参数 CTL_CMD 将被传递给主进程。CTL_CMD 可取值 check(检查主进程是否正在运行)和 exit(让主进程退出)
-o OPTION
    可以在这里给出某些选项,格式和配置文件中的格式一样。它用来设置那些没有单独的命令行标志的选项
-p PORT
    指定远程主机的端口。可以在配置文件中对每个主机单独设定这个参数
-q
    安静模式。消除大多数的警告和诊断信息
-R [BIND_ADDRESS:]PORT:HOST:HOSTPORT
	将远程主机上的地址和端口接收的数据通过安全通道转发给本地主机的地址和端口
-S CTL_PATH
	指定用于连接共享的控制套接字的位置
-s
    用于请求远程系统上的子系统调用。子系统是 SSH2 协议的一个特性,它有助于将 SSH 用作其他应用程序(如 sftp(1))的安全传输。子系统通过远程命令指定
-T
    禁止分配伪终端
-t
	强制分配伪终端。这可用于在远程计算机上执行基于屏幕的任意程序,例如菜单服务。多个 -t  选项强制分配终端, 即使没有本地终端
-V
	显示版本信息并退出
-v
    冗详模式。打印关于运行情况的调试信息。在调试连接、认证和配置问题时非常有用。多个 -v 选项能够增加冗详程度,最多三个
-W HOST:PORT
	将客户端上的标准输入和输出通过安全通道转发给指定主机的端口
-w LOCAL_TUN[:REMOTE_TUN]
	指定客户端和服务端之间转发的隧道设备
-X
    允许 X11 转发,可以在配置文件中对每个主机单独设定这个参数
-x
    禁止 X11 转发
-Y
	启用受信任的 X11 转发。受信任的 X11 转发不受 X11 安全扩展控制的约束
-y
	使用 syslog(3) 系统模块发送日志信息。默认情况下,此信息被发送到 stderr

4. Common examples

(1) using the specified user login name and port of the remote host.

ssh -p3600 [email protected]

Enter the user completes the login login password. Unspecified port and user name, respectively, by the configuration file ~ / .ssh / ssh_config and / etc / ssh / Port and User options in ssh_config decision. If the configuration file is not specified, the default port 22, the default user name for the current user.

Note: ssh obtain the relevant parameters in the following order:

1.command-line options
2.user's configuration file (~/.ssh/config)
3.system-wide configuration file (/etc/ssh/ssh_config)

ssh use asymmetric encryption for secure login, asymmetric encryption, there are two keys: a public key and a private key. Generating a public key from a secret key, but can not calculate the private key; the public key encrypted ciphertext can only be decrypted by the corresponding private key. Log asymmetric encryption process is as follows:
Here Insert Picture Description
the entire login process is as follows:

1.topgun 终端要登录 Server 服务器,发起登录请求 `ssh [email protected]`。
2.服务端运行有 sshd 服务,并监听指定的端口,默认为 22 号端口。服务端会生成一对公钥和私钥;此时将公钥返回给客户端;
3.客户端使用公钥,对登录密码进行加密(如服务器work用户密码为xxx),生成公钥加密字符串;
4.客户端将公钥加密字符串发送给服务端;
5.服务端使用私钥,解密公钥加密字符串,得到原始密码;
6.校验密码是否合法(此为本机 work 密码);
7.返回登录结果给客户端:成功登录或密码错。

In asymmetric encryption, since only the public key is transmitted, while the private key is saved locally on the server, so even if the public key is listening, you can not get the original password, thereby securely log server.

(2) executes a specified command on a remote host, the remote host to check the partitions as designated device.

ssh -p3600 [email protected] fdisk -l /dev/vda
root's password: 
Authentication successful.

Disk /dev/vda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xf6abafec

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1              63   209712509   104856223+  83  Linux

(3) to achieve free Log in secret by ssh. Free secret login process is as follows:
Here Insert Picture Description

1.在客户端使用 ssh-keygen 生成一对密钥:公钥+私钥;
2.将客户端公钥追加到服务端的 authorized_key 文件中,完成公钥认证操作;
3.认证完成后,客户端向服务端发起登录请求,并传递公钥到服务端;
4.服务端检索 authorized_key 文件,确认该公钥是否存在。如果存在该公钥,则生成随机数 R,并用公钥进行加密,生成公钥加密字符串 pubKey(R);
5.将公钥加密字符串传递给客户端;
6客户端使用私钥解密公钥加密字符串,得到 R;
7.服务端和客户端通信时会产生一个会话 ID(sessionKey),用 MD5 对 R 和 SessionKey 进行加密,生成摘要;
8.客户端将生成的 MD5 加密字符串传给服务端;
9.服务端同样生成 MD5(R,SessionKey) 加密字符串;
10.如果客户端传来的加密字符串等于服务端自身生成的加密字符串,则认证成功。此时不用输入密码,即完成建连,可以开始远程执行 Shell 命令了。

The first step in using the ssh-keygen command on the client to generate RSA public and private keys, has been press Enter. Public and private keys default name for the id_rsa.pub (public key) and private key (id_rsa), by default stored in ~ / .ssh directory under.

ssh-keygen -t rsa

The second step will be added to the client public key server ~ / .ssh / authorized_keys file, authorized_keys public key is used to store client files. There are three ways, first by ssh-copy-id command, the second is through scp command, the third is to manually copy. For example, ssh-copy-id command is implemented as follows:

ssh-copy-id -i ~/.ssh/id_rsa.pub -p 3600 [email protected]

The third step is to use ssh for a free confidential login.

ssh -p3600 [email protected]

references

[1] SSH (1) Manual
[2] ssh_config (5) Manual
[3] OpenSSH official website
[4] SSH login-free secret principle and implementation of the blog garden .linux

Published 519 original articles · won praise 1567 · Views 2.02 million +

Guess you like

Origin blog.csdn.net/K346K346/article/details/104616612