Software engineering notes

SSH

產生 ssh key 並且設定免密碼直接連入遠端主機

本機:

cd ~
ssh-keygen -t rsa
(enter到底)
cd .ssh
scp id_rsa.pub 遠端主機網址:~/.ssh/qq  (把本機的公鑰複製到遠端主機)

登入遠端主機:

ssh 遠端主機網址
(會要你輸入密碼)
cd .ssh
cat qq >> authorized_keys   (將主機的公鑰加入到遠端的驗證key裡)
rm qq                       (加入到authorized_keys後就沒有用了, 刪除它)
exit                        (大功告成, 離開遠端主機)

回本機:

ssh 遠端主機網址
(不會要求你輸入密碼而直接進入)

安全性設定

Open /etc/ssh/sshd_config (Please note, it’s sshd_config as opposed to ssh_config) :

允許密碼驗證

PasswordAuthentication yes

改 SSH default port

Port 2222
Protocol 2
PermitRootLogin no

只允許某個 User

UseDNS no
AllowUsers test

Reload 新設定

別急著登出, 先測試修改是否成功

sudo service ssh restart
ssh -p 2222 test@localhost

如果有設定防火牆記得把 2222 port 打開

設定 push 或 pull github 不用再輸入帳密

將你的 repository 的 ssh URL 加到你的 origin, 不要用 https URL

然候將你的 ssh public key 加到 github 後台的 ssh 管理

設定 alias - 每一次連線不用輸入過多參數

~/.ssh/config 加上要 alias 對應的欄位

HOST test
    HostName 192.168.79.21
    User test
    Port 22
    IdentityFile aws.pem

之後輸入 ssh test 即可

.pem 的權限要調低, 否則會報錯, chmod 400 aws.pem

使用 .pem 連進主機,除了上述方法設定也可以用參數 -i

sudo ssh ubuntu@123.123.123.123 -i aws.pem

AWS Ubuntu 主機預設 User 叫 ubuntu

Generate public key from private key (.pem)

ssh-keygen -y -f your-app.pem > your-app.pub

在同一台主機避免跟同事用到同一個 session 方法

可在 .bashrc 設定 :

sssh (){ ssh -t "$1" 'tmux attach -t test || tmux new -s test || screen -DR'; }

之後執行 sssh 就好了!

Local Port Forwarding

While opening http://127.0.0.1:8080/ on local browser, it will be forward to remote server on port 8080

Mac

Command

ssh ubuntu@54.199.219.49 -L 8080:54.199.219.49:8080

Config

Host ec2
    HostName 54.199.219.49
    User ubuntu
    Port 22
    LocalForward 8080 54.199.219.49:8080

Remote Port Forwarding

When remote server on port 8080 receives request, it will be forward to localhost on port 8080

Command

ssh -R 8080:localhost:8080 ubuntu@54.199.219.49

Config

Host ec2
    HostName 54.199.219.49
    User ubuntu
    Port 22
    RemoteForward 8080 localhost:8080

sshd config (/etc/ssh/sshd_config)

GatewayPorts yes

it must be enabled, otherwise the port forwarding will fail

then restart sshd daemon

sudo systemctl restart sshd

forward agent - 本機登錄到 basion 後再登入到其他主機時不需要把 private key 放到 basion

  1. 將本機的私鑰加入到 keychain

    ssh-add -K

ssh-add -lssh-add -L 就可以看到剛剛加入的那把私鑰了

在本機及 basion 加入以下,當從 basion 登入到其他台時, basion 那台就不需要有本機 private key 就能連到其他台了

Host *
        ForwardAgent yes
        ServerAliveInterval 300
        ServerAliveCountMax 5

一行指令直接經由 basion 登入主機 (proxycommand)

大部份我們的主機架構會長這樣,每次要到 server 都要經由 basion,所以會輸入兩次 ssh 很麻煩

laptop -> basion -> server

我們可以用一行指令就能直接登入 server

ssh -tt basion ssh -tt server

但這樣雖然縮短時間,不需要等到進去 basion 再 ssh 到主機,但 command 還是有點太長,可以用 proxycommand 做到,它的原理是用 nc 指令建立 TCP 連線連接 basion 與 server

PuTTY (Windows)

免密碼以SSH登入主機

  1. 主機要先產生完 SSH Key (ssh請選擇rsa)
  2. 下載 WinSCP,並使用它登入主機把私鑰的 SSH Key 下載下來(id_rsa)
  3. 將私鑰名稱(id_rsa)加上副檔名(.ppk) => id_rsa.ppk
  4. PuTTY 官網頁下載 puttygen.exe
  5. 打開PuTTYgen
    1. 點選 “Load” 讀取 id_rsa.ppk
    2. 選擇 SSH-1 (RSA)
    3. 點選 Save private key
  6. 打開PuTTY
    • 左邊 Connection→Data 輸入 Auto-login username
    • 左邊 Connection→SSH→Auth
    1. 打勾 Attempt authentication using Pageant
    2. 打勾 Attempt TIS or CrptoCard auth(SSH-1) (其他不要打勾)
    3. 下面瀏覽框要將 PuTTYgen 儲存的私鑰讀進來
  7. 完成,記得回到 Session 點 save 儲存這些設定,這樣下次就不用又從頭設定一次喔!

將 AmazonEC2MicroInstance.pem 轉成 ppk

開啟 puttygen.ext -> Load (選擇 AmazonEC2MicroInstance.pem) -> Save private key (存成 amazon-ec2.ppk)

左邊 SSH -> Auth 的 Private key file for authentication 選擇剛剛生出來的 amazon-ec2.ppk

Windows

如果使用 putty 要多設定 Conection->SSH->Tunels

Source port : 35729
Destination : 54.199.219.49:35729

下面選項維持預設 Local 及 Auto

Troubleshooting

Someone could be eavesdropping on you right now (man-in-the-middle attack)!

device-f33d32:~ apple$ ssh test@test.com
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
e2:c8:73:6b:01:ad:29:cf:7c:1c:54:68:93:4c:a8:7e.
Please contact your system administrator.
Add correct host key in /Users/apple/.ssh/known_hosts to get rid of this message.
Offending RSA key in /Users/apple/.ssh/known_hosts:1
RSA host key for test.com has changed and you have requested strict checking.
Host key verification failed.

意思是 .ssh/known_hosts 原本記著這樣的紀錄

test.com,142.4.51.133 ssh-rsa AAAAB3NzaC1yc2EAAA(..略..)

但是重灌後的 rsa 不一樣了, 所以為了安全性的考量而無法登入, 解決方法很簡單

只要把 .ssh/known_hosts 裡主機那行紀錄刪除, 再 ssh 進去重新產生紀錄就行了