這邊整理了一些手動 Linux server 伺服器安裝,需注意的一些事情與指令。
備忘一下以備不時之需。
(如果是設定雲端主機的話,部分步驟可以跳過,它預設都幫你建好了。)
製作可開機 USB (Bootable USB)
使用 UNetbootin 軟體
軟體下載:https://unetbootin.github.io/
選擇 USB drive,選擇 ISO 就可以了
針對目標機器做開機。
Mac 系統的話,可以使用 Etcher
軟體下載:https://www.balena.io/etcher/
做法差不多
選擇作業系統:
- Debian 系列:可選擇 Ubuntu, Debian
- RedHat 系列:可選擇 RHEL, CentOS, Fedora
- BSD 系列:可選擇 FreeBSD
- SUSE 系列:可選擇 OpenSUSE
前二項是筆者較為熟悉的,推薦 Ubuntu, Debian, CentOS 做為選項。
ISO 的版本很多:
- Desktop ISO:有一個完整的 Live CD 可供試用
- Server ISO:有預載一些伺服器使用的套件
- Minimal ISO:只是檔案小,預設網路驅動了之後,大多都從網路上抓
不知怎麼選擇的話,預設就選 Desktop ISO。
(以下撰文用 ubuntu 做示範)
網路指令相關
這邊列出常用的網路指令,如果網路不通的事情,當然要優先處理。
列出網路介面與 IP 位址
$ ip a
$ ip addr show
$ ifconfig
這幾個指令都可以,輸出格式稍有不同。
設定 IP 位址 (IP Address)
$ sudo ip addr add 192.168.10.200/24 dev eth0
這邊用 192.168.10.200 做為例子,請修改成恰當的值。
列出路由閘道 Gateway 設定
$ route -n
設定網路閘道 Gateway
$ sudo route add default gw 192.168.10.1 eth0
這邊用 192.168.10.1 做為例子,請修改成恰當的值。
設定 DNS
$ sudo echo nameserver 8.8.8.8″ > /etc/resolv.conf
這邊用 8.8.8.8 的 Google DNS 做為例子,你也可以調整成你喜歡的。
DHCP Relase
釋放從 DHCP 取得的 IP 位址
$ sudo dhclient -r
指令等同 Windows 裡的 ipconfig /release
DHCP Renew
從 DHCP 重新取得新的 IP
$ sudo dhclient
指令等同 Windows 裡的 ipconfig /renew
啟動/關閉 網路介面 (ip 指令)
$ ip link set dev eth0 up
$ ip link set dev eth0 down
例如介面名稱為 eth0
,請自行修改成合適的網路名稱。
啟動/關閉 網路介面 (ifconfig 指令)
$ /sbin/ifconfig eth0 up
$ /sbin/ifconfig eth0 down
例如介面名稱為 eth0
,請自行修改成合適的網路名稱。
列出所有網路介面與狀態
$ ip link show
$ ifconfig -a
這二個都可以
檢查外部公有 IP (Public IP)
$ curl ipinfo.io/ip
一個簡單的指令可以查詢外部公有IP地址 (Public IP)
SSH 相關
安裝 SSH Server (應該預設就有安裝了)
應該預設就有安裝了,如果沒有安裝,請手動用指令安裝之。
(以下為 ubuntu 的指令)
$ sudo apt install ssh
$ sudo apt-get install openssh-server
開機預設啟動 ssh
$ sudo systemctl enable ssh
啟動 ssh
$ sudo systemctl start ssh
查看 ssh 狀態
$ sudo systemctl status ssh
使用 ssh key 取代密碼登入
增加方便性也加強安全性,建議用 ssh key (pem) 檔案來登入 ssh。
產生 ssh key
$ ssh-keygen
指定檔案,例如 id_rsa
檔案(檔名可自訂)。
密碼 passphrase 可以留空
將會產生 id_rsa
(私鑰) 與 id_rsa.pub
(公鑰) 檔案。
接下來的步驟將是把您的公鑰複製到伺服器上(或者是把私鑰下載回使用者電腦上)。
使用者(你)透過電腦上的私鑰來做連線。
自動複製 ssh key
(在 Client 端執行此指令)
這個步驟是自動把您的公鑰複製到伺服器上。
$ ssh-copy-id -i ~/.ssh/id_rsa -p 22 [email protected]
如果不能運作也不用太糾結,等等有手動的方式。
運行結果:
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/user/.ssh/id_rsa.pub"
The authenticity of host '[192.168.10.200]:22 ([192.168.10.200]:22)' can't be established.
ECDSA key fingerprint is SHA256:wYmwxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxcFme8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh -p '22' '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
另外一個指令,作法相同。
$ cat ~/.ssh/id_rsa.pub | ssh -p 22 [email protected] "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
或者手動複製產生之公鑰 (PublicKey) 到伺服器的 ~/.ssh/authorized_keys
檔案。
(如果沒有 .ssh
隱藏資料夾與 authorized_keys
檔案,請自行建立。)
設定 SSH 關閉密碼登入
$ vi /etc/ssh/sshd_config
找到這行並修改
PasswordAuthentication no
設定免密碼 sudo
(這個步驟非必要)
在設定之前,先調整預設開啟的編輯器。
因為小弟長期習慣用 vim 所以用此指令先切換預設開啟的編輯器
$ 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 15 manual mode
Press <enter> to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in manual mode
ubuntu 預設是開 nano 編輯器,可以用這個來修改
可以選擇 3 改用 vim 編輯器。
然後使用該指令編輯設定檔
$ sudo visudo
找到
%sudo ALL=(ALL:ALL) ALL
把它改成
%sudo ALL=(ALL:ALL) NOPASSWD: ALL
然後存檔離開。
連線 SSH
這個可以做為 bash script 以後方便使用。
$ ssh -i ~/.ssh/id_rsa -p 22 [email protected]
參考資料
https://www.cyberciti.biz/faq/ubuntu-linux-install-openssh-server/
https://www.digitalocean.com/community/tutorials/how-to-configure-ssh-key-based-authentication-on-a-linux-server
https://vitux.com/ubuntu-ip-address-management/
https://www.cyberciti.biz/faq/howto-linux-renew-dhcp-client-ip-address/
https://tldp.org/HOWTO/Linux+IPv6-HOWTO/ch05s02.html