這裡記錄在 VPS 裝好 Ubuntu server 後,為加強保安而要做的一些後續措施。 也方便自己和別人在下次跟著操作便好。

環境:Ubuntu Server 20.04 (LTS)

若你見到 <橙色>,代表你需要轉換內容。


建立一個普通帳號

在絕大多數情況下,不需要用 root 帳號,普通帳號 + sudo 權限便足矣。

  1. 先登入 root 帳號

macOS 用家可以用內置的 Terminal,

Windows 10 用家可以用內置的 Command Prompt, Windows PowerShell, 或者 Windows Terminal

ssh root@<your_server_ip>

Windows 用家可能會遇上沒有 ssh 情況,可以參考 Microsoft 官網的安裝方法:

1. Open Settings, select Apps > Apps & Features, then select Optional Features.
2. Scan the list to see if the OpenSSH is already installed. If not, at the top of the page, select Add a feature, then:
    Find OpenSSH Client, then click Install

1. 開啟 設定,選取 [應用程式] > 應用程式 & 功能,然後選取 [選用功能]。
2. 掃描清單,查看是否已安裝 OpenSSH。 如果沒有,請在頁面頂端選取 [新增功能],然後:
    尋找 OpenSSH 用戶端,然後按一下 [安裝]。

Ref: Get started with OpenSSH

第一次連接,會出現類似以下的訊息,輸入 yes 即可。

The authenticity of host '<your_server_ip>' can't be established.
ED25519 key fingerprint is '<key fingerprint>'.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

之後再輸入 root 的密碼登入。

Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '<your_server_ip>' (ECDSA) to the list of known hosts.
root@<your_server_ip>'s password:
  1. 建立新帳號

輸入以下 command 以建立新帳號,隨後它要你為帳號起個密碼,及輸入其他資訊(可不填)。

adduser <username_of_new_acct>
# 例子:我用 oldestdream 作為新帳號的用戶名

root@blog:~# adduser oldestdream
Adding user oldestdream' ... Adding new group oldestdream' (1000) ...
Adding new user oldestdream' (1000) with group oldestdream' ...
Creating home directory /home/oldestdream' ... Copying files from /etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for oldestdream
Enter the new value, or press ENTER for the default
Full Name []: Oldest Dream
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] Y
root@blog:~#

可以輸入 tail -n 1 /etc/passwd 來驗證帳號是否建立成功,見到有自己輸入的用戶名即是成功。

root@blog:~# tail -n 1 /etc/passwd
oldestdream:x:1000:1000:Oldest Dream,,,:/home/oldestdream:/bin/bash
  1. 給予 sudo 權限 (i.e. 至高無上的管理員權限)
usermod -aG sudo <username_of_new_acct>

可以用以下 commands 驗證,看有沒有 sudo 這字眼,有便是成功了。

# 例子:我用 oldestdream 作為新帳號的用戶名

root@blog:~# id -nG oldestdream
oldestdream sudo

之後便可以用新帳號來登入系統了。

ssh <username_of_new_acct>@<your_server_ip>


禁用 root 帳號

當你成功用普通帳號登入後,便可以禁用 root 帳號了。

sudo passwd -l root

若有什麼特別原因,真的要用上 root 帳號的話,可以使用這句解鎖:

sudo passwd -u root


更新系統

系統難免有漏洞 / Bugs,及時更新系統修補漏洞,能減低系統被入侵的風險。

sudo apt-get update -y && sudo apt-get full-upgrade -y
sudo apt-get autoremove
sudo apt-get autoclean

若想自動安裝安全更新,可以輸入:

sudo apt-get install unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades

出現下一段字時選擇 Yes 便可。

Applying updates on a frequent basis is an important part of keeping systems secure. By default, updates need to be applied manually using package management tools. Alternatively, you can choose to have this system automatically download and install important updates.  

Automatically download and install stable updates?

修改 SSHD CONFIG

  • 禁止 SSH 使用密碼登入(慎選)
  • 修改 SSH 登入 Port

因篇幅略長,所以拆分成另一篇文章 《SSH 禁止密碼登入和改 Port 方法》


開啓防火場

  1. 輸入以下的 commands:
sudo ufw default deny incoming
sudo ufw default allow outgoing

# 如果你改過了 Port, 請把下面的 22 改為你修訂的 Port 數字
sudo ufw allow 22/tcp

# 如果你打算用伺服器架網站,可以增加 http (80) & https (443)
sudo ufw allow http
sudo ufw allow https

sudo ufw enable
  1. 之後你可以驗證剛才所允許的 Port 是否正確:
sudo ufw status numbered

如果設定有錯,可以刪掉,再重新輸入:

# 把在 sudo ufw status numbered 看到有問題的序號
sudo ufw delete <the_number_you_want_to_del>
# e.g. 要刪掉第3個便輸入 sudo ufw delete 3


防止暴力破解

我們可以安裝一套叫 Fail2ban 的軟件,防止 SSH 暴力登入。可見另一篇文章 《Fail2ban 基本設定》

到此,基本的安全設定就完成了。

Recommended Posts

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments