Ubuntu Server 18.04 系統(tǒng)代號為 bionic,可以通過 'lsb_release -c' 命令查看,這是一個長期支持版本。
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.4 LTS
Release: 18.04
Codename: bionic
目錄:
1. 下載
2. 創(chuàng)建 U 盤安裝盤
3. 安裝 Ubuntu Server
3.1. 在第一個安裝界面 "Language" 中選擇默認的 English 選項進行安裝
3.2. 在安裝菜單界面選擇 "Install Ubuntu Server"
3.3. 在 "Welcome" 語言選擇界面也同樣選擇 English
3.4. 在 "Keyboard configuration" 配置鍵盤布局
3.5. 在 "Network connections" 配置網(wǎng)絡連接
3.6. 在 "Configure proxy" 配置網(wǎng)絡代理
3.7. 在 "Configure Ubuntu archive mirror" 更換為國內鏡像源
3.8. 在 "Filesystem setup" 界面選擇默認的全盤自動分區(qū)方案
3.9. 在 "Profile setup" 設置登陸賬號和主機名
3.10. 在 "SSH Setup" 選擇安裝 SSH
3.11. 確認并等待安裝完成
3.12. 查看磁盤分區(qū)信息
3.13. 安裝后系統(tǒng)默認編碼是 en_US.UTF-8
3.14. 更改系統(tǒng)默認時區(qū)為國內時區(qū)
3.15. 設置固定 IP
3.16. 系統(tǒng)更新
1. 下載
- 官網(wǎng)下載:http://releases.ubuntu.com - 國內下載速度慢
- 阿里鏡像:https://mirrors.aliyun.com/ubuntu-releases - 國內下載速度快
當前(2020-03-16)的最新 LTS 版為:ubuntu-18.04.4-live-server-amd64.iso。
2. 創(chuàng)建 U 盤安裝盤
官方文檔:https://help.ubuntu.com/community/Installation/FromUSBStick
官方指引:
- Windows 下使用 Rufus
- MacOS 下使用 UNetbootin for Mac OS X
打開 UNetBootin 前先自行將 U 盤用主引導記錄方式格式化為 FAT32 格式,否則 UNetBootin 檢測不到 U 盤
- Ubuntu 桌面系統(tǒng)使用自帶的
Ubuntu Startup Disk Creator - 其它 Linux 系統(tǒng)使用 UNetbootin 或者 mkusb
以下為 MacOS 下使用 UNetBootin 的 U 盤制作截圖:
- 點選 "光盤鏡像" 然后選擇下載好的 Ubuntu ISO 文件,驅動器選擇對應的 U 盤:
unetbootin-mac-01.png - 點擊確定開始制作:
unetbootin-mac-02.png - 制作完成,點擊退出,就可使用此 U 盤啟動 Ubuntu Server 的安裝:
unetbootin-mac-03.png
3. 安裝 Ubuntu Server
以下安裝步驟既適用于 U 盤啟動盤的安裝,也適合于使用光盤啟動的安裝。
3.1. 在第一個安裝界面 "Language" 中選擇默認的 English 選項進行安裝

由于安裝的是 Server 版,是沒有 GUI 的,一般都是通過遠程 ssh 連接進服務器維護,故推薦直接安裝 English,不推薦使用其它語言。
3.2. 在安裝菜單界面選擇 "Install Ubuntu Server"

3.3. 在 "Welcome" 語言選擇界面也同樣選擇 English

3.4. 在 "Keyboard configuration" 配置鍵盤布局
使用默認配置即可:

3.5. 在 "Network connections" 配置網(wǎng)絡連接
默認自動 IP 配置,也可以手動指定為固定 IP。推薦安裝完畢后再重新設置固定 IP,因此這里使用默認配置:

3.6. 在 "Configure proxy" 配置網(wǎng)絡代理
這里使用默認的無代理配置:

3.7. 在 "Configure Ubuntu archive mirror" 更換為國內鏡像源
在 "Configure Ubuntu archive mirror" 安裝界面記得更換為國內鏡像源地址,否則默認自動連接到 Ubuntu 的官方源 https://cn.archive.ubuntu.com/ubuntu,在后續(xù)的 "Installing system" 安裝界面因為會聯(lián)網(wǎng)下載很多包,速度將非常慢。
國內鏡像源推薦使用阿里的:http://mirrors.aliyun.com/ubuntu

系統(tǒng)安裝后可以看到
/etc/apt/sources.list文件已更改為使用阿里源,Ubuntu 原始的源配置會自動備份在文件/etc/apt/sources.list.curtin.old。
3.8. 在 "Filesystem setup" 界面選擇默認的全盤自動分區(qū)方案
這里可以對硬盤進行分區(qū),可自動分區(qū)也可自行手動分區(qū)。這里選擇全盤自動分區(qū)方案,即在 "Filesystem setup" 界面選擇第一個選項 "Use An Entire Disk",確認后會自動分區(qū)及格式化整個磁盤為 ext4 格式:




3.9. 在 "Profile setup" 設置登陸賬號和主機名
相關信息按需設置,如:
- Your name 設置為 gftaxi
- server'name 設置為 ubuntu-test-server
- username 設置為 gftaxi
- password 設置為 gftaxi20200101

3.10. 在 "SSH Setup" 選擇安裝 SSH
在 "SSH Setup" 界面選擇 "Install OpenSSH server",默認是沒有勾選的:

安裝 "Install OpenSSH server" 后才可以在安裝完畢后通過 ssh 客戶端遠程連接。
3.11. 確認并等待安裝完成
"SSH Setup" 界面確認后,就耐心等待過程完成:


安裝完畢后需手動點擊上面的 "Reboot" 重啟,重啟后就可以看到成功的登陸界面了:

首次登陸后的終端界面如下:

