2019年11月23日 星期六

ssh client

ssh client 的系統設定在 /etc/ssh/ssh_config,如果要個人設定,是寫在 ~/.ssh/config。

有個方便的用處是,當 ssh 伺服器不是使用預設通訊埠時,可以寫在設定檔裡,這樣 ssh 連線時就不用再額外指定通訊埠。此外也可以設定別名。

連線到伺服器有些情況會有 locale 問題,可以關掉 SendEnv。但由於 SendEnv 是累加的,不是不設 SendEnv 或將 SendEnv 設為空白就可以,如果 /etc/ssh/ssh_config 有設 SendEnv,也需要移除。

問題:no matching key exchange method found. Their offer: diffie-hellman-group1-sha1
  • 加參數 -oKexAlgorithms=+diffie-hellman-group1-sha1
  • 設定加入連線加密方法
  • 參考
ssh 是一個透過 SSH 加密連線登入遠端機器執行指令的程式,同時這個連線也可以用來轉送 X11 連線、任意 TCP port、和 UNIX-domain socket。

指令參數

  • -N:不執行遠端指令
  • -nNT:不開 tty
  • -f:背景執行 
  • -L:local port forwarding
  • -D:dynamic port forwarding
  • -R:remote port forwarding
  • -X:X11 Forwarding

SSH tunnel

local port forwarding

ssh client 近端一個 port 透過 SSH 通到 ssh server,然後存取遠端 remotehost:remoteport:

ssh -L localport:remotehost:remoteport user@example.com

連 ssh client 的 localport,相當於連到遠端的 remotehost:remoteport,SSH server 會去解析 remotehost。遠端位置可能是 Web Server、RDP Server、VNC Server 等。

dynamic port forwording

同 local port forwarding,但最終位置不指定,改依 L5 SOCKS 協定的 handshake 得知。可用於任何 TCP/UDP socket 連線。provide a local SOCKS 4/4A/5 proxy on a local port

ssh -D localport user@example.com

所有連 ssh client 的 localport,都會連到 SSH server 再去進行連線到最終位置。例如 Firefox 等瀏覽器可設定 SOCKS proxy 為 SSH client 的 localport,網頁就會透過 SSH server 去連線。參考:https://linuxize.com/post/how-to-setup-ssh-socks-tunnel-for-private-browsing/

remote port forwarding

遠端 ssh server 的一個 port 透過 SSH 通到 ssh client,然後存取近端的 localhost:port:

ssh -R 9000:localhost:3000 user@example.com

SSH server 預設不允許 remote port forwarding,啟用需要在 /etc/ssh/sshd_config

GatewayPorts yes

然後重啟

sudo service ssh restart

連遠端 ssh server 的 port,相當於連到近端的 localhost:port。

ssh client


PuTTY pieTTY Xshell FireSSH zterm
安裝 可免安裝 可免安裝 需要安裝,個人用免費 JavaScript 版,給 Firefox 使用 Java 版,用於各式瀏覽器,需要 Java 執行環境
拖曳傳擋
有 (sftp,需要輸入密碼、選目錄) 有 (sftp)

Tab



中文



判斷字元自動輸入



其它說明
基於 PuTTY,但沒隨時跟上更新

選項設定

ssh 依照下列優先來源取得設定:

  1. command-line options
  2. 個人設定檔:~/.ssh/config
  3. 系統設定檔:/etc/ssh/ssh_config

設定檔包含 host 規範的 section,只要符合指令行的主機名稱就有效 (見 CanonicalizeHostname)。由於每個參數使用第一個取得的值,越 host 特殊的宣告應該要放在前面,越通用的放後面。

設定檔包含 keyword-argument 對,每對一行。# 開始的行和空白行是註解。argument 可用双引號包起來,為了包含空白。Configuration options may be separated by whitespace or optional whitespace and exactly one ‘=’; the latter format is useful to avoid the need to quote whitespace when specifying configuration options using the ssh, scp, and sftp -o option.

keyword argument

keyword=argument

keyword =arguemnt

keyword 不分大小寫,argument 分大小寫。

Host

接下來設定區塊 (到下個的 Host 或 Match 為止) 適用的 host,多個用 whitespace 分隔。「*」表示所有 host。The host is usually the hostname argument given on the command line (見 CanonicalizeHostname).

