Source Shell(SSH)
全称: 安全外壳协议
简称: shell = sh
实质与用途: 是一种加密的网络传输协议,用于在不安全网络中为网络服务提供安全的传输环境
由来: 作为 替代 Telnet 及 非安全 Shell 等旧·明文传输协议 而 诞生
现今: SSH 已可破解 [CIA 及 中国]
注0: 仅以写文时的需求,提供仅需信息,完整信息源为:
0.Linux man 命令
1.cat /usr/bin/ssh-copy-id
2.https://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html
3.https://www.ruanyifeng.com/blog/2011/12/ssh_port_forwarding.html [含参考文献]
Linux ssh 命令常用功能:
0.命令格式
ssh [-option] destination [command]
注: destination 目的/终点 => 数据传输终点 (相较于 执行 该 SSH 命令的机器(坐标原点) 而言)
1.密码远程Shell登录
$ ssh user@host
if localuser == remoteuser
$ ssh host
if default port != 22
$ ssh -p 2222 user@host
注1: 第一次登录时 会提示你 是否接受 显示的 RSA key fingerprint,
如果接受就会将 该远程主机公钥存储至当前用户 $HOME/.ssh/known_hosts 中
下次再连接就会 根据 known_hosts 文件中 记录 跳过 warning 提示
2.公私钥远程Shell登录
2.1 建立 公私钥
$ ssh-keygen -C "something@email.com"
注2: ssh-keygen can create keys for use by SSH protocol version 2
2.2 传输 公钥
$ ssh-copy-id user@host
实质: ssh登录服务端 然后 创建目录 创建文件 追加客户端公钥
ssh user@host "exec sh -c 'cd ; umask 077 ; mkdir -p .ssh
&& { [ -z "'`tail -1c .ssh/authorized_keys 2>/dev/null`'" ] || echo >> .ssh/authorized_keys ; }
&& cat >> .ssh/authorized_keys || exit 1 ;'" < ~/.ssh/id_rsa.pub
[2.3] 如果仍然需要输入密码 则 检查 远程主机的/etc/ssh/sshd_config 打开注释/新增
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
[2.4] 并且重启 ssh 服务,指定开机启动
# centos 7
systemctl restart sshd.service
systemctl enable sshd.service
3.远程Shell命令执行及数据传输
# 在远程主机执行命令
$ ssh user@host 'ps -ef | grep httpd'
# 将本地文件 追加至 远程主机文件
$ ssh user@host 'mkdir -p .ssh && cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub
# 将本地目录 压缩传输至 远程主机 家目录
$ cd && tar zcv src | ssh user@host 'tar zx'
# 将远程主机目录 压缩传输至 本地当前目录
$ ssh user@host 'tar cz src' | tar xzv
4.本地端口转发 | SSH隧道
host1 <--> host3 <--> host2
本机 中间 远端
# 例1: 本机 host1 将 远端 host2:21 端口 透过 中间 host3 代理至本机 host1:21 端口
# host1 执行
$ ssh -L 2121:host2:21 host3
注1: ssh -L 本机端口:目标主机地址:目标主机端口 中间主机地址
注2: 目标主机地址 是 相对于 destination 而言,下例 表示 将本机5900端口绑定在host3的5900端口
$ ssh -L 5900:localhost:5900 host3
# 例2: 代理 ssh 22 端口,在本机 9001 端口登录
$ ssh -L 9001:host2:22 host3
$ ssh -p 9001 localhost
5.远端端口转发 | 中间代理
host1 -x-> host3
host1 <-✔- host3 <--> host2
远端1 本机 远端2
# 例1: host3 将 远端1 host1:2121 端口所有数据 转发至 远端2 host2:21
# host3 执行
$ ssh -R 2121:host2:21 host1
注1: ssh -R 远端主机端口:目标主机地址:目标主机端口 远端主机地址
注2: 前提条件,host1 && host3 两台主机都有 sshD 和 ssh 客户端
其他常用参数
-N 只连接远程主机,不打开 Shell
-T 不为这个连接分配 TTY
共同使用情景: 代表这个 SSH 连接 只用来传输数据 不执行远程操作
-f 后台运行 在 SSH 连接成功后
注: 暂未验证 4,5 待验证后 给出具体实践case
未整理信息
-L [bind_address:]port:host:hostport
-L [bind_address:]port:remote_socket
-L local_socket:host:hostport
-L local_socket:remote_socket
Specifies that connections to the given TCP port or Unix socket on the local (client) host are to be forwarded to the given host
and port, or Unix socket, on the remote side. This works by allocating a socket to listen to either a TCP port on the local
side, optionally bound to the specified bind_address, or to a Unix socket. Whenever a connection is made to the local port or
socket, the connection is forwarded over the secure channel, and a connection is made to either host port hostport, or the Unix
socket remote_socket, from the remote machine.
Port forwardings can also be specified in the configuration file. Only the superuser can forward privileged ports. IPv6
addresses can be specified by enclosing the address in square brackets.
By default, the local port is bound in accordance with the GatewayPorts setting. However, an explicit bind_address may be used
to bind the connection to a specific address. The bind_address of ``localhost’’ indicates that the listening port be bound for
local use only, while an empty address or `*’ indicates that the port should be available from all interfaces.
指定到本地(客户端)主机上给定TCP端口或Unix套接字的连接将被转发到给定主机
和远程端端口,或Unix套接字。它的工作原理是分配一个套接字来监听本地的TCP端口
端,可以选择绑定到指定的bind_address或Unix套接字。每当连接到本地端口或
套接字,连接通过安全通道转发,连接到主机端口hostport或Unix
socket remote_socket,从远程机器。
端口转发也可以在配置文件中指定。只有超级用户才能转发特权端口。IPv6地址可以通过将地址括在方括号中来指定。
缺省情况下,本端端口按照GatewayPorts的配置进行绑定。但是,可以使用显式的bind_address
将连接绑定到特定的地址。’’ localhost "的bind_address表示要绑定的监听端口
仅在本地使用,而空地址或’ *'表示端口应该从所有接口可用。
-R [bind_address:]port:host:hostport
-R [bind_address:]port:local_socket
-R remote_socket:host:hostport
-R remote_socket:local_socket
-R [bind_address:]port
Specifies that connections to the given TCP port or Unix socket on the remote (server) host are to be forwarded to the local
side.
This works by allocating a socket to listen to either a TCP port or to a Unix socket on the remote side. Whenever a connection
is made to this port or Unix socket, the connection is forwarded over the secure channel, and a connection is made from the local
machine to either an explicit destination specified by host port hostport, or local_socket, or, if no explicit destination was
specified, ssh will act as a SOCKS 4/5 proxy and forward connections to the destinations requested by the remote SOCKS client.
Port forwardings can also be specified in the configuration file. Privileged ports can be forwarded only when logging in as root
on the remote machine. IPv6 addresses can be specified by enclosing the address in square brackets.
By default, TCP listening sockets on the server will be bound to the loopback interface only. This may be overridden by specify-
ing a bind_address. An empty bind_address, or the address `*’, indicates that the remote socket should listen on all interfaces.
Specifying a remote bind_address will only succeed if the server’s GatewayPorts option is enabled (see sshd_config(5)).
If the port argument is `0’, the listen port will be dynamically allocated on the server and reported to the client at run time.
When used together with -O forward the allocated port will be printed to the standard output.
指定到远程(服务器)主机上给定TCP端口或Unix套接字的连接将被转发到本地的一面。
这是通过分配一个套接字来监听远程端的TCP端口或Unix套接字来实现的。当一个连接
将连接发送到此端口或Unix套接字,连接将通过安全通道转发,连接将从本地
将机器转移到由主机端口hostport或local_socket指定的显式目标,或者(如果没有显式目标的话)
指定,ssh将充当SOCKS 4/5代理,并将连接转发到远程SOCKS客户机请求的目的地。
端口转发也可以在配置文件中指定。特权端口只有在以root身份登录时才能转发
在远程机器上。IPv6地址可以用方括号括起来指定。
缺省情况下,服务器端的TCP监听套接字只绑定到loopback接口。这可以被指定-覆盖
荷兰国际集团(ing) bind_address。空的bind_address或地址’ *'表示远程套接字应该监听所有接口。
只有启用了服务器的GatewayPorts选项,才能成功指定远程bind_address(参见sshd_config(5))。
如果port参数为’ 0’,监听端口将在服务器上动态分配,并在运行时报告给客户端。
当与-O forward一起使用时,分配的端口将被打印到标准输出。
SSH(Secure Shell)是一种加密网络协议,用于在不安全的网络环境中提供安全的数据传输。本文介绍了SSH的基本概念,包括如何进行远程登录(如公私钥认证)、执行远程命令、数据传输以及端口转发(本地端口转发和远程端口转发)。此外,还详细阐述了端口转发的使用场景和命令格式,如-L和-R选项,以及如何设置和验证SSH隧道。
920

被折叠的 条评论
为什么被折叠?



