Ubuntu 22.04 离线安装软件包
在使用最小化安装时,默认是不带有vim 或者nano编辑器的,如果你的环境不能上外网就需要离线安装。
首先你需要先找一台可以上网的ubuntu系统(虚拟机搭建也行),下载所有的依赖包,然后上传到需要安装的服务器上手动安装。
我已经使用sudo -i 切换到了root用户
如下示例:
root@localhost:/opt# mkdir Package
root@localhost:/opt# cd Package/
下载软件包
root@localhost:/opt/Package# apt-get download vim
Get:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-updates/main amd64 vim amd64 2:8.2.3995-1ubuntu2.20 [1729 kB]
Fetched 1729 kB in 1s (1555 kB/s)
查看vim所有依赖包
如果在下载软件包过程中报错:
couldn't be accessed by user '_apt'. - pkgAcquire::Run (13: Permission denied)
时,这是因为在下载软件包的时候使用_apt 权限不足所以切换到root用户下载了。saz
root@localhost:/opt/Package# apt-cache depends vim
vim
Depends: vim-common
Depends: vim-runtime
Depends: libacl1
Depends: libc6
Depends: libgpm2
Depends: libpython3.10
Depends: libselinux1
Depends: libsodium23
Depends: libtinfo6
Suggests: <ctags>
exuberant-ctags
universal-ctags
Suggests: vim-doc
Suggests: vim-scripts
- Depends 的必要下
- Suggests 建议下载
到这里只需要把所有依赖的包都下载到本地,然后打包到需要安装的服务器上,dpkg -i *.deb
即可安装
这种适合安装一个或者少个软件,如果要一次性安装多个软件就会有点不方便,下面介绍一次性安装多个软件以及依赖。
root@localhost:~# mkdir Package
root@localhost:~# cd Package
sudo apt-get download $(apt-cache depends --recurse --no-recommends --no-suggests --no-conflicts --no-breaks --no-replaces --no-enhances <packagename> | grep "^\w" | sort -u)
- apt-get download 仅仅下载软件包
apt-cache depends --recurse --no-recommends --no-suggests --no-conflicts --no-breaks --no-replaces --no-enhances <packagename> | grep "^\w" | sort -u
-
<packagename> 指明你要下载的软件包,可以有多个
-
apt-cache depends 列出软件包的依赖关系
例如:root@localhost:~# apt-cache depends net-tools net-tools Depends: libc6 Depends: libselinux1
-
--recurse 递归显示所有依赖项,把依赖的依赖也显示出来
例如:消息过多这里只展示一部分
root@localhost:~# apt-cache depends net-tools --recurse | head net-tools Depends: libc6 Depends: libselinux1 libc6 Depends: libgcc-s1 Depends: libcrypt1 Breaks: busybox Breaks: fakeroot Breaks: <hurd> Breaks: ioquake3
-
--no-recommends: 排除推荐依赖;推荐依赖不是软件包运行所必需的,但通常建议安装以获得更好的用户体验。
-
--no-suggests:排除建议依赖;建议依赖是那些APT认为可能对用户有用的软件包,但不是必需的。
内容过多不展示
-
--no-conflicts:不显示与给定软件包冲突的软件包信息。
-
--no-breaks:不显示会与给定软件包产生破坏性更新的软件包信息。
-
--no-replaces:不显示被给定软件包替换的软件包信息。
-
--no-enhances:不显示增强依赖。增强依赖是指那些可以提升软件包功能,但不是必需的软件包
root@localhost:~# apt-cache depends net-tools --recurse --no-recommends --no-suggests --no-conflicts --no-breaks --no-replaces --no-enhances net-tools Depends: libc6 Depends: libselinux1 libc6 Depends: libgcc-s1 Depends: libcrypt1 libselinux1 Depends: libc6 Depends: libpcre2-8-0 libgcc-s1 Depends: gcc-12-base Depends: libc6 libcrypt1 Depends: libc6 libpcre2-8-0 Depends: libc6 gcc-12-base
-
grep “^\w”: 这个管道命令将过滤输出,只保留以单词字符(字母、数字、下划线)开头的行,即忽略空行或以非单词字符开头的行
获得的内容如下:root@localhost:~# apt-cache depends net-tools --recurse --no-recommends --no-suggests --no-conflicts --no-breaks --no-replaces --no-enhances | grep "^\w" net-tools libc6 libselinux1 libgcc-s1 libcrypt1 libpcre2-8-0 gcc-12-base
-
sort -u: 排序后并去重
最后得到所有的依赖包:root@localhost:~# apt-cache depends net-tools --recurse --no-recommends --no-suggests --no-conflicts --no-breaks --no-replaces --no-enhances | grep "^\w" | sort -u gcc-12-base libc6 libcrypt1 libgcc-s1 libpcre2-8-0 libselinux1 net-tools
将所有的依赖包作为参数给到 apt-get download 去下载
root@localhost:/opt/Package# apt-get download $(apt-cache depends net-tools --recurse --no-recommends --no-suggests --no-conflicts --no-breaks --no-replaces --no-enhances | grep "^\w" | sort -u)
Get:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy/main amd64 libcrypt1 amd64 1:4.4.27-1 [82.0 kB]
Get:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy/main amd64 libselinux1 amd64 3.3-1build2 [74.6 kB]
Get:3 http://cn.archive.ubuntu.com/ubuntu jammy/main amd64 net-tools amd64 1.60+git20181103.0eebece-1ubuntu5 [204 kB]
Get:4 http://cn.archive.ubuntu.com/ubuntu jammy-updates/main amd64 gcc-12-base amd64 12.3.0-1ubuntu1~22.04 [20.1 kB]
Get:5 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-updates/main amd64 libc6 amd64 2.35-0ubuntu3.8 [3235 kB]
Get:6 http://cn.archive.ubuntu.com/ubuntu jammy-updates/main amd64 libgcc-s1 amd64 12.3.0-1ubuntu1~22.04 [53.9 kB]
Get:7 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-updates/main amd64 libpcre2-8-0 amd64 10.39-3ubuntu0.1 [221 kB]
Fetched 3891 kB in 2s (1986 kB/s)
W: Download is performed unsandboxed as root as file '/opt/Package/libcrypt1_1%3a4.4.27-1_amd64.deb' couldn't be accessed by user '_apt'. - pkgAcquire::Run (13: Permission denied)
使用dpkg-scanpackages给当前的所有软件包建立依赖关系,生成本地软件包目录,以便在本地使用apt-get 安装
sudo apt-get install dpkg-dev # 提前安装dpkg-dev
创建archives目录将生成的依赖关系打包到archives目录下
一定要写成Packages.gz,不然不识别会报错
如果dpkg-scanpackages命令只是输出waring和info信息不用管,只要不报错就行
root@localhost:/opt/Package# mkdir archives
root@localhost:/opt/Package# dpkg-scanpackages ./ | gzip > ./archives/Packages.gz
dpkg-scanpackages: info: Wrote 7 entries to output Packages file.
root@localhost:/opt/Package# ls archives/
Packages.gz
打包Package整个目录
root@localhost:/opt/Package# tar -zcvf ../Package.tar.gz ../Package
root@localhost:/opt/Package# ls ../
Package Package.tar.gz
将打包好的文件放到需要安装的服务器上解压,这里到放到 /tmp 目录下
root@localhost:/tmp# ls -1
Package.tar.gz
Package/
备份/etc/apt/sources.list文件,并重启编辑它。
root@localhost:/tmp# mv /etc/apt/sources.list /etc/apt/sources.list.bak
root@localhost:/tmp# echo "deb [trusted=yes] file:///tmp/Package/ archives/" > /etc/apt/sources.list
- archives 软件源名称
- /tmp/Package/ 软件包路径
更新软件源
root@localhost:/tmp# apt-get update
Get:1 file:/tmp/Package archives/ InRelease
Ign:1 file:/tmp/Package archives/ InRelease
Get:2 file:/tmp/Package archives/ Release
Ign:2 file:/tmp/Package archives/ Release
Get:3 file:/tmp/Package archives/ Packages
Ign:3 file:/tmp/Package archives/ Packages
Get:4 file:/tmp/Package archives/ Translation-en
Ign:4 file:/tmp/Package archives/ Translation-en
Get:3 file:/tmp/Package archives/ Packages
Ign:3 file:/tmp/Package archives/ Packages
Get:4 file:/tmp/Package archives/ Translation-en
Ign:4 file:/tmp/Package archives/ Translation-en
Get:3 file:/tmp/Package archives/ Packages
Ign:3 file:/tmp/Package archives/ Packages
Get:4 file:/tmp/Package archives/ Translation-en
Ign:4 file:/tmp/Package archives/ Translation-en
Get:3 file:/tmp/Package archives/ Packages [3358 B]
Get:4 file:/tmp/Package archives/ Translation-en
Err:4 file:/tmp/Package archives/ Translation-en
File not found - /tmp/Package/archives/en.gz (2: No such file or directory)
Get:4 file:/tmp/Package archives/ Translation-en
Err:4 file:/tmp/Package archives/ Translation-en
File not found - /tmp/Package/archives/en.lz4 (2: No such file or directory)
Get:4 file:/tmp/Package archives/ Translation-en
Err:4 file:/tmp/Package archives/ Translation-en
File not found - /tmp/Package/archives/en.zst (2: No such file or directory)
Get:4 file:/tmp/Package archives/ Translation-en
Ign:4 file:/tmp/Package archives/ Translation-en
Reading package lists... Done
安装net-tools
root@localhost:/tmp# apt-get install net-tools
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
net-tools
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 0 B/204 kB of archives.
After this operation, 819 kB of additional disk space will be used.
Get:1 file:/tmp/Package archives/ net-tools 1.60+git20181103.0eebece-1ubuntu5 [204 kB]
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package net-tools.
(Reading database ... 64334 files and directories currently installed.)
Preparing to unpack .../net-tools_1.60+git20181103.0eebece-1ubuntu5_amd64.deb ...
Unpacking net-tools (1.60+git20181103.0eebece-1ubuntu5) ...
Setting up net-tools (1.60+git20181103.0eebece-1ubuntu5) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 78.)
debconf: falling back to frontend: Readline
Scanning processes...
Scanning linux images...
Running kernel seems to be up-to-date.
No services need to be restarted.
No containers need to be restarted.
No user sessions are running outdated binaries.
No VM guests are running outdated hypervisor (qemu) binaries on this host.
root@localhost:/tmp# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 929/sshd: zjh@pts/0
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 682/systemd-resolve
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 765/sshd: /usr/sbin
tcp6 0 0 ::1:6010 :::* LISTEN 929/sshd: zjh@pts/0
tcp6 0 0 :::22 :::* LISTEN 765/sshd: /usr/sbin
到这里就安装成功了
同时安装多个软件包测试
还是一样的步骤
root@localhost:/opt/Package# apt-get download $(apt-cache depends vim nginx haproxy --recurse --no-recommends --no-suggests --no-conflicts --no-breaks --no-replaces --no-enhances | grep "^\w" | sort -u)
同时下载 vim nginx haproxy 的软件包以及依赖
生成依赖关系:
root@localhost:/opt/Package# dpkg-scanpackages ./ | gzip > ./archives/Packages.gz
打包整个目录:
root@localhost:/opt/Package# tar -zcvf ../Package.tar.gz ../Package
将压缩包上传到需要下载的服务器上解压
我的路径在/opt/Package
root@localhost:/opt/Package# pwd
/opt/Package
root@localhost:/opt/Package# ls
adduser_3.118ubuntu5_all.deb libgssapi-krb5-2_1.19.2-2ubuntu0.4_amd64.deb libsemanage2_3.3-1build2_amd64.deb
archives libicu70_70.1-2_amd64.deb libsepol2_3.3-1build1_amd64.deb
cdebconf_0.261ubuntu1_amd64.deb libjbig0_2.1-3.1ubuntu0.22.04.1_amd64.deb libslang2_2.3.2-5build4_amd64.deb
coreutils_8.32-4.1ubuntu1.2_amd64.deb libjpeg-turbo8_2.1.2-0ubuntu1_amd64.deb libsodium23_1.0.18-1build2_amd64.deb
debconf_1.5.79ubuntu1_all.deb libjpeg8_8c-2ubuntu10_amd64.deb libsqlite3-0_3.37.2-2ubuntu0.3_amd64.deb
dpkg_1.21.1ubuntu2.3_amd64.deb libk5crypto3_1.19.2-2ubuntu0.4_amd64.deb libssl3_3.0.2-0ubuntu1.18_amd64.deb
fontconfig-config_2.13.1-4.2ubuntu5_all.deb libkeyutils1_1.6.1-2ubuntu3_amd64.deb libstdc++6_12.3.0-1ubuntu1~22.04_amd64.deb
fonts-croscore_20201225-1build1_all.deb libkrb5-3_1.19.2-2ubuntu0.4_amd64.deb libsystemd0_249.11-0ubuntu3.12_amd64.deb
fonts-dejavu-core_2.37-2build1_all.deb libkrb5support0_1.19.2-2ubuntu0.4_amd64.deb libtextwrap1_0.1-15build1_amd64.deb
fonts-freefont-otf_20120503-10build1_all.deb liblua5.3-0_5.3.6-1build1_amd64.deb libtiff5_4.3.0-6ubuntu0.10_amd64.deb
fonts-freefont-ttf_20120503-10build1_all.deb liblz4-1_1.9.3-2build2_amd64.deb libtinfo6_6.3-2ubuntu0.1_amd64.deb
fonts-liberation2_2.1.5-1_all.deb liblzma5_5.2.5-2ubuntu1_amd64.deb libtirpc-common_1.3.2-2ubuntu0.1_all.deb
fonts-liberation_1%3a1.07.4-11_all.deb libmaxminddb0_1.5.2-1build2_amd64.deb libtirpc3_1.3.2-2ubuntu0.1_amd64.deb
fonts-texgyre_20180621-3.1_all.deb libmd0_1.0.4-1build1_amd64.deb libuuid1_2.37.2-4ubuntu3.4_amd64.deb
fonts-urw-base35_20200910-1_all.deb libmnl0_1.0.4-3build2_amd64.deb libwebp7_1.2.2-2ubuntu0.22.04.2_amd64.deb
gcc-12-base_12.3.0-1ubuntu1~22.04_amd64.deb libmpdec3_2.5.1-2build2_amd64.deb libx11-6_2%3a1.7.5-1ubuntu0.3_amd64.deb
haproxy_2.4.24-0ubuntu0.22.04.1_amd64.deb libncursesw6_6.3-2ubuntu0.1_amd64.deb libx11-data_2%3a1.7.5-1ubuntu0.3_all.deb
init-system-helpers_1.62_all.deb libnewt0.52_0.52.21-5ubuntu2_amd64.deb libxau6_1%3a1.0.9-1build5_amd64.deb
install-info_6.8-4build1_amd64.deb libnginx-mod-http-auth-pam_1.18.0-6ubuntu14.5_amd64.deb libxcb1_1.14-3ubuntu3_amd64.deb
iproute2_5.15.0-1ubuntu2_amd64.deb libnginx-mod-http-cache-purge_1.18.0-6ubuntu14.5_amd64.deb libxdmcp6_1%3a1.1.3-0ubuntu5_amd64.deb
libacl1_2.3.1-1_amd64.deb libnginx-mod-http-dav-ext_1.18.0-6ubuntu14.5_amd64.deb libxml2_2.9.13+dfsg-1ubuntu0.4_amd64.deb
libattr1_1%3a2.5.1-1build1_amd64.deb libnginx-mod-http-echo_1.18.0-6ubuntu14.5_amd64.deb libxpm4_1%3a3.5.12-1ubuntu0.22.04.2_amd64.deb
libaudit-common_1%3a3.0.7-1build1_all.deb libnginx-mod-http-fancyindex_1.18.0-6ubuntu14.5_amd64.deb libxslt1.1_1.1.34-4ubuntu0.22.04.1_amd64.deb
libaudit1_1%3a3.0.7-1build1_amd64.deb libnginx-mod-http-geoip2_1.18.0-6ubuntu14.5_amd64.deb libxtables12_1.8.7-1ubuntu5.2_amd64.deb
libbpf0_1%3a0.5.0-1ubuntu22.04.1_amd64.deb libnginx-mod-http-geoip_1.18.0-6ubuntu14.5_amd64.deb libzstd1_1.4.8+dfsg-3build1_amd64.deb
libbrotli1_1.0.9-2build6_amd64.deb libnginx-mod-http-headers-more-filter_1.18.0-6ubuntu14.5_amd64.deb lsb-base_11.1.0ubuntu4_all.deb
libbsd0_0.11.5-1_amd64.deb libnginx-mod-http-image-filter_1.18.0-6ubuntu14.5_amd64.deb mailcap_3.70+nmu1ubuntu1_all.deb
libbz2-1.0_1.0.8-5build1_amd64.deb libnginx-mod-http-perl_1.18.0-6ubuntu14.5_amd64.deb media-types_7.0.0_all.deb
libc6_2.35-0ubuntu3.8_amd64.deb libnginx-mod-http-subs-filter_1.18.0-6ubuntu14.5_amd64.deb mime-support_3.66_all.deb
libcap-ng0_0.7.9-2.2build3_amd64.deb libnginx-mod-http-uploadprogress_1.18.0-6ubuntu14.5_amd64.deb nginx-common_1.18.0-6ubuntu14.5_all.deb
libcap2-bin_1%3a2.44-1ubuntu0.22.04.1_amd64.deb libnginx-mod-http-upstream-fair_1.18.0-6ubuntu14.5_amd64.deb nginx-core_1.18.0-6ubuntu14.5_amd64.deb
libcap2_1%3a2.44-1ubuntu0.22.04.1_amd64.deb libnginx-mod-http-xslt-filter_1.18.0-6ubuntu14.5_amd64.deb nginx-extras_1.18.0-6ubuntu14.5_amd64.deb
libcom-err2_1.46.5-2ubuntu1.2_amd64.deb libnginx-mod-mail_1.18.0-6ubuntu14.5_amd64.deb nginx-full_1.18.0-6ubuntu14.5_amd64.deb
libcrypt1_1%3a4.4.27-1_amd64.deb libnginx-mod-nchan_1.18.0-6ubuntu14.5_amd64.deb nginx-light_1.18.0-6ubuntu14.5_amd64.deb
libdb5.3_5.3.28+dfsg1-0.8ubuntu3_amd64.deb libnginx-mod-stream-geoip2_1.18.0-6ubuntu14.5_amd64.deb nginx_1.18.0-6ubuntu14.5_amd64.deb
libdebian-installer4_0.122ubuntu3_amd64.deb libnginx-mod-stream-geoip_1.18.0-6ubuntu14.5_amd64.deb passwd_1%3a4.8.1-2ubuntu2.2_amd64.deb
libdeflate0_1.10-2_amd64.deb libnginx-mod-stream_1.18.0-6ubuntu14.5_amd64.deb perl-base_5.34.0-3ubuntu1.3_amd64.deb
libelf1_0.186-1build1_amd64.deb libnsl2_1.3.0-2build2_amd64.deb perl-modules-5.34_5.34.0-3ubuntu1.3_all.deb
libexpat1_2.4.7-1ubuntu0.4_amd64.deb libpam-modules-bin_1.4.0-11ubuntu2.4_amd64.deb perl_5.34.0-3ubuntu1.3_amd64.deb
libffi8_3.4.2-4_amd64.deb libpam-modules_1.4.0-11ubuntu2.4_amd64.deb readline-common_8.1.2-1_all.deb
libfontconfig1_2.13.1-4.2ubuntu5_amd64.deb libpam0g_1.4.0-11ubuntu2.4_amd64.deb sensible-utils_0.0.17_all.deb
libfreetype6_2.11.1+dfsg-1ubuntu0.2_amd64.deb libpcre2-8-0_10.39-3ubuntu0.1_amd64.deb tar_1.34+dfsg-1ubuntu0.1.22.04.2_amd64.deb
libgcc-s1_12.3.0-1ubuntu1~22.04_amd64.deb libpcre3_2%3a8.39-13ubuntu0.22.04.1_amd64.deb ttf-bitstream-vera_1.10-8.2_all.deb
libgcrypt20_1.9.4-3ubuntu3_amd64.deb libperl5.34_5.34.0-3ubuntu1.3_amd64.deb ucf_3.0043_all.deb
libgd3_2.3.0-2ubuntu2.3_amd64.deb libpng16-16_1.6.37-3build5_amd64.deb vim-common_2%3a8.2.3995-1ubuntu2.21_all.deb
libgdbm-compat4_1.23-1_amd64.deb libpython3.10-minimal_3.10.12-1~22.04.7_amd64.deb vim-runtime_2%3a8.2.3995-1ubuntu2.21_all.deb
libgdbm6_1.23-1_amd64.deb libpython3.10-stdlib_3.10.12-1~22.04.7_amd64.deb vim_2%3a8.2.3995-1ubuntu2.21_amd64.deb
libgeoip1_1.6.12-8_amd64.deb libpython3.10_3.10.12-1~22.04.7_amd64.deb xxd_2%3a8.2.3995-1ubuntu2.21_amd64.deb
libgmp10_2%3a6.2.1+dfsg-3ubuntu1_amd64.deb libreadline8_8.1.2-1_amd64.deb zlib1g_1%3a1.2.11.dfsg-2ubuntu9.2_amd64.deb
libgpg-error0_1.43-3_amd64.deb libselinux1_3.3-1build2_amd64.deb
libgpm2_1.20.7-10build1_amd64.deb libsemanage-common_3.3-1build2_all.deb
配置本地源:
root@localhost:/opt/Package# echo "deb [trusted=yes] file:///opt/Package archives/" > /etc/apt/sources.list
更新本地源,并安装 vim nginx haproxy软件
root@localhost:/opt/Package# apt-get update
root@localhost:/opt/Package# echo "deb [trusted=yes] file:///opt/Package archives/" > /etc/apt/sources.list
root@localhost:/opt/Package# apt-get install vim nginx haproxy -y
查看已安装的软件
root@localhost:/opt/Package# dpkg -l | grep -E 'nginx|vim|haproxy'
ii haproxy 2.4.24-0ubuntu0.22.04.1 amd64 fast and reliable load balancing reverse proxy
ii libnginx-mod-http-geoip2 1.18.0-6ubuntu14.5 amd64 GeoIP2 HTTP module for Nginx
ii libnginx-mod-http-image-filter 1.18.0-6ubuntu14.5 amd64 HTTP image filter module for Nginx
ii libnginx-mod-http-xslt-filter 1.18.0-6ubuntu14.5 amd64 XSLT Transformation module for Nginx
ii libnginx-mod-mail 1.18.0-6ubuntu14.5 amd64 Mail module for Nginx
ii libnginx-mod-stream 1.18.0-6ubuntu14.5 amd64 Stream module for Nginx
ii libnginx-mod-stream-geoip2 1.18.0-6ubuntu14.5 amd64 GeoIP2 Stream module for Nginx
ii nginx 1.18.0-6ubuntu14.5 amd64 small, powerful, scalable web/proxy server
ii nginx-common 1.18.0-6ubuntu14.5 all small, powerful, scalable web/proxy server - common files
ii nginx-core 1.18.0-6ubuntu14.5 amd64 nginx web/proxy server (standard version)
ii vim 2:8.2.3995-1ubuntu2.21 amd64 Vi IMproved - enhanced vi editor
ii vim-common 2:8.2.3995-1ubuntu2.21 all Vi IMproved - Common files
ii vim-runtime 2:8.2.3995-1ubuntu2.21 all Vi IMproved - Runtime files