如何在 Ubuntu 上安裝安全的 OpenSSH 伺服器?

如何在 Ubuntu 上安裝安全的 OpenSSH 伺服器?

我想在 Ubuntu 上安裝 OpenSSH 伺服器。怎麼做?

我需要執行以下操作:

  1. 設定 OpenSSH 伺服器

  2. 為用戶設定ssh公私鑰對

  3. 停用密碼登入

  4. 啟用根用戶

  5. 為root用戶設定ssh公私鑰對

  6. 設定密碼

答案1

轉到終端機並輸入:

sudo su
aptitude install openssh-server openssh-client

測試安裝

ps -A | grep sshd

如果輸出是這樣的:

<some number> ?        00:00:00 sshd

然後 ssh 守護程式正在運行。

再次輸入終端機;

ss -lnp | grep sshd

如果輸出是這樣的:

0  128  :::22  :::*  users:(("sshd",16893,4))
0  128   *:22   *:*  users:(("sshd",16893,3))

那麼這意味著 ssh 守護程式正在監聽傳入的連接

現在我們編輯設定檔。首先我們對原始文件進行備份。

sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.factory-defaults

現在我們打開設定檔進行編輯

sudo gedit /etc/ssh/sshd_config

弱密碼很容易被猜到。最佳實踐是使用 SSH 金鑰而不是密碼。

所以我們完全禁用密碼驗證。

去排隊

#PasswordAuthentication yes

並將其替換為

PasswordAuthentication no

啟用轉送為已經猜出密碼的攻擊者提供了更多選擇。

所以我們禁用它。它給了我們一點安全感

轉到線路

AllowTcpForwarding yes
X11Forwarding yes

並將它們替換為

AllowTcpForwarding no
X11Forwarding no

我們可以明確允許某些使用者並拒絕某些使用者登入。

為此,我們必須將以下行放在設定檔的底部。

AllowUsers Fred Wilma
DenyUsers Dino Pebbles

為了筆記型電腦的最佳性能,我們允許兩個掛起的連接。在第三個和第十個連接之間,系統將在第十個同時連接時開始隨機丟棄連接,從 30% 到 100%。這可以透過以下行完成

MaxStartups 2:30:10

為了記錄更多錯誤和其他有用信息,我們更改了該行

LogLevel INFO

進入 LogLevel VERBOSE

為了嚇跑新手攻擊者,我們可以顯示一個橫幅我們從行的前面刪除了哈希標籤

#Banner /etc/issue.net

做到這一點

Banner /etc/issue.net

然後我們進入終端機並輸入:

sudo gedit /etc/issue.net

***************************************************************************
                        NOTICE TO USERS
This computer system is the private property of its owner, whether
individual, corporate or government.  It is for authorized use only.
Users (authorized or unauthorized) have no explicit or implicit
expectation of privacy.
Any or all uses of this system and all files on this system may be
intercepted, monitored, recorded, copied, audited, inspected, and
disclosed to your employer, to authorized site, government, and law
enforcement personnel, as well as authorized officials of government
agencies, both domestic and foreign.
By using this system, the user consents to such interception, monitoring,
recording, copying, auditing, inspection, and disclosure at the
discretion of such personnel or officials.  Unauthorized or improper use
of this system may result in civil and criminal penalties and
administrative or disciplinary action, as appropriate. By continuing to
use this system you indicate your awareness of and consent to these terms
and conditions of use. LOG OFF IMMEDIATELY if you do not agree to the
conditions stated in this warning.
****************************************************************************

現在我們儲存並關閉設定檔並透過在終端機中輸入以下命令重新啟動 ssh:

systemctl restart ssh

接下來我們設定 SSH 金鑰 有兩對 SSH 金鑰公用和私有。公鑰存在於伺服器中,私鑰存在於個人中如果有人可以將他的私鑰與公鑰匹配,則只有他/她可以登入。此外,可選地,私鑰可以透過密碼保護。

第一步 - 建立 RSA 金鑰對:

輸入終端

ssh-keygen -t rsa -b 4096

這裡我們使用64位元加密以提高安全性

第二步 - 儲存密鑰和密碼:

按照螢幕上的指示操作,提供所需的密鑰儲存位置,建議接受預設值,選擇密碼,提供強密碼,記住它。

螢幕是這樣的:

ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/demo/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/demo/.ssh/id_rsa.
Your public key has been saved in /home/demo/.ssh/id_rsa.pub.
The key fingerprint is:
4a:dd:0a:c6:35:4e:3f:ed:27:38:8c:74:44:4d:93:67 demo@a
The key's randomart image is:
+--[ RSA 2048]----+
|          .oo.   |
|         .  o.E  |
|        + .  o   |
|     . = = .     |
|      = S = .    |
|     o + = +     |
|      . o + o .  |
|           . o   |
|                 |
+-----------------+

第三步 - 複合公鑰:

輸入終端

ssh-copy-id [email protected]

