Mac 上玩 Linux(三)使用 SSH 連接 Ubuntu

Mac 上玩 Linux(一)安裝Ubuntu
Mac 上玩 Linux(二)Ubuntu 支持 root 登錄
Mac 上玩 Linux(三)使用 SSH 連接 Ubuntu
Mac 上玩 Linux(四)安裝 VMware Tools

首先 Mac 不像 Windows 需要類似 XShell 這樣的客戶端進(jìn)行 SSH 連接,直接使用終端即可岔激。

一齿尽、首先查看 IP 地址

我這里是使用的 Ubuntu 18.04 的模擬器血崭。

  1. 輸入命令:ifconfig

    未安裝提示

  2. 需要安裝 net-tools:apt install net-tools

    安裝 net-tools

  3. 安裝成功后仍源,再次 ifconfig

    IP 地址

二钧大、ssh 登錄

  1. ssh 登錄 root 用戶
    使用 ssh root@IP地址 或者 ssh -p 22 root@IP地址 命令
    出現(xiàn)錯(cuò)誤:
    ?  ~ ssh root@192.168.139.129
    ssh: connect to host 192.168.139.129 port 22: Connection refused
    ?  ~ ssh -p 22 root@192.168.139.129
    ssh: connect to host 192.168.139.129 port 22: Connection refused
    
  2. 檢查服務(wù)端 ssh 服務(wù)是否開啟
    ps -e | grep sshd
    
    沒有找到 sshd 蛤迎,說明 ssh 服務(wù)沒有啟動(dòng),需要開啟 ssh 服務(wù)刊懈。

三逗爹、啟動(dòng)服務(wù)器的 ssh

  1. 開啟 ssh 服務(wù)
service ssh start
沒有安裝 ssh
  1. 更新源列表
apt-get update
  1. 安裝 openssh-server
apt-get install openssh-server
  1. 再次查看 ssh 服務(wù)是否啟動(dòng)
ssh 服務(wù)已啟動(dòng)

四擎宝、ssh 登錄

  1. 連接 root 用戶
?  ~ ssh root@192.168.139.129      
The authenticity of host '192.168.139.129 (192.168.139.129)' can't be established.
ECDSA key fingerprint is SHA256:rVB8kHFOI0hCUyibvY6w5TfDF6I+5z0qG2jcVIxi94c.
Are you sure you want to continue connecting (yes/no)? yes
  1. 輸入 root 用戶密碼
Warning: Permanently added '192.168.139.129' (ECDSA) to the list of known hosts.
root@192.168.139.129's password: 
  1. 權(quán)限被拒絕
Permission denied, please try again.

這種情況束析,很大的可能是由于服務(wù)器默認(rèn)禁止 root 用戶進(jìn)行 ssh 遠(yuǎn)程登錄申尤。

  1. 嘗試普通用戶登錄滚粟,排除配置問題
?  ~ ssh ff@192.168.139.129
ff@192.168.139.129's password: 
  1. 輸入用戶密碼寻仗,普通用戶登錄成功。
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.18.0-17-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage


 * Canonical Livepatch is available for installation.
   - Reduce system reboots and improve kernel security. Activate at:
     https://ubuntu.com/livepatch

167 個(gè)可升級軟件包凡壤。
75 個(gè)安全更新署尤。

Your Hardware Enablement Stack (HWE) is supported until April 2023.

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

ff@FF-VM:~$ 

到這里就基本確定了就是,服務(wù)器禁止 root 用戶進(jìn)行 ssh 遠(yuǎn)程登錄了亚侠。

五曹体、允許 root 用戶 ssh 登錄

  1. 修改 sshd_config
    注意,安裝了 openssh 才會有這個(gè)文件硝烂,如果文件不存在請檢查是否安裝了 openssh箕别。
vim /etc/ssh/sshd_config
  1. 配置 PermitRootLogin
  • 檢查是否有 PermitRootLogin
  • 存在 PermitRootLogin ... 的話(...代表任何語句),全部修改為 PermitRootLogin yes
  • 不存在 PermitRootLogin 的話滞谢,增加 PermitRootLogin yes
配置 PermitRootLogin yes

不過這里有問題串稀,正常情況下 PermitRootLogin 應(yīng)該也是黃色高亮的。
我嘗試了多次狮杨,還是不能高亮母截,不管了先保存。

  1. 重啟 ssh 服務(wù)
/etc/init.d/ssh restart
  1. 連接 root 用戶
?  ~ ssh root@192.168.139.129      
root@192.168.139.129's password: 
Permission denied, please try again.

依然權(quán)限被拒絕禾酱,說明配置 PermitRootLogin yes 沒有生效微酬。

六绘趋、重新安裝 openssh-server

大多數(shù)情況下,完成上一步就可以完成登錄了颗管,是不需要該步驟的陷遮。

  1. 卸載 openssh-server
apt-get purge openssh-server
  1. 刪除 sshd_config
rm /etc/ssh/sshd_config
  1. 更新源列表
