Ubuntu Server 基礎應用

學渣角鹿白發表於2020-11-27

實驗環境

  • 1 臺 VMware Workstation 虛擬機器,4 塊網路卡(2塊橋接模式、2塊僅主機模式),安裝 ubuntu-18.04.3-server-amd64;
  • 實驗網路:
    外網(橋接):192.168.1.0/24(DNS & 閘道器:192.168.1.1)
    內網(僅主機):172.16.1.0/24

Ubuntu 映象下載

  • Server 版映象下載:
    http://cdimage.ubuntu.com/releases/
  • 桌面版映象下載:
    http://releases.ubuntu.com/

Ubuntu Server 系統初始化

參見《Ubuntu Server 系統初始化》

Ubuntu Server 網路配置

netplan 官方文件:https://netplan.io/reference/

靜態 IP 配置

配置如下:

  • eth0:192.168.1.111/24
  • eth1:192.168.1.112/24
  • eth2:172.16.1.111/24
  • eth3:172.16.1.112/24

編輯 netplan 配置檔案

root@ubuntu:~# vim /etc/netplan/01-netcfg.yaml 
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      dhcp6: no
      addresses: [192.168.1.111/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [192.168.1.1]
    eth1:
      dhcp4: no
      dhcp6: no
      addresses: [192.168.1.112/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [192.168.1.1]
    eth2:
      dhcp4: no
      dhcp6: no
      addresses: [172.16.1.111/24]
      nameservers:
        addresses: [172.16.1.250]
    eth3:
      dhcp4: no
      dhcp6: no
      addresses: [172.16.1.112/24]
      nameservers:
        addresses: [172.16.1.250]

使配置生效

root@ubuntu:~# netplan apply

驗證配置

  • 檢視網路配置:
root@ubuntu:~# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.111  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::20c:29ff:fe41:f5d7  prefixlen 64  scopeid 0x20<link>
        inet6 240e:324:79e:f400:20c:29ff:fe41:f5d7  prefixlen 64  scopeid 0x0<global>
        ether 00:0c:29:41:f5:d7  txqueuelen 1000  (Ethernet)
        RX packets 9835  bytes 11417564 (11.4 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3566  bytes 364990 (364.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.112  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::20c:29ff:fe41:f5e1  prefixlen 64  scopeid 0x20<link>
        inet6 240e:324:79e:f400:20c:29ff:fe41:f5e1  prefixlen 64  scopeid 0x0<global>
        ether 00:0c:29:41:f5:e1  txqueuelen 1000  (Ethernet)
        RX packets 456  bytes 43972 (43.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 58  bytes 5896 (5.8 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.1.111  netmask 255.255.255.0  broadcast 172.16.1.255
        inet6 fe80::20c:29ff:fe41:f5eb  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:41:f5:eb  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6  bytes 516 (516.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.1.112  netmask 255.255.255.0  broadcast 172.16.1.255
        inet6 fe80::20c:29ff:fe41:f5f5  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:41:f5:f5  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6  bytes 516 (516.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • 驗證內外網通訊:
root@ubuntu:~# ping www.baidu.com
PING www.a.shifen.com (180.101.49.11) 56(84) bytes of data.
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=2 ttl=52 time=38.7 ms
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=4 ttl=52 time=39.0 ms

root@ubuntu:~# ping 172.16.1.1
PING 172.16.1.1 (172.16.1.1) 56(84) bytes of data.
64 bytes from 172.16.1.1: icmp_seq=1 ttl=128 time=0.659 ms
64 bytes from 172.16.1.1: icmp_seq=2 ttl=128 time=0.392 ms

bond 配置

配置如下:

  • eth0 和 eth1 繫結為 bond0(192.168.1.111/24)
  • eth2 和 eth3 繫結為 bond1(172.16.1.111/24)
  • bond 模式為主從,每 100ms 檢測一次;

編輯 netplan 配置檔案

root@ubuntu:~# vim /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      dhcp6: no
    eth1:
      dhcp4: no
      dhcp6: no
    eth2:
      dhcp4: no
      dhcp6: no
    eth3:
      dhcp4: no
      dhcp6: no
  bonds:
    bond0:
      interfaces:
        - eth0
        - eth1
      addresses: [192.168.1.111/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [192.168.1.1]
      parameters:
        mode: active-backup
        mii-monitor-interval: 100
    bond1:
      interfaces:
        - eth2
        - eth3
      addresses: [172.16.1.111/24]
      parameters:
        mode: active-backup
        mii-monitor-interval: 100

使配置生效

root@ubuntu:~# netplan apply

驗證配置

  • 檢視網路配置:
root@ubuntu:~# ifconfig
bond0: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST>  mtu 1500
        inet 192.168.1.111  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 240e:324:79e:f400:54e6:c4ff:fe6d:50fe  prefixlen 64  scopeid 0x0<global>
        inet6 fe80::54e6:c4ff:fe6d:50fe  prefixlen 64  scopeid 0x20<link>
        ether 56:e6:c4:6d:50:fe  txqueuelen 1000  (Ethernet)
        RX packets 264  bytes 24514 (24.5 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 106  bytes 15962 (15.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

bond1: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST>  mtu 1500
        inet 172.16.1.111  netmask 255.255.255.0  broadcast 172.16.1.255
        inet6 fe80::f460:71ff:fedf:14e9  prefixlen 64  scopeid 0x20<link>
        ether f6:60:71:df:14:e9  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 10  bytes 796 (796.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 56:e6:c4:6d:50:fe  txqueuelen 1000  (Ethernet)
        RX packets 12927  bytes 11717005 (11.7 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4772  bytes 550676 (550.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 56:e6:c4:6d:50:fe  txqueuelen 1000  (Ethernet)
        RX packets 2347  bytes 233495 (233.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 265  bytes 31413 (31.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth2: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether f6:60:71:df:14:e9  txqueuelen 1000  (Ethernet)
        RX packets 70  bytes 8286 (8.2 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 24  bytes 1872 (1.8 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth3: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether f6:60:71:df:14:e9  txqueuelen 1000  (Ethernet)
        RX packets 74  bytes 8640 (8.6 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 19  bytes 1490 (1.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • 驗證內外網通訊:
root@ubuntu:~# ping www.baidu.com -c 4         
PING www.a.shifen.com (180.101.49.11) 56(84) bytes of data.
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=1 ttl=52 time=38.6 ms
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=2 ttl=52 time=39.3 ms
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=3 ttl=52 time=38.8 ms
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=4 ttl=52 time=38.8 ms

root@ubuntu:~# ping 172.16.1.1 -c 4             
PING 172.16.1.1 (172.16.1.1) 56(84) bytes of data.
64 bytes from 172.16.1.1: icmp_seq=1 ttl=128 time=0.692 ms
64 bytes from 172.16.1.1: icmp_seq=2 ttl=128 time=0.430 ms
64 bytes from 172.16.1.1: icmp_seq=3 ttl=128 time=0.434 ms
64 bytes from 172.16.1.1: icmp_seq=4 ttl=128 time=0.427 ms

bridge 配置

配置如下:

  • bond0 橋接至 br0;
  • bond1 橋接至 br1;

編輯 netplan 配置檔案

root@ubuntu:~# vim /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      dhcp6: no
    eth1:
      dhcp4: no
      dhcp6: no
    eth2:
      dhcp4: no
      dhcp6: no
    eth3:
      dhcp4: no
      dhcp6: no

  bonds:
    bond0:
      interfaces:
        - eth0
        - eth1
      parameters:
        mode: active-backup
        mii-monitor-interval: 100

    bond1:
      interfaces:
        - eth2
        - eth3
      parameters:
        mode: active-backup
        mii-monitor-interval: 100

  bridges:
    br0:
      interfaces:
        - bond0
      addresses: [192.168.1.111/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [192.168.1.1]

    br1:
      interfaces:
        - bond1
      addresses: [172.16.1.111/24]

使配置生效

root@ubuntu:~# netplan apply

驗證配置

  • 檢視網路配置:
root@ubuntu:~# ifconfig
bond0: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST>  mtu 1500
        ether 56:e6:c4:6d:50:fe  txqueuelen 1000  (Ethernet)
        RX packets 4475  bytes 438417 (438.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2084  bytes 258182 (258.1 KB)
        TX errors 0  dropped 6 overruns 0  carrier 0  collisions 0

bond1: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST>  mtu 1500
        ether f6:60:71:df:14:e9  txqueuelen 1000  (Ethernet)
        RX packets 186  bytes 12310 (12.3 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 213  bytes 14058 (14.0 KB)
        TX errors 0  dropped 5 overruns 0  carrier 0  collisions 0

br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.111  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::77:4ff:fe24:4abf  prefixlen 64  scopeid 0x20<link>
        inet6 240e:324:79e:f400:77:4ff:fe24:4abf  prefixlen 64  scopeid 0x0<global>
        ether 02:77:04:24:4a:bf  txqueuelen 1000  (Ethernet)
        RX packets 341  bytes 37563 (37.5 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 229  bytes 27660 (27.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

br1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.1.111  netmask 255.255.255.0  broadcast 172.16.1.255
        inet6 fe80::2449:37ff:fe9b:91f8  prefixlen 64  scopeid 0x20<link>
        ether 26:49:37:9b:91:f8  txqueuelen 1000  (Ethernet)
        RX packets 7  bytes 657 (657.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 17  bytes 1362 (1.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 56:e6:c4:6d:50:fe  txqueuelen 1000  (Ethernet)
        RX packets 13897  bytes 11811255 (11.8 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4775  bytes 550912 (550.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 56:e6:c4:6d:50:fe  txqueuelen 1000  (Ethernet)
        RX packets 5588  bytes 553148 (553.1 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2240  bytes 273397 (273.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth2: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether f6:60:71:df:14:e9  txqueuelen 1000  (Ethernet)
        RX packets 144  bytes 13092 (13.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 111  bytes 7178 (7.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth3: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether f6:60:71:df:14:e9  txqueuelen 1000  (Ethernet)
        RX packets 186  bytes 16144 (16.1 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 135  bytes 9446 (9.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • 驗證內外網通訊:
root@ubuntu:~# ping www.baidu.com -c 4
PING www.a.shifen.com (180.101.49.11) 56(84) bytes of data.
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=1 ttl=52 time=38.7 ms
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=2 ttl=52 time=38.8 ms
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=3 ttl=52 time=38.6 ms
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=4 ttl=52 time=38.9 ms

--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3006ms
rtt min/avg/max/mdev = 38.675/38.817/38.938/0.261 ms
root@ubuntu:~# ping 172.16.1.1 -c 4
PING 172.16.1.1 (172.16.1.1) 56(84) bytes of data.
64 bytes from 172.16.1.1: icmp_seq=1 ttl=128 time=0.581 ms
64 bytes from 172.16.1.1: icmp_seq=2 ttl=128 time=0.436 ms
64 bytes from 172.16.1.1: icmp_seq=3 ttl=128 time=0.404 ms
64 bytes from 172.16.1.1: icmp_seq=4 ttl=128 time=0.438 ms

--- 172.16.1.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3051ms

Ubuntu 程式包管理

修改 apt 倉庫

  • 修改官方倉庫為阿里雲倉庫:
$ sudo vim /etc/apt/sources.list
# 將預設的cn.archive.ubuntu.com替換為阿里雲的mirrors.aliyun.com
:%s@cn.archive.ubuntu.com@mirrors.aliyun.com@g

$ grep -v '^#' /etc/apt/sources.list | grep -v '^$'
deb http://mirrors.aliyun.com/ubuntu bionic main restricted
deb http://mirrors.aliyun.com/ubuntu bionic-updates main restricted
deb http://mirrors.aliyun.com/ubuntu bionic universe
deb http://mirrors.aliyun.com/ubuntu bionic-updates universe
deb http://mirrors.aliyun.com/ubuntu bionic multiverse
deb http://mirrors.aliyun.com/ubuntu bionic-updates multiverse
deb http://mirrors.aliyun.com/ubuntu bionic-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu bionic-security main restricted
deb http://mirrors.aliyun.com/ubuntu bionic-security universe
deb http://mirrors.aliyun.com/ubuntu bionic-security multiverse

$ sudo apt update

apt/apt-get 應用

SYNOPSIS
apt [-h] [-o=config_string] [-c=config_file] [-t=target_release] [-a=architecture] {list | search | show | update | install pkg [{=pkg_version_number | /target_release}]… | remove pkg… | upgrade | full-upgrade | edit-sources | {-v |–version} | {-h | --help}}

apt list

list - list packages based on package names

基於指定的程式包名稱,列出 apt 倉庫中匹配的程式包(類似於 yum list);

  • 列出 apache 相關程式包:
root@ubuntu:~# apt list apache*
Listing... Done
apache2/bionic-updates,bionic-security 2.4.29-1ubuntu4.14 amd64
apache2-bin/bionic-updates,bionic-security 2.4.29-1ubuntu4.14 amd64
apache2-data/bionic-updates,bionic-updates,bionic-security,bionic-security 2.4.29-1ubuntu4.14 all
apache2-dbg/bionic-updates,bionic-security 2.4.29-1ubuntu4.14 amd64
apache2-dev/bionic-updates,bionic-security 2.4.29-1ubuntu4.14 amd64
apache2-doc/bionic-updates,bionic-updates,bionic-security,bionic-security 2.4.29-1ubuntu4.14 all
apache2-ssl-dev/bionic-updates,bionic-security 2.4.29-1ubuntu4.14 amd64
apache2-suexec-custom/bionic-updates,bionic-security 2.4.29-1ubuntu4.14 amd64
apache2-suexec-pristine/bionic-updates,bionic-security 2.4.29-1ubuntu4.14 amd64
apache2-utils/bionic-updates,bionic-security 2.4.29-1ubuntu4.14 amd64
apachedex/bionic,bionic 1.6.2-1 all
apacheds/bionic-updates,bionic-updates,bionic-security,bionic-security 2.0.0~M24-2~18.04 all
apachetop/bionic 0.12.6-18build2 amd64

apt show

show - show package details

檢視指定程式包的詳細資訊;

  • 檢視 apache2 的詳細資訊:
root@ubuntu:~# apt show apache2
Package: apache2
Version: 2.4.29-1ubuntu4.14
Priority: optional
Section: web
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian Apache Maintainers <debian-apache@lists.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 536 kB
Provides: httpd, httpd-cgi
Pre-Depends: dpkg (>= 1.17.14)
Depends: lsb-base, procps, perl, mime-support, apache2-bin (= 2.4.29-1ubuntu4.14), apache2-utils (= 2.4.29-1ubuntu4.14), apache2-data (= 2.4.29-1ubuntu4.14), perl:any
Recommends: ssl-cert
Suggests: www-browser, apache2-doc, apache2-suexec-pristine | apache2-suexec-custom, ufw
Conflicts: apache2.2-bin, apache2.2-common
Replaces: apache2.2-bin, apache2.2-common
Homepage: http://httpd.apache.org/
Task: lamp-server
Supported: 5y
Download-Size: 95.1 kB
APT-Sources: http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 Packages
Description: Apache HTTP Server
 The Apache HTTP Server Project's goal is to build a secure, efficient and
 extensible HTTP server as standards-compliant open source software. The
 result has long been the number one web server on the Internet.
 .
 Installing this package results in a full installation, including the
 configuration files, init scripts and support scripts.

N: There is 1 additional record. Please use the '-a' switch to see it

apt search

search - search in package descriptions

基於程式包描述中的內容搜尋相應程式包;

  • 摘取上面 apache2 的 Description 部分資訊,驗證搜尋結果:
root@ubuntu:~# apt search "The Apache HTTP Server Project"        
Sorting... Done
Full Text Search... Done
apache2/bionic-updates,bionic-security 2.4.29-1ubuntu4.14 amd64
  Apache HTTP Server

apache2-bin/bionic-updates,bionic-security 2.4.29-1ubuntu4.14 amd64
  Apache HTTP Server (modules and other binary files)

apache2-data/bionic-updates,bionic-updates,bionic-security,bionic-security 2.4.29-1ubuntu4.14 all
  Apache HTTP Server (common files)

apache2-dbg/bionic-updates,bionic-security 2.4.29-1ubuntu4.14 amd64
  Apache debugging symbols

apache2-dev/bionic-updates,bionic-security 2.4.29-1ubuntu4.14 amd64
  Apache HTTP Server (development headers)

apache2-doc/bionic-updates,bionic-updates,bionic-security,bionic-security 2.4.29-1ubuntu4.14 all
  Apache HTTP Server (on-site documentation)

apache2-ssl-dev/bionic-updates,bionic-security 2.4.29-1ubuntu4.14 amd64
  Apache HTTP Server (mod_ssl development headers)

apt install

install - install packages

安裝指定程式包;

  • 安裝 apache2:
root@ubuntu:~# apt install apache2

apt remove

remove - remove packages

解除安裝指定程式包(會保留已修改的配置檔案);

  • 解除安裝 apache2:
root@ubuntu:~# apt remove apache2

apt autoremove

autoremove - Remove automatically all unused packages

autoremove is used to remove packages that were automatically installed to satisfy dependencies for other packages and are now no longer needed as dependencies changed or the package(s) needing them were removed in the meantime.

You should check that the list does not include applications you have grown to like even though they were once installed just as a dependency of another package. You can mark such a package as manually installed by using apt-mark(8). Packages which you have installed explicitly via install are also never proposed for automatic removal.

自動刪除不再需要的被依賴程式包(這些程式包是為了解決依賴關係而自動安裝的,在執行 autoremove 時,已經不存在當時的依賴關係-即依賴關係改變、或依賴它們的程式包已被刪除);

使用 autoremove 前,確保已經對之前為了解決某依賴關係而手動安裝的程式包進行了標記(apt-mark);

apt update

update - update list of available packages

更新倉庫列表;一般在更新了 apt 倉庫後執行;

apt upgrade

upgrade - upgrade the system by installing/upgrading packages

full-upgrade - upgrade the system by removing/installing/upgrading packages

升級所有已安裝的有更新版本的程式包;

full-upgrade 在必要時會解除安裝舊版本的程式包;

apt edit-sources

edit-sources - edit the source information file

更改 apt 倉庫配置;和 直接 vim /etc/apt/sources.list 一個效果;

apt purge

Removing a package removes all packaged data, but leaves usually small (modified) user configuration files behind, in case the remove was an accident.
Just issuing an installation request for the accidentally removed package will restore its function as before in that case. On the other hand you can get rid of these leftovers by calling purge even on already removed packages. Note that this does not affect any data or configuration stored in your home directory.

解除安裝指定程式包(並刪除配置檔案);
也可用於清除已經解除安裝了的程式包的資料和配置檔案(但不包括儲存在家目錄的相‘關資料和配置);

dpkg 應用

dpkg -I

-I, --info archive [control-file…]
Show information about a package

檢視程式包資訊;

root@ubuntu:~# dpkg -I nginx_1.18.0-2~bionic_amd64.deb
 new Debian package, version 2.0.
 size 855966 bytes: control archive=2692 bytes.
     314 bytes,    14 lines      conffiles            
     533 bytes,    15 lines      control              
     840 bytes,    13 lines      md5sums              
    3022 bytes,    82 lines   *  postinst             #!/bin/sh
    1552 bytes,    58 lines   *  postrm               #!/bin/sh
    1306 bytes,    62 lines   *  preinst              #!/bin/sh
     483 bytes,    24 lines   *  prerm                #!/bin/sh
 Package: nginx
 Version: 1.18.0-2~bionic
 Architecture: amd64
 Maintainer: Sergey Budnevitch <sb@nginx.com>
 Installed-Size: 2942
 Depends: libc6 (>= 2.27), libpcre3, libssl1.1 (>= 1.1.1), zlib1g (>= 1:1.1.4), lsb-base (>= 3.0-6), adduser
 Conflicts: nginx-common, nginx-core
 Replaces: nginx-common, nginx-core
 Provides: httpd, nginx, nginx-r1.18.0
 Section: httpd
 Priority: optional
 Homepage: http://nginx.org
 Description: high performance web server
  nginx [engine x] is an HTTP and reverse proxy server, as well as
  a mail proxy server.

dpkg -c

-c, --contents archive
List contents of a deb package.

檢視指定程式包的內容(即安裝後會生成哪些檔案);

root@ubuntu:~# dpkg -c nginx_1.18.0-2~bionic_amd64.deb | head -10
drwxr-xr-x root/root         0 2020-10-29 17:59 ./
drwxr-xr-x root/root         0 2020-10-29 17:59 ./etc/
drwxr-xr-x root/root         0 2020-10-29 17:59 ./etc/default/
-rw-r--r-- root/root       125 2020-10-29 17:59 ./etc/default/nginx
-rw-r--r-- root/root       200 2020-10-29 17:59 ./etc/default/nginx-debug
drwxr-xr-x root/root         0 2020-10-29 17:59 ./etc/init.d/
-rwxr-xr-x root/root      4287 2020-10-29 17:59 ./etc/init.d/nginx
-rwxr-xr-x root/root      4312 2020-10-29 17:59 ./etc/init.d/nginx-debug
drwxr-xr-x root/root         0 2020-10-29 17:59 ./etc/logrotate.d/
-rw-r--r-- root/root       351 2020-10-29 17:59 ./etc/logrotate.d/nginx

dpkg -i

-i, --install package-file…
Install the package. If --recursive or -R option is specified, package-file must refer to a directory instead.

安裝指定程式包(可以使用 --recursive 或 -R 來指定一個目錄進行遞迴安裝其中的程式包);

root@ubuntu:~# dpkg -i nginx_1.18.0-2~bionic_amd64.deb

dpkg -r

-r, --remove package…|-a|–pending
Remove an installed package. This removes everything except conffiles, which may avoid having to reconfigure the package if it is reinstalled later (conffiles are configuration files that are listed in the DEBIAN/conffiles control file). If -a or --pending is given instead of a package name, then all packages unpacked, but marked to be removed in file /var/lib/dpkg/status, are removed.

解除安裝指定程式包,保留配置檔案(或可以在 /var/lib/dpkg/status 中指定要解除安裝的程式包,並使用 -a 或 --pending 來進行解除安裝);

dpkg -P

-P, --purge package…|-a|–pending
Purge an installed or already removed package. This removes everything, including conffiles. If -a or --pending is given instead of a package name, then all packages unpacked or removed, but marked to be purged in file /var/lib/dpkg/status, are purged.

Note:  some  configuration files might be unknown to dpkg because they are created and handled separately through the configuration scripts. In that case, dpkg won't remove them by itself, but the package's postrm script (which is called by dpkg), has to take care of their removal  during  purge. Of course, this only applies to files in system directories, not configuration files written to individual users' home directories.

清除指定程式包,包括刪除配置檔案;

dpkg -l

列出所有已安裝的程式包;

root@ubuntu:~# dpkg -l | head -10
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                                  Version                           Architecture Description
+++-=====================================-=================================-============-===============================================================================
ii  accountsservice                       0.6.45-1ubuntu1                   amd64        query and manipulate user account information
ii  acl                                   2.2.52-3build1                    amd64        Access control list utilities
ii  acpid                                 1:2.0.28-1ubuntu1                 amd64        Advanced Configuration and Power Interface event daemon
ii  adduser                               3.116ubuntu1                      all          add and remove users and groups
ii  amd64-microcode                       3.20180524.1~ubuntu0.18.04.2      amd64        Processor microcode firmware for AMD CPUs

相關文章