A pattern entry may be negated by prefixing it with an exclamation mark (‘!’).  If a negated entry is matched, then the Host entry is ignored, regardless of whether any other patterns on the line match.  Negated matches are therefore useful to provide exceptions for wildcard matches.

See PATTERNS for more information on patterns.

Match

接下來設定區塊 (到下個的 Host 或 Match 為止) 適用的條件,可多個,前置驚嘆號否定。

all: 都符合。必須單獨使用或緊接著 canonicalfinal 之後。

canonical:遠端 hostname 有 canonicalization 時額外套用。

final:最後額外套用。

exec 指令:在 shell 在執行指令回 0 時。指令有 whitespace characters 要quoted,接受 token %%、%h、%i、%L、%l、%n、%p、%r、和 %u。

The other keywords' criteria must be single entries or comma-separated lists and may use the wildcard and negation operators described in the PATTERNS section.

host hostname:比對 HostnameCanonicalizeHostname 轉換過後 hostname 符合。

originalhost hostname:比對指令行原始 hostname 符合。

user user:比對遠端帳號符合。

localuser user:比對本地帳號符合。在系統設定檔較有用。

BatchMode
如果設為 yes,user interaction such as password prompts and host key confirmation requests will be disabled. In addition, the ServerAliveInterval option will be set to 300 seconds by default (Debian-specific). This option is useful in scripts and other batch jobs where no user is present to interact with ssh(1), and where it is desirable to detect a broken network swiftly. The argument must be yes or no (the default).
CanonicalizeHostname
控制是否進行 hostname canonicalization。參考:https://sleeplessbeastie.eu/2020/08/24/how-to-perform-hostname-canonicalization/

預設 no:使用系統 resolver 處理 hostname lookups。

設為 yes:不用 ProxyCommand 或 ProxyJump 的連線,嘗試使用 CanonicalDomains 後綴和 CanonicalizePermittedCNAMEs 規則來canonicalize 指令行的 hostname。

設為 always:也用在 proxied 的連線。

設為 enabled:the configuration files are processed again using the new target name to pick up any new configuration in matching Host and Match stanzas.

CheckHostIP

預設是 yes,額外檢查 known_hosts 檔的 host IP 位址,無論 StrictHostKeyChecking 的設定都會加 host 位址到 ~/.ssh/known_hosts,用來偵測 host key 是否因為 DNS spoofing 而改變。

If the option is set to no, the check will not be executed.

Hostname
實際要登入的 host name,可以是 nicknames or abbreviations,接受 token %% and %h,可以是數字 IP (both on the command line and in Hostname specifications). The default is the name given on the command line.
KexAlgorithms
指定可用的 KEX (Key Exchange) 演算法. Multiple algorithms must be comma-separated. If the specified list begins with a ‘+’ character, then the specified methods will be appended to the default set instead of replacing them. If the specified list begins with a ‘-’ character, then the specified methods (in‐ cluding wildcards) will be removed from the default set instead of replacing them. If the specified list begins with a ‘^’ char‐ acter, then the specified methods will be placed at the head of the default set. The default is: curve25519-sha256,curve25519-sha256@libssh.org, ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521, diffie-hellman-group-exchange-sha256, diffie-hellman-group16-sha512, diffie-hellman-group18-sha512, diffie-hellman-group14-sha256 The list of available key exchange algorithms may also be ob‐ tained using "ssh -Q kex".
NumberOfPasswordPrompts
密碼嘗試次數,預設是 3。
PasswordAuthentication
是否使用密碼認證,yes (預設) 或 no。
Port
遠端 port,方便不是預設的 22。
PreferredAuthentications
嘗試認證方式的順序,預設是 gssapi-with-mic,hostbased,publickey,keyboard-interactive,password。
ServerAliveInterval
Sets a timeout interval 秒數 after which if no data has been received from the server, ssh 會送 message to request a response from the server. The default is 0, indicating that these messages will not be sent to the server, or 300 if the BatchMode option is set (Debian-spe‐ cific). ProtocolKeepAlives and SetupTimeOut are Debian-specific compatibility aliases for this option.
StrictHostKeyChecking

預設是 ask,新 host 的 key 在使用者同意後加入 ~/.ssh/known_hosts。host key 改變會拒絕連線。

設為 accept-new,自動加新 host 的 key。host key 改變會拒絕連線。