這裡123.45.56.78是伺服器IP位址

如果是本機主機,則為

ssh-copy-id user@localmachinename

螢幕是這樣的

The authenticity of host '12.34.56.78 (12.34.56.78)' can't be established.
RSA key fingerprint is b1:2d:33:67:ce:35:4d:5f:f3:a8:cd:c0:c4:48:86:12.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '12.34.56.78' (RSA) to the list of known hosts.
[email protected]'s password: 
Now try logging into the machine, with "ssh '[email protected]'", and check in:

~/.ssh/authorized_keys

確保我們沒有添加您不期望的額外密鑰。

現在我們的安裝已經完成了。要登入,我們需要在終端機中輸入:

ssh username@servername

然後,當提示輸入密碼時,我們需要提供它。

現在我們啟用 opessh 伺服器的 root 登入 首先必須啟用 sudo 密碼,因為預設情況下它在 Ubuntu 中是停用的。

為此,我們在終端機中輸入以下內容,螢幕將如下所示:

sudo passwd
[sudo] password for [username]: [Type your user password and press return]
Type new UNIX password: [Type the root password you want]
Retype new UNIX password: [Retype the root password you chosen before]
passwd: password updated successfully

現在我們必須編輯 /etc/sudoers 檔案。

這裡我們使用名為visudo的編輯器

這是因為 visudo 的唯一目的是編輯 sudoes 文件

在 ubuntu 中,預設設定檔是由 nano 編輯器開啟的

若要變更它,請在終端機中鍵入:

sudo update-alternatives --config editor

將出現以下畫面:

There are 4 choices for the alternative editor (providing /usr/bin/editor).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /bin/nano            40        auto mode
  1            /bin/ed             -100       manual mode
  2            /bin/nano            40        manual mode
  3            /usr/bin/vim.basic   30        manual mode
  4            /usr/bin/vim.tiny    10        manual mode

Press <enter> to keep the current choice[*], or type selection number:

輸入 3 並按 Enter 鍵

然後輸入:

sudo visudo

移至顯示以下內容的行

Defaults    env_reset

按回車鍵

在上面建立一個新行

類型:

預設 rootpw

使用空白鍵,而不是 TAB

按 Esc --> :x --> Enter

在終端類型中:

gedit /etc/ssh/sshd_config

移至行:

PermitRootLogin password-prohibited

並將其更改為

PermitRootLogin yes

接下來移動到該行:

PasswordAuthentication no

並將其更改為

PasswordAuthentication yes

儲存並關閉

然後重新啟動SSH

service ssh restart

現在是時候再次為使用者 root 產生 ssh 公私金鑰對了。

輸入終端

ssh-keygen -t rsa -b 4096

第二步 - 儲存密鑰和密碼:

按照螢幕上的說明進行操作,提供所需的密鑰儲存位置,不要接受預設值,因為這次您必須創建新的對,預設值已經創建,選擇密碼,提供強密碼,記住它。

螢幕是這樣的:

ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/demo/.ssh/id_rsa): u-root-id_rsa 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/demo/.ssh/u-root-id_rsa.
Your public key has been saved in /home/demo/.ssh/u-root-id_rsa.pub.
The key fingerprint is:
4a:dd:0a:c6:35:4e:3f:ed:27:38:8c:74:44:4d:93:67 demo@a
The key's randomart image is:

+--[ RSA 2048]----+
|          .oo.   |
|         .  o.E  |
|        + .  o   |
|     . = = .     |
|      = S = .    |
|     o + = +     |
|      . o + o .  |
|           . o   |
|                 |
+-----------------+

然後在終端機輸入:

ssh-copy-id -i u-root-id_rsa.pub root@localmachinename

輸出螢幕可能會顯示:

The authenticity of host '12.34.56.78 (12.34.56.78)' can't be established.
RSA key fingerprint is b1:2d:33:67:ce:35:4d:5f:f3:a8:cd:c0:c4:48:86:12.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '12.34.56.78' (RSA) to the list of known hosts.
[email protected]'s password: 

現在嘗試使用“ssh '登入機器[電子郵件受保護]'”,然後簽入:

~/.ssh/authorized_keys

確保我們沒有添加您不期望的額外密鑰。

現在我們已經授予 root 私鑰存取權限來登入

要測試類型:

ssh root@localmachine

再次在終端機類型中:

gedit /etc/ssh/sshd_config

移動到該行:

PasswordAuthentication yes

並將其更改為

PasswordAuthentication no

儲存並關閉

然後重新啟動SSH

service ssh restart

它會要求輸入密碼。密碼不變。給吧。

現在root就可以成功登入了

現在為了提高安全性,我們必須添加防火牆

類型:

apt install ufw

現在開始吧

enable ufw

取得目前正在運行的進程列表

ufw app list

OpenSSH 將會列在那裡。

ALow it through firewall
ufw allow OpenSSH

重新啟動防火牆

systemctl restart ufw

我們的安裝完成了

相關內容