使用安全外殼 (ssh) 的 Linux 命令


簡介

我們有很多方法可以透過 CLI 在兩個系統之間進行訪問或通訊 [Linux 到 Linux,Windows 到 Linux]。安全外殼 (ssh) 是訪問任何其他 Linux 系統的非常有用的工具。如果兩個 Linux 系統透過任何網路連線,那麼我們可以使用 ssh 訪問其他系統。要使用此功能,需要滿足一些基本先決條件。

  • ssh 客戶端軟體 示例:Linux 中的預設 ssh 二進位制檔案、putty、mobaxterm 等。

  • 遠端 Linux 系統中執行的 ssh 伺服器。

  • 遠端系統的 IP 或主機名。

讓我們討論一下使用 ssh 進行 Linux 系統到 Linux 系統的通訊。

配置 ssh 設定

根據先決條件,

如果客戶端系統是 Windows,我們需要安裝 putty 或 mobaxterm。對於 Linux 客戶端系統,無需安裝任何內容,因為預設情況下可以使用 ssh 命令。

我們可以執行以下命令來檢查 ssh 是否已安裝。

$ ssh
usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
           [-D [bind_address:]port] [-E log_file] [-e escape_char]
           [-F configfile] [-I pkcs11] [-i identity_file] [-L address]
           [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
           [-Q query_option] [-R address] [-S ctl_path] [-W host:port]
           [-w local_tun[:remote_tun]] [user@]hostname [command]

然後在 Linux 伺服器端,我們需要安裝“openssh-server”軟體包。我們可以使用以下命令進行安裝。

$ sudo apt-get install openssh-server

讓我們假設,

系統 A 是客戶端,系統 B 是伺服器。

系統 B 的 ip 為“192.168.1.102”,使用者名稱為“rian”。我們將從系統 A 訪問系統 B。

登入到遠端 Linux 系統的命令

這是訪問系統 B 的最小命令。

$ ssh <user name of system B>@<IP address or hostname of system B: 192.168.1.102>

實際命令是

$ ssh rian@192.168.1.102

將提示使用者“rian”輸入密碼。在輸入正確的密碼後,ssh 將成功連線到 ip 192.168.1.102 或系統 B。

輸出

The authenticity of host '192.168.1.102 (192.168.1.102)' can't be established.
ECDSA key fingerprint is SHA256:YdQpl9u/N2+Dc6hPeDr8e5IdDXUJ3POj30prm1XJA2s.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.102' (ECDSA) to the list of known hosts.
rian@192.168.1.102's password:
Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.15.0-45-generic x86_64)

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

447 packages can be updated.
385 updates are security updates.

New release '18.04.6 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Last login: Thu Feb 11 22:20:03 2016 from 192.168.1.4

使用 ssh 在遠端系統中執行命令

如果我們想在不登入遠端系統的情況下執行任何遠端命令,那麼我們可以使用以下語法。

$ ssh <user name of system B>@<IP address of system B: 192.168.1.102> <command full path>

這是實際命令 [示例:ls]

$ ssh rian@192.168.1.102 /bin/ls

輸出

rian@192.168.1.102's password:
Desktop
Documents
Downloads
examples.desktop
Music
Pictures
Public
Templates
tutorialpoint
Videos

在遠端系統中訪問基於 GUI 的應用程式

如果我們需要開啟任何基於 GUI 的應用程式(例如遠端系統的 firefox),則必須使用 –X 選項進行 ssh。

$ ssh –X rian@192.168.1.102

現在,我們可以在 ssh 終端中從遠端系統開啟“firefox”瀏覽器。如果我們在沒有 –X 的情況下使用 ssh,則無法開啟基於 gui 的應用程式。

$ firefox

透過 ssh 連線複製

如果我們想使用“scp”命令透過 ssh 將任何檔案從系統 A 複製到系統 B,我們可以使用以下語法。

$ scp system_a_file <user name of system B>@<system B ip address:system b path>

這是實際命令。

$ scp examples.desktop rian@192.168.1.102:/tmp/

輸出

rian@192.168.1.102's password:
examples.desktop                                                                                                                100% 8980     4.4KB/s   00:02

使用 ssh 進行埠轉發

要訪問埠,可以使用以下命令。這是一個非常有用的功能。

$ ssh -L 8888:localhost:8080 <user name of system B>@<ip or hostname of user B>

帶有自定義埠的 ssh

預設情況下,ssh 監聽埠號 22。但是,我們可以使用以下命令使用自定義 ssh 埠。

$ ssh <user@remote ip> -p 3423

重新啟動 ssh 服務

如果 ssh 配置檔案或其他原因發生更改,這是重新啟動 sshd 服務的命令。

$ sudo sshd service restart

ssh 常用錯誤

有時在對遠端系統執行 ssh 時,我們會遇到以下錯誤。

ssh: connect to host 192.168.1.107 port 22: No route to host

可能的原因是遠端 ip 不可達。我們應該對遠端 ip 進行 ping 測試,然後嘗試 ssh。

結論

透過本文,我們瞭解了 ssh 命令的許多用法。使用 ssh,我們可以更快地訪問另一個 Linux 系統,並執行一些操作,例如複製、執行命令等。

更新於: 2023-07-14

780 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.