設為 yes,使用者必須手動加入新 host 的 key。host key 改變會拒絕連線。提供 man-in-the-middle (MITM) 攻擊最大的保護, though it can be annoying when the /etc/ssh/ssh_known_hosts file is poorly maintained or when connections to new hosts are frequently made.

設為 no 或 off,自動加新 host 的 key。host key 改變會檢查,除了一些限制外,仍允許連線。

User
指定登入帳號。
UserKnownHostsFile

Specifies one or more files to use for the user host key database, separated by whitespace. 預設是「~/.ssh/known_hosts ~/.ssh/known_hosts2」。

設為 UserKnownHostsFile=/dev/null

VerifyHostKeyDNS

Specifies whether to verify the remote key using DNS and SSHFP resource records.

設為 yes, the client will implicitly trust keys that match a secure fingerprint from DNS. Insecure fingerprints will be handled as if this option was set to ask.

設為 ask, information on fingerprint match will be displayed, but the user will still need to confirm new host keys according to the StrictHostKeyChecking option.

預設是 no。

See also VERIFYING HOST KEYS in ssh(1).

PATTERNS

pattern 是 0 或更多 non-whitespace characters

*:代表 0 或更多 characters

?:代表 1 個 character

pattern-list 是逗號分隔的 pattern 列表。裡面的 pattern 可以前置驚嘆號來否定。要注意的是否定符合本身不會變成正面的符合結果,例如下列結果 host3 並不符合:

from="!host1,!host2"

一種解決方式是包含一項達到正面符合,如 wildcard:

from="!host1,!host2,*"

TOKENS

有些 keyword 的 argument 可用 token,意思如下:

%%%
%CHash of %l%h%p%r。
%d自己的家目錄。
%h遠端 hostname。
%i自己 user ID。
%L自己 hostname。
%l自己 hostname, including the domain name。
%n原始指令行的遠端 hostname。
%p遠端 port。
%r遠端 username。
%T請求 tunnel forwarding 指定的 tun 或 tap 網路界面,無則 "NONE"。
%u自己 username。

      CertificateFile accepts the tokens %%, %d, %h, %i, %l, %r, and %u.

      ControlPath accepts the tokens %%, %C, %h, %i, %L, %l, %n, %p, %r, and
     %u.

    

     IdentityAgent and IdentityFile accept the tokens %%, %d, %h, %i, %l, %r,
     and %u.

     LocalCommand accepts the tokens %%, %C, %d, %h, %i, %l, %n, %p, %r, %T,
     and %u.

     ProxyCommand accepts the tokens %%, %h, %n, %p, and %r.

     RemoteCommand accepts the tokens %%, %C, %d, %h, %i, %l, %n, %p, %r, and
     %u.

問題

參考

  1. http://stackoverflow.com/questions/7332124/gitolite-with-non-default-port
  2. man ssh
  3. 基於 sftp 的 sshfs
  4. http://blog.trackets.com/2014/05/17/ssh-tunnel-local-and-remote-port-forwarding-explained-with-examples.html
  5. Bash ssh 由歷史資料來自動補齊主機名稱
  6. man ssh_config

SEE ALSO

     scp(1), ssh-add(1), ssh-agent(1), ssh-argv0(1), ssh-keygen(1),
     ssh-keyscan(1), tun(4), ssh-keysign(8), sshd(8)
待讀
https://www.bitvise.com/remote-desktop
http://www.howtogeek.com/114812/5-cool-things-you-can-do-with-an-ssh-server/
http://www.linuxjournal.com/content/more-secure-ssh-connections?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+linuxjournalcom+%28Linux+Journal+-+The+Original+Magazine+of+the+Linux+Community%29
http://www.cyberciti.biz/faq/install-ssh-identity-key-remote-host/
[Linux 透過 SSH SOCKS 連線 來 使用 Firefox / Pidgin(MSN, GTalk..)](https://blog.longwin.com.tw/2010/01/linux-ssh-socks-firefox-pidgin-2010/)

沒有留言:

張貼留言

SIP header Via

所有 SIP 訊息 都要有 Via,縮寫 v。一開始的 UAC 和後續途經的每個 proxy 都會疊加一個 Via 放傳送的位址,依序作為回應的路徑。 格式 sent-protocol sent-by [ ;branch= branch ][ ; 參數 ...] s...