apt-get update
  1. 安裝 openssh-server
apt-get install openssh-server
  1. 打開 sshd_config
vim /etc/ssh/sshd_config
  1. 配置 PermitRootLogin
配置 PermitRootLogin yes
  1. 啟動(dòng) ssh 服務(wù)
/etc/init.d/ssh start

七、 ssh 登錄

連接 root 用戶

?  ~ ssh root@192.168.139.129
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    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 ECDSA key sent by the remote host is
SHA256:5devwrE/cIsO8P0l9nc7zQ+xxKRO5BIUDZ1tjd8BB+0.
Please contact your system administrator.
Add correct host key in /Users/ff/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /Users/ff/.ssh/known_hosts:5
ECDSA host key for 192.168.139.129 has changed and you have requested strict checking.
Host key verification failed.

出現(xiàn)錯(cuò)誤的原因是垦江,重裝了 openssh-server 帽馋,服務(wù)器的公鑰發(fā)生了變化導(dǎo)致的錯(cuò)誤。

SSH 服務(wù)是通過公鑰和私鑰來進(jìn)行連接的比吭,它會把每個(gè)曾經(jīng)訪問過計(jì)算機(jī)或服務(wù)器的公鑰绽族,記錄在 known_hosts 中,當(dāng)下次訪問曾經(jīng)訪問過的計(jì)算機(jī)或服務(wù)器時(shí)衩藤,SSH 就會核對公鑰吧慢,如果和上次記錄的不同,OpenSSH 會發(fā)出警告赏表。

八检诗、修改客戶端 known_hosts 文件

根據(jù)提示進(jìn)行修改

Add correct host key in /Users/ff/.ssh/known_hosts to get rid of this message.
  1. 進(jìn)入 known_hosts
?  ~ vim /Users/ff/.ssh/known_hosts
  1. 找到以 192.168.139.129 起始的行,將正行刪除
192.168.139.129 ecdsa-sha2-nistp256 ...省略

九瓢剿、ssh 登錄

  1. 連接 root 用戶
?  ~ ssh root@192.168.139.129      
The authenticity of host '192.168.139.129 (192.168.139.129)' can't be established.
ECDSA key fingerprint is SHA256:5devwrE/cIsO8P0l9nc7zQ+xxKRO5BIUDZ1tjd8BB+0.
Are you sure you want to continue connecting (yes/no)? yes
  1. 輸入 root 用戶密碼
Warning: Permanently added '192.168.139.129' (ECDSA) to the list of known hosts.
root@192.168.139.129's password: 
  1. root 用戶登錄成功
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.18.0-20-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage


 * Canonical Livepatch is available for installation.
   - Reduce system reboots and improve kernel security. Activate at:
     https://ubuntu.com/livepatch

0 個(gè)可升級軟件包逢慌。
0 個(gè)安全更新。

Your Hardware Enablement Stack (HWE) is supported until April 2023.

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

root@FF-VM:~# 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末间狂,一起剝皮案震驚了整個(gè)濱河市攻泼,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌鉴象,老刑警劉巖忙菠,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異炼列,居然都是意外死亡只搁,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進(jìn)店門俭尖,熙熙樓的掌柜王于貴愁眉苦臉地迎上來氢惋,“玉大人,你說我怎么就攤上這事稽犁⊙嫱” “怎么了?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵已亥,是天一觀的道長熊赖。 經(jīng)常有香客問我,道長虑椎,這世上最難降的妖魔是什么震鹉? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任俱笛,我火速辦了婚禮,結(jié)果婚禮上传趾,老公的妹妹穿的比我還像新娘迎膜。我一直安慰自己,他們只是感情好浆兰,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布磕仅。 她就那樣靜靜地躺著,像睡著了一般簸呈。 火紅的嫁衣襯著肌膚如雪榕订。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天蜕便,我揣著相機(jī)與錄音劫恒,去河邊找鬼。 笑死轿腺,一個(gè)胖子當(dāng)著我的面吹牛兼贸,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播吃溅,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼鸯檬!你這毒婦竟也來了决侈?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤喧务,失蹤者是張志新(化名)和其女友劉穎赖歌,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體功茴,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡庐冯,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了坎穿。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片展父。...
    茶點(diǎn)故事閱讀 39,834評論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖玲昧,靈堂內(nèi)的尸體忽然破棺而出栖茉,到底是詐尸還是另有隱情,我是刑警寧澤孵延,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布吕漂,位于F島的核電站,受9級特大地震影響尘应,放射性物質(zhì)發(fā)生泄漏惶凝。R本人自食惡果不足惜吼虎,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望苍鲜。 院中可真熱鬧思灰,春花似錦、人聲如沸坡贺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽遍坟。三九已至拳亿,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間愿伴,已是汗流浹背肺魁。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留隔节,地道東北人鹅经。 一個(gè)月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像怎诫,于是被迫代替她去往敵國和親瘾晃。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內(nèi)容