Ubuntu Server 基础应用

实验环境

  • 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值