圖片來源 File:Man in the middle attack.svg. by Miraceti . Wikimedia Commons
- 圖中 Mallory 是密碼學中常用來代表「惡意的攻擊者」虛構角色的人名[3]
圖片來源 File:Client-server-model.svg. 由 Calimo 以 David Vignoni 的 Gnome-fs-client.svg, Gnome-fs-server.svg 為基礎來創作. Wikimedia Commons.
sudo apt update
sudo apt install openssh-server
sudo apt install ssh
apt-cache search [keyword]
找軟體指令openssh-client
openssh-server
ssh
(metapackage)openssh-server
for server ,必須等待來自使用者端的請求,所以於遠端的主機必須要有 ssh-serveropenssh-client
只負責發送資訊給 server 對應的 portopenssh-client
於 Ubuntu 中已內建,如果沒有要做連入的主機可以不安裝 openssh-server
P.S: Windows 10 1809 的 cmd 中也內建了 openssh-client
sudo systemctl status ssh
sudo ss -ltpn
-l
: 列出傾聽狀態(listening)的 sockets[6]-t
: 只列出 TCP 的 sockets-p
: 顯示使用 sockets 的程式資訊-n
: 不要嘗試把 port 號解析成服務名稱sudo systemctl start ssh
開始sudo systemctl stop ssh
停止sudo systemctl restart ssh
重新啟動;關掉再打開ssh localhost
ssh 127.0.0.1
ssh [username]@127.0.0.1
localhost
會被解析成 127.0.0.1
/etc/hosts
檔案127.0.0.1/8
被設定為保留地址,所有向這個 IP 發送的封包都會留在本機ssh [username]@[hostname]
ssh -p [port num] [username]@[hostname]
指定 server 的 port 號(預設 22)
ssh [username]@[hostname] '[cmd]'
執行單行指令ssh -f [username]@[hostname] '[cmd]'
背景執行指令
ssh -v [username]@[hostname]
使用偵錯模式連線
-vv
、-vvv
,v 越多,過程顯示的越多exit
/ logout
/ Ctrl+D
退出連線ssh -A
允許 Forward agentFIXME: 可能要加一下 -A 和 -X 成功的 demo
ssh -X
允許遠端打開圖形化介面視窗FIXME: -X 可能會無法運作,理解一下原因以及解法
Ref: xorg - X11 forwarding - Putty - Xming: Why app run on the server instead of the client side? - Super User
Ubuntu 22.04 預設是 Wayland,而不是 X server
ssh -o
根據 ssh_config 做單次的設定
ssh -o ConnectTimeout=3 [username]@[hostname]
等待時間如果超過三秒即斷線流程圖來源:1121 LSA-I Week 08 Linux 遠端連線 講義 by @wzray07
ssh-keygen -t ed25519
-t
: 指定用哪種加密方式,預設為 rsa-b
: 指定要生幾 bits 的金鑰 (不一定需要這參數)$ ssh-keygen -t ed25519
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/user/.ssh/id_ed25519): /home/user/.ssh/id_ed25519_lsademo
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_ed25519_lsademo
Your public key has been saved in /home/user/.ssh/id_ed25519_lsademo.pub
The key fingerprint is:
SHA256:icqDAThC3dD/aQItF7e5ZU4n0vXgn4ot3JvxicYtEjI user@host
The key's randomart image is:
+--[ED25519 256]--+
| ...+ |
|o . o . . o |
|= o o + o o |
|.o o = = * o . |
| . = S O o . .|
| + . . E o o |
| . + o + *.o |
| . = B=..|
| ++oo |
+----[SHA256]-----+
~/.ssh/id_ed25519
-> 自己留著,不能給別人的~/.ssh/id_ed25519.pub
-> 傳給別人的y
會覆蓋過去/home/user/id_ed25519 already exists.
Overwrite (y/n)? y
ssh -o VisualHostKey=yes [username]@[hostname]
rsa
預設
1,024
~ 16,384
bits ,現在預設為 3072
bits
2048
bits 以下都不建議用,如需提升安全性可選擇更長的 key ,如:3,072
或 4,096
bits (詳見文末延伸閱讀連結)3072
bits,公鑰會被 OpenSSH 編碼成 544
個 16 進位字元(chars)ed25519
較推薦使用
256
bits,被 OpenSSH 編碼成 68
個 16 進位字元(chars)ecdsa
256
, 384
, 521
bits 三種 key 長度可以選擇,無法選擇其他的使用ssh-copy-id -i ~/.ssh/id_ed25519.pub [username]@[hostname]
cd ~/.ssh
touch authorized_keys
id_ed25519.pub
公鑰內容放入 authorized_keys
中,以換行方式區分不同支公鑰vim authorized_keys
chmod -R go= ~/.ssh
把這個資料夾中所有檔案的 group 及 other 權限拿掉ssh [username]@[hostname]
ssh -i ~/.ssh/id_ed25519 [username]@[hostname]
$ ssh username@remote_host
The authenticity of host '203.0.113.1 (203.0.113.1)' can't be established.
ECDSA key fingerprint is fd:fd:d4:f9:77:fe:73:84:e1:55:00:ad:d6:6d:22:fe.
Are you sure you want to continue connecting (yes/no)? yes
yes
後按 Enter 即可繼續連線若遇到權限問題,用
chmod 600 ~/.ssh/authorized_keys
sudo rm /etc/ssh/ssh_host*
刪除sudo dpkg-reconfigure openssh-server
重新設定ssh-keygen -f "~/.ssh/known_hosts" -R "[hostname]"
手動刪除不會紀錄在
known_host.old
中
#
為註解,且註解後通常為藍字/etc/ssh/
/etc/ssh/ssh_config
Include /etc/ssh/ssh_config.d/*.conf
Host *
# ForwardAgent no
# ForwardX11 no
# ForwardX11Trusted yes
# PasswordAuthentication yes
# HostbasedAuthentication no
# GSSAPIAuthentication no
# GSSAPIDelegateCredentials no
# GSSAPIKeyExchange no
# GSSAPITrustDNS no
# BatchMode no
# CheckHostIP yes
# AddressFamily any
# ConnectTimeout 0
# StrictHostKeyChecking ask
# IdentityFile ~/.ssh/id_rsa
# IdentityFile ~/.ssh/id_dsa
# IdentityFile ~/.ssh/id_ecdsa
# IdentityFile ~/.ssh/id_ed25519
# Port 22
# Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,3des-cbc
# MACs hmac-md5,hmac-sha1,umac-64@openssh.com
# EscapeChar ~
# Tunnel no
# TunnelDevice any:any
# PermitLocalCommand no
# VisualHostKey no
# ProxyCommand ssh -q -W %h:%p gateway.example.com
# RekeyLimit 1G 1h
# UserKnownHostsFile ~/.ssh/known_hosts.d/%k
SendEnv LANG LC_*
HashKnownHosts yes
GSSAPIAuthentication yes
Host ec2
User lsa
Port 22
Hostname lsalab.moli.rocks
ssh ec2
指令時,會採用以上設定連線/etc/ssh_config
中,也可以建立一個檔案 ~/.ssh/config
/etc/ssh/sshd_config
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key
0.0.0.0
為 IPv4 中允許主機所擁有的任何 IP 連入
::
為 IPv6 中,允許主機所擁有的任何 IP 連入no
,剩下 public key 驗證模式,可大大降低猜密碼攻擊危險性
Match User ray
PasswordAuthentication no
ClientAliveInterval 30
ClientAliveCountMax 20
/etc/ssh/
中會發現有一個目錄 sshd_config.d
.conf
檔去做設定sudo apt install vsftpd
sudo service vsftpd status
/etc/vsftpd.conf
設定檔位置anonymous_enable=YES (NO)
anon_other_write_enable=YES (NO)
anon_mkdir_write_enable=YES (NO)
anon_upload_enable=YES (NO)
no_anon_password=YES (NO)
anon_root = /var/ftp/
guest_enable=YES (NO)
與 local_enable=YES (NO)
FIXME: 開設定好匿名上傳下載的 server 給大家玩?
anonymous
表示匿名登入/var/log/vsftpd.log
ftp [ip address]
登入anonymous
作為匿名,密碼可空白put [client_file] [server_pwd]
上傳檔案,如 server 上已有重複的檔案則覆蓋get [file] [client_pwd]
下載檔案,如 client 上已有重複的檔案則覆蓋ls
與 Linux 指令一樣, listcd
與 Linux 指令一樣, change directorylpwd
顯示本機端目前位置lcd
切換本機端位置help
查看更多指令bye
退出所以 server 端需要 SSH 才能使用
在 sshd_config 檔案中,有
# override default of no subsystems
Subsystem sftp /usr/lib/openssh/sftp-server
sftp [user]@[hostname]
cp
在 Linux 是複製scp
為遠端複製檔案scp [參數] [source] [destination]
scp [本機檔案路徑] [usr_name]@[hostname]:[遠端檔案路徑]
path
不輸入則為家目錄scp [usr_name]@[hostname]:[遠端檔案路徑] [本機檔案路徑]
-r
: recursive ,目錄中所有檔案
scp [folder]/** [destination]
cp
跟 scp
的事,但 Rsync 的效率比 cp
跟 scp
好sudo apt install rsync
rsync [參數] [來源檔案] [目的檔案]
參數 | 說明 |
---|---|
-v |
verbose 模式,輸出比較詳細的訊息 |
-a |
封裝備份模式,遞迴備份所有子目錄下的目錄與檔案,保留連結檔、檔案的擁有者、群組、權限以及時間戳記。 |
-z |
啟用壓縮。 |
-h |
將數字以比較容易閱讀的格式輸出。 |
-P / --progress |
顯示傳輸進度 |
--delete |
同步將不存在於來源端的檔案刪除 |
rsync -avh /home/wzray/a/ /home/wzray/b/
-a
這個參數包含 -r
,所以要複製整個目錄下的檔案也沒問題rsync -avh /home/wzray/a/ /home/wzray/b/
rsync -avzh ~/file wzray@192.168.1.12:/home/wzray/
rsync -avzh wzray@192.168.1.12:/home/wzray/ ~/file
scp
語法很像rsync -avh --delete /home/wzray/a/ /home/wzray/b/
語法
ssh -L [bind_address:][port]:[host]:[host_port] [SSH Server]
[bind address:]
不輸入預設為 localhost
情境一:
防火牆後有一台 Server ,但防火牆設定不接受 8080 port 連入,不過接受 22 port
使用 tunneling 將 client 中的某個 port (圖中範例 port 9090),對他發送的資料經 SSH 通道轉送至 8080 port,等於連上 client 9090 port 就可以連上 Server 8080 port ,完成繞過防火牆限制
ssh -L 9090:localhost:8080 [username]@[server]
Client
SSH Server
[username]@[server]
Target Server
bind address
沒輸入代表使用預設 localhost
0.0.0.0
host
的 localhost
是只針對 server
的 localhost
情境二:
前面情境為 Server 能連入的情況,那如果今天權限不足以連入要怎麼辦
如果防火牆後有一台你的機器且可用 ssh 連入即可完成
ssh -L 9090:192.168.2.10:8080 [username]@[server]
192.168.2.10:8080
[username]@[server]
ssh -R [bind_address:][port]:[host]:[host_port] [username]@[server]
ssh -R 0.0.0.0:9090:localhost:8080 [username]@[SSH Server]
0.0.0.0
localhost
是針對你的電腦[username]@[server]
GatewayPorts yes
ssh -R 0.0.0.0:9090:192.168.1.100:8080 [username]@[SSH Server]
192.168.1.100
為內部服務 ip(相對於你的電腦)[username]@[server]
192.168.1.100:8080
ssh -D [bind_address:][port] [username]@[SSH Server]
FIXME: 三種 tunnel 模式想個 demo
FIXME: 線要畫確實,包括從哪進、從哪出,下面幾張圖亦然
cryptography - SSH Key: Ed25519 vs RSA - Information Security Stack Exchange
EdDSA - 維基百科,自由的百科全書 當中的 Ed25519 章節
基礎密碼學(對稱式與非對稱式加密技術). 現代密碼學泛指透過數學演算法與電腦科學對資料(明文)進行加密和解密的科學。 | by Chan @RiverChan | Medium ↩︎
SSH的安全驗證 | 凌群電子報 by 鄭翔文 ↩︎
SSH加密過程 - HackMD by @yanren1996 ↩︎
Understanding the SSH Encryption and Connection Process | DigitalOcean by Justin Ellingwood ↩︎
選擇 SSH key 的加密演算法. 在 ssh-keygen 的 man page 說明中有一個… | by Chris Yuan, 袁昇禾 | Medium ↩︎
Day 24. 非對稱式加密演算法 - 橢圓曲線密碼學 Elliptic Curve Cryptography , ECC (觀念篇) - iT 邦幫忙::一起幫忙解決難題,拯救 IT 人的一天 by 羊小咩 (lamb_mei) ↩︎
Ubuntu / Debian Linux 重新產生 OpenSSH Host Keys 的方法 | The Will Will Web ↩︎
rsync & scp - HackMD by 葉佳臻 @celineyeh ↩︎
SSH Tunneling (Port Forwarding) 詳解 · John Engineering Stuff ↩︎