3.12. 查看磁盤分區(qū)信息
此次是使用 32GB 的虛擬磁盤執(zhí)行的虛擬機安裝。分別使用 parted、fdisk、lsblk 查詢分區(qū)信息如下:
$ sudo parted -l
Model: ATA Ubuntu18.04-0 SS (scsi)
Disk /dev/sda: 34.4GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 2097kB 1049kB bios_grub
2 2097kB 34.4GB 34.4GB ext4
$ sudo fdisk -l
Disk /dev/loop0: 89.1 MiB, 93417472 bytes, 182456 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/sda: 32 GiB, 34359738368 bytes, 67108864 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 55DD2D05-4575-4B21-90DE-FC9CAA535098
Device Start End Sectors Size Type
/dev/sda1 2048 4095 2048 1M BIOS boot
/dev/sda2 4096 67106815 67102720 32G Linux filesystem
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 89.1M 1 loop /snap/core/8268
sda 8:0 0 32G 0 disk
├─sda1 8:1 0 1M 0 part
└─sda2 8:2 0 32G 0 part /
sr0 11:0 1 1024M 0 rom
由此可見 Ubuntu 默認全盤分區(qū)結果是:
- GPT 分區(qū)
- 沒有獨立的 swap 分區(qū)(經檢測是在根分區(qū)下創(chuàng)建一個大小為內存兩倍的 swap.img 的文件,可以通過 swapon -s 命令查看交換分區(qū)的使用情況)
$ swapon -s Filename Type Size Used Priority /swap.img file 2006012 0 -2
作為參考,執(zhí)行 'df -h' 結果為:
$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 460M 0 460M 0% /dev
tmpfs 98M 1.1M 97M 2% /run
/dev/sda2 32G 4.3G 26G 15% /
tmpfs 490M 0 490M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 490M 0 490M 0% /sys/fs/cgroup
/dev/loop0 90M 90M 0 100% /snap/core/8268
tmpfs 98M 0 98M 0% /run/user/1000
3.13. 安裝后系統(tǒng)默認編碼是 en_US.UTF-8
# 查看系統(tǒng)當前編碼
$ locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
$ cat /etc/default/locale
LANG=en_US.UTF-8
對于服務器而言,UTF-8 編碼足爾,默認值已滿足常規(guī)使用需求,無需修改。如果需要修改,直接編輯文件 /etc/default/locale 然后重啟即可。
3.14. 更改系統(tǒng)默認時區(qū)為國內時區(qū)
安裝后默認是 UTC 時區(qū),要更改為國內時區(qū):
# 查看當前時區(qū)信息
$ date
Sat Mar 21 05:02:37 UTC 2020
$ date -R
Sat, 21 Mar 2020 05:02:42 +0000
$ ls -l /etc/localtime
lrwxrwxrwx 1 root root 27 Feb 3 18:23 /etc/localtime -> /usr/share/zoneinfo/Etc/UTC
$ ls -l /usr/share/zoneinfo/Asia/Shanghai
lrwxrwxrwx 1 root root 6 Sep 20 2019 /usr/share/zoneinfo/Asia/Shanghai -> ../PRC
# 修改為國內時區(qū)
$ sudo rm /etc/localtime
$ sudo ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 查看修改后的時區(qū)結果
$ ls -l /etc/localtime
lrwxrwxrwx 1 root root 33 Mar 21 13:05 /etc/localtime -> /usr/share/zoneinfo/Asia/Shanghai
$ date
Sat Mar 21 13:06:16 CST 2020
$ date -R
Sat, 21 Mar 2020 13:06:20 +0800
系統(tǒng)支持的時區(qū)信息在 /usr/share/zoneinfo/ 目錄下,修改 /etc/localtime 軟鏈接到相應的時區(qū)即可,上述命令就是查看和將系統(tǒng) /etc/localtime 默認軟鏈接到的 /usr/share/zoneinfo/Etc/UTC 更改為 /usr/share/zoneinfo/Asia/Shanghai,這樣修改的是系統(tǒng)的全局默認時區(qū)。
如果特定用戶想使用不同的時區(qū),只要在此特定用戶下創(chuàng)建一個環(huán)境變量 TZ,將其值設定為相應的時區(qū)值即可,如:export TZ='Asia/Shanghai'。不同時區(qū)的 TZ 值可以通過執(zhí)行 tzselect 命令一步一步的指引你得到:
$ tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, "coord", or "TZ".
1) Africa
2) Americas
3) Antarctica
4) Asia
5) Atlantic Ocean
6) Australia
7) Europe
8) Indian Ocean
9) Pacific Ocean
10) coord - I want to use geographical coordinates.
11) TZ - I want to specify the time zone using the Posix TZ format.
#? 4
Please select a country whose clocks agree with yours.
1) Afghanistan 18) Israel 35) Palestine
2) Armenia 19) Japan 36) Philippines
3) Azerbaijan 20) Jordan 37) Qatar
4) Bahrain 21) Kazakhstan 38) Russia
5) Bangladesh 22) Korea (North) 39) Saudi Arabia
6) Bhutan 23) Korea (South) 40) Singapore
7) Brunei 24) Kuwait 41) Sri Lanka
8) Cambodia 25) Kyrgyzstan 42) Syria
9) China 26) Laos 43) Taiwan
10) Cyprus 27) Lebanon 44) Tajikistan
11) East Timor 28) Macau 45) Thailand
12) Georgia 29) Malaysia 46) Turkmenistan
13) Hong Kong 30) Mongolia 47) United Arab Emirates
14) India 31) Myanmar (Burma) 48) Uzbekistan
15) Indonesia 32) Nepal 49) Vietnam
16) Iran 33) Oman 50) Yemen
17) Iraq 34) Pakistan
#? 9
Please select one of the following time zone regions.
1) Beijing Time
2) Xinjiang Time
#? 1
The following information has been given:
China
Beijing Time
Therefore TZ='Asia/Shanghai' will be used.
Selected time is now: Mon Mar 16 19:48:25 CST 2020.
Universal Time is now: Mon Mar 16 11:48:25 UTC 2020.
Is the above information OK?
1) Yes
2) No
#? 1
You can make this change permanent for yourself by appending the line
TZ='Asia/Shanghai'; export TZ
to the file '.profile' in your home directory; then log out and log in again.
Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Shanghai
3.15. 設置固定 IP
Ubuntu 從 17.10 開始引入了新的工具 netplan 來設置網(wǎng)絡,不再推薦使用以前的 /etc/network/interfaces 配置文件。netplan 的默認描述文件在 /etc/netplan/*.yaml 里。對于 Ubuntu 18.04 Server 這個文件的名稱是 /etc/netplan/50-cloud-init.yaml。
查看當前的網(wǎng)絡配置:
$ ls -l /etc/network
total 12
drwxr-xr-x 2 root root 4096 Mar 16 16:34 if-pre-up.d
drwxr-xr-x 2 root root 4096 Mar 16 16:35 if-up.d
-rw-r--r-- 1 root root 190 Feb 4 02:22 interfaces
$ cat /etc/network/interfaces
# ifupdown has been replaced by netplan(5) on this system. See
# /etc/netplan for current configuration.
# To re-enable ifupdown on this system, you can run:
# sudo apt install ifupdown
$ ls -l /etc/netplan
total 4
-rw-r--r-- 1 root root 391 Mar 16 16:40 50-cloud-init.yaml
$ cat /etc/netplan/50-cloud-init.yaml
# This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
ethernets:
enp0s5:
dhcp4: true
version: 2
$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp0s5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:1c:42:0b:cc:0c brd ff:ff:ff:ff:ff:ff
inet 10.211.55.9/24 brd 10.211.55.255 scope global dynamic enp0s5
valid_lft 1247sec preferred_lft 1247sec
inet6 fdb2:2c26:f4e4:0:21c:42ff:fe0b:cc0c/64 scope global dynamic mngtmpaddr noprefixroute
valid_lft 2591718sec preferred_lft 604518sec
inet6 fe80::21c:42ff:fe0b:cc0c/64 scope link
valid_lft forever preferred_lft forever
從上述文件 /etc/netplan/50-cloud-init.yaml 的內容可以看到,僅設置了 network.ethernets.enp0s5.dhcp4=true,即自動 IP 配置。如下修改 enp0s5 節(jié)點的值來設置固定 IP:
$ sudo vi /etc/netplan/50-cloud-init.yaml
network:
version: 2
ethernets:
enp0s5:
dhcp4: false
dhcp6: false
# 可同時設多個,對于 IP4 每個值為 "IP地址/掩碼長度"
addresses: [192.168.0.60/24, 'fe80:0:0:0:0:0:c0a8:64d3']
# 網(wǎng)關
gateway4: 192.168.0.1
# 設置 DNS
nameservers:
addresses: [192.168.0.1]
# 完成修改后執(zhí)行下面的命令使配置生效
$ sudo netplan apply
注意 yaml 文件的縮進務必使用空格而不是 tab 鍵。
支持的配置屬性:
| 屬性名 | 值類型 | 說明 |
|---|---|---|
| enp0s5 | string | 指定需配置網(wǎng)絡接口的名稱 |
| dhcp4 | boolean | 是否打開 IPv4 的 dhcp,值為 true 或 false |
| dhcp6 | boolean | 是否打開 IPv6 的 dhcp,值為 true 或 false |
| addresses | array | 設置靜態(tài) IP 地址,如 192.168.0.100 |
| gateway4 | string | 設置默認網(wǎng)關的 IPv4 地址,如 192.168.0.1 |
| nameservers | {addresses, ...} | 指定域名服務器的 IP 地址 |
netplan 官方配置例子可以參考 《Netplan configuration examples》。
3.16. 系統(tǒng)更新
更新軟件列表:
$ sudo apt-get update
這個命令會訪問源列表(
/etc/apt/sources/list)里的每個網(wǎng)址,并讀取軟件列表,然后保存在本地電腦。軟件包管理器里看到的軟件列表,都是通過 update 命令更新的。
更新軟件包:
$ sudo apt-get upgrade
這個命令會把本地已安裝的軟件,與剛下載的軟件列表里對應軟件進行對比,如果發(fā)現(xiàn)已安裝的軟件版本太低,就提示更新。


