SSH 配置詳解
阿新 • • 發佈:2022-04-29
本文詳細介紹了 SSH 的配置。
無密碼登入
# 產生公鑰與私鑰對
$ ssh-keygen
# 按三次回車鍵
# 將本機的公鑰 id_rsa.pub 複製到遠端機器的 ~/.ssh/authorized_keys 檔案中
$ ssh-copy-id user@ip
Ubuntu
SSH 分客戶端 openssh-client
和服務端 openssh-server
如果你只是想登陸別的機器只需要安裝客戶端
$ sudo apt install openssh-client
如果要使本機開放 SSH 服務就需要安裝服務端
$ sudo apt install openssh-server
然後確認 SSH 服務端是否啟動了:
$ ps -e |grep ssh
如果看到 sshd 那說明 SSH 服務端已經啟動了,如果沒有則可以這樣啟動:
$ sudo /usr/sbin/sshd
配置
ssh-server
配置檔案位於 /etc/ssh/sshd_config
在這裡可以定義 SSH 的服務埠,預設埠是 22,你可以自己定義成其他埠號。
不允許密碼登入,只允許公鑰登入
# To disable tunneled clear text passwords, change to no here! PasswordAuthentication no #PermitEmptyPasswords no # Change to yes to enable challenge-response passwords (beware issues with # some PAM modules and threads) ChallengeResponseAuthentication no
配置檔案中上訴兩項改為 no
,之後重啟 sshd
服務。現在我們在一臺不帶信任 key 的機器嘗試登入,那麼會提示如下資訊:
⋊> ~ ssh [email protected]
Permission denied (publickey).
解決自動斷開
服務端設定環境變數 TMOUT=0
,在客戶端 ~/.ssh/config
檔案中進行如下配置:
Host *
ServerAliveInterval 60