OpenSSH是使用SSH協議進行遠程連接的工具,當前最新版本是OpenSSH 7.7,於 April 03, 2018 釋出。
OpenSSH可分爲 client 和 server 端。其中 client 的配置文件有2個,一個是全局配置文件/etc/ssh/ssh_config
,另一個是用戶配置文件~/.ssh/config
,默認後者中的設置會覆蓋前者(全局配置文件)。通過配置~/.ssh/config
,可實現SSH的自定義操作。
本文主要介紹~/.ssh/config
的配置、使用,實驗主機爲Digital Ocean的VPS主機。
Preparation
在Digital Ocean中創建2台VPS,系統選用Debian Stretch 9.5
,一台作爲跳板機,一台作爲內網主機,只能通過跳板機訪問,通過SSH keygen進行認證連接。
VPS信息如下
Hostname | Intern IP | Public IP | Port |
---|---|---|---|
front | 10.128.29.79 |
162.243.63.4 |
22 |
secret | 10.128.24.198 |
162.243.86.165 |
22 |
在主機front
中創建ssh genkey,key的類型選擇ed25519
,將公鑰中的內容添加到主機secret
的~/.ssh/authorized_key
中。此處假設已經進行防火牆配置,只允許通過主機front
訪問主機secret
。
爲方便進行實驗,將在主機front
生成的密鑰對保存到本地主機/tmp
目錄下。
測試過程如下
|
|
Introduction
ssh_config
的說明可通過如下命令查看
|
|
也可參閱文檔 SSH Config File for OpenSSH Client
通過~/.ssh/config
配置的主機,可以通過ssh
、sftp
等命令進行連接。
Parameters List
詳細配置參數清單如下
parameter | detail |
---|---|
Host | |
Match | |
AddKeysToAgent | |
AddressFamily | |
BatchMode | |
BindAddress | |
CanonicalDomains | |
CanonicalizeFallbackLocal | |
CanonicalizeHostname | |
CanonicalizeMaxDots | |
CanonicalizePermittedCNAMEs | |
CertificateFile | |
ChallengeResponseAuthentication | |
CheckHostIP | |
Cipher | |
Ciphers | |
ClearAllForwardings | |
Compression | |
CompressionLevel | |
ConnectionAttempts | |
ConnectTimeout | |
ControlMaster | |
ControlPath | |
ControlPersist | |
DynamicForward | |
EnableSSHKeysign | |
EscapeChar | |
ExitOnForwardFailure | |
FingerprintHash | |
ForwardAgent | |
ForwardX11 | |
ForwardX11Timeout | |
ForwardX11Trusted | |
GatewayPorts | |
GlobalKnownHostsFile | |
GSSAPIAuthentication | |
GSSAPIKeyExchange | |
GSSAPIClientIdentity | |
GSSAPIServerIdentity | |
GSSAPIDelegateCredentials | |
GSSAPIRenewalForcesRekey | |
GSSAPITrustDns | |
HashKnownHosts | |
HostbasedAuthentication | |
HostbasedKeyTypes | |
HostKeyAlgorithms | |
HostKeyAlias | |
HostName | |
IdentitiesOnly | |
IdentityAgent | |
IdentityFile | |
IgnoreUnknown | |
Include | |
IPQoS | |
KbdInteractiveAuthentication | |
KbdInteractiveDevices | |
KexAlgorithms | |
LocalCommand | |
LocalForward | |
LogLevel | |
MACs | |
NoHostAuthenticationForLocalhost | |
NumberOfPasswordPrompts | |
PasswordAuthentication | |
PermitLocalCommand | |
PKCS11Provider | |
Port | |
PreferredAuthentications | |
Protocol | |
ProxyCommand | |
ProxyJump | |
ProxyUseFdpass | |
PubkeyAcceptedKeyTypes | |
PubkeyAuthentication | |
RekeyLimit | |
RemoteForward | |
RequestTTY | |
RevokedHostKeys | |
RhostsRSAAuthentication | |
RSAAuthentication | |
SendEnv | |
ServerAliveCountMax | |
ServerAliveInterval | |
StreamLocalBindMask | |
StreamLocalBindUnlink | |
StrictHostKeyChecking | |
TCPKeepAlive | |
Tunnel | |
TunnelDevice | |
UpdateHostKeys | |
UsePrivilegedPort | |
User | |
UserKnownHostsFile | |
VerifyHostKeyDNS | |
VisualHostKey | |
XAuthLocation |
Format
在文件~/.ssh/config
中配置的格式如下
|
|
以Host
開頭,配額選項以key val
形式設置,縮進1個Tab(或4個空格)。
Host *
表示通用配置,適用於~/.ssh/config
所配置的主機。Host front
則表示針對主機front
進行配置,如果通用配置和特定主機中的配置選項重複,則自動忽略通用配置中的選項。
Simple Login
使用SSH登錄遠程主機,需要提供遠程主機的IP、端口號、用戶名、密碼或key。則ssh_config
中對應的參數選項如下
item | para |
---|---|
IP | HostName |
端口號 | Port |
用戶名 | User |
key | IdentityFile |
在最簡配置中,只要指定這4個選項即可。
如主機front
的配置
|
|
處於安全等考慮,還需進行其他參數的配置。
Common Configuration
其中某些參數的配置,可參考Mozilla的Security/Guidelines/OpenSSH。
如果想通過socket進行通信,可使用指令ControlMaster
、ControlPath
、ControlPersist
,創建目錄~/.ssh/sockets/
存放生成的socket文件。
|
|
通用配置如下
|
|
Visit Intern Host Via Front
配置通過主機front
訪問內網主機secret
(10.128.24.198),通過參數ProxyCommand
實現
In the command string, any occurrence of
%h
will be substituted by the host name to connect,%p
by the port, and%r
by the remote user name.
在ProxyCommand
中,%h
指代要連接的主機名,%p
指代端口號,%r
指代用戶名,SSH會自動進行替換。
Method 1
方式1通過ssh front -W %h:%p %r
實現
注意
- ssh後的 front 是跳板主機的主機名稱,即
Host front
中的front
; - 主機
secret
指定的key是主機front
連接secret
時使用的key;
|
|
演示過程如下
|
|
Method 2 nc
方式2通過ssh front nc %h %p %r
實現,但是有前提,命令中的nc
必須已經安裝在主機front
中,否則會報錯,無法正常連接。
|
|
Method 3 ProxyJump
OpenSSH 7.3中添加了ProxyJump
選項,通過該選項可實現與ProxyCommand
相同的功能,但設置更爲簡單。
注意:要想使用ProxyJump
,須確保OpenSSH的版本至少爲7.3
。
具體實例可參見What is new in OpenSSH 7.4 (in RHEL 7.4)?中的示例。
|
|
Port Forward
端口轉發分爲LocalForward
、RemoteForward
、DynamicForward
這3種。
在主機front
中進行操作
LocalForward
在主機front
安裝Nginx,在瀏覽器中輸入外網IP162.243.63.4
即能訪問Web內容,默認爲80端口。通過本地端口轉發,將主機front
的 80 端口轉發到本機的 9999 端口,實現通過訪問本地127.0.0.1:9999
獲取主機front
中的Web頁面,即80端口顯示的內容。
配置如下內容
|
|
執行
|
|
使用ss -tnl
可查看到本機已經在監聽9999
端口。
注意:僅指定-fNg
即可,不要添加L
、R
、D
。如果設置爲-fNgL
,會出現報錯
Bad local forwarding specification
提取Head信息比對
|
|
RemoteForward
本機安裝有Nginx,在瀏覽器中輸入127.0.0.1
即能訪問Web內容,默認爲80端口。通過遠程端口轉發,將本機的 80 端口轉發到遠程主機front
的 7777 端口。實現在主機front
中通過訪問本地127.0.0.1:7777
獲取本機中的Web頁面,即80端口顯示的內容。
注意:使用主機front
進行端口轉發,須在主機front
的/etc/ssh/sshd_config
中將選項GatewayPorts
設置爲yes
|
|
然後重啓sshd服務
|
|
配置如下內容
|
|
注意:RemoteForward
的第一個參數爲遠程主機front
要啓用的端口號,第二個參數127.0.0.1:80
爲本機的端口信息。
執行
|
|
可在主機front
中看到已經監聽端口7777
。
提取Head信息比對
|
|
DynamicForward
動態端口轉發,創建SOCKS,在本機監聽6666
端口
配置如下內容
|
|
執行
|
|
使用ss -tnl
可查看到本機已經在監聽6666
端口。
X Forwarding
X
is a popular window system for Unix workstations, and one of its best features is its transparency. UsingX
, you can run remote X applications that open their windows on your local display (and vice versa, running local applications on remote displays). Unfortunately, the inter-machine communication is insecure and wide open to snoopers. But there’s good news: SSH X forwarding makes the communication secure by tunneling the X protocol. – https://docstore.mik.ua/orelly/networking_2ndEd/ssh/ch09_03.htm
X
是一種通信協議,通過該協議,可將遠程主機中的應用在本地窗口中打開,通信默認是不安全的,通過 SSH X Forwarding 可確保通信安全。
因X
協議在認證請求連接的客戶端時,需要對客戶端進行身份認證,方式可分爲兩種Host-based X
和Key-based X
。後者通過程序xauth
維護X的認證key,通常保存在文件~/.Xauthority
中。
Step 1 xauth
故首先須確保在本地和遠程主機中安裝了xauth
。
Step 2 sshd_config
然後在配置文件/etc/ssh/sshd_config
中進行如下配置
|
|
修改完成後重啓sshd服務。
如果X11UseLocalhost no
爲no
,則會出現如下報錯
|
|
其中Malachite:10.0
的含義,可閱讀如下說明:
A central concept of
X
is the display, an abstraction for the screen managed by an X server. When an X client is invoked, it needs to know which display to use. Displays are named by strings of the form HOST:n.v, where:
HOST
is the name of the machine running the X server controlling the display.n
is the display number, an integer, usually 0. X allows for multiple displays controlled by a single server; additional displays are numbered 1, 2, and so on.v
is the visual number, another integer. A visual is a virtual display. X supports multiple virtual displays on a single, physical display. If there’s only one virtual display (which is the most common scenario), you omit the “.v”, and the default is visual 0. – https://docstore.mik.ua/orelly/networking_2ndEd/ssh/ch09_03.htm
Step 3 DISPLAY
如果不使用X轉發,但想使使用遠程主機(通過SSH登錄)中的X,則必須手動設置變量DISPLAY
,說明如下
SSH sets the
DISPLAY
variable automatically only if X forwarding is in effect. If you don’t use X forwarding but want to use X on a remote machine you logged into via SSH, remember that you have to set the DISPLAY variable yourself. You should only do this when the both machines are on the same, trusted network, as the X protocol by itself is quite insecure. – https://docstore.mik.ua/orelly/networking_2ndEd/ssh/ch09_03.htm
在本機(X client)中進行如下設置
|
|
也可将其写入文件~/.bashrc
中。
Step 4 ssh -Y
操作完成後即可通過ssh -X
或ssh -Y
登錄遠程主機,啓用含有圖形化界面的程序,即可看到效果(在本地跳出新窗口,內容爲遠程主機中的圖形化程序)。
-X Enables X11 forwarding -Y Enables trusted X11 forwarding.
如果按照以上操作执行后仍报错,可退出当前用户登录,重新登入后尝试。
The Onion Router
The Onion Router簡稱Tor,是一個匿名網絡。可通過LocalForward
進行端口轉發,在本機連入Tor網絡,實現網路匿名訪問。
在VPS中安裝tor
,服務啓用後,監聽 9050 端口。
配置如下
|
|
執行
|
|
操作成功後,會在本機啓用 3333 端口,通過 127.0.0.1:3333 可連入Tor網絡。
如果需要獲取本機的真實外網IP,可通過如下命令查看
|
|
該命令參考於 Command for determining my public IP?。
~/.ssh/config
完整配置文件內容如下
|
|
References
- How To Configure Custom Connection Options for your SSH Client
- OpenSSH Config File Examples
- SSH ProxyCommand example: Going through one host to reach another server
- Simplify Your Life With an SSH Config File
- Limiting Exposure via SSH ProxyJump
- set-up X11 Forwarding over ssh
- SSH XForwarding fails - xauth bad display name
Bibliography
Change Logs
- 2017.03.08 23:59 Wed Asia/Shanghai
- 初稿完成
- 2018.07.27 14:48:32 Fri America/Boston
- 勘誤,更新,遷移到新Blog