Ubuntu作为最优秀的Linux发行版之一,是初学者入门的不二选择,但Linux有个最大的问题,就是离了网络就废了。
在Windows系统中,安装软件十分方便,下载安装程序,直接setup就可以了。
其实Linux安装软件也非常方便,前提是在联网的情况下,但要是离线安装,就无法享用apt的优势了,而直接使用dpkg安装,各种软件的依赖关系绝对让你头疼。
在某些对安全要求比较严的地方,是没有上网环境的,如果我们要在不能联网的Ubuntu上安装特定的软件包(比如nginx),怎么办?
类比CentOS,聪明如果你,一定想到了将指定的软件包及其依赖先在有网的机器上下载下来,然后拷贝到不能联网的机器,搭建本地仓库(源),执行安装。
那么问题来了:
1. 怎么在有网的机器上将一个特定软件包及其依赖一次性下载下来?
2. 怎么在不能联网的机器,搭建本地仓库(源)?
操作系统: Ubuntu16.04
serverA 192.168.1.101 能联网
serverB 192.168.1.102 不能联网
1. 添加nginx官方仓库
Official Debian/Ubuntu packages
https://www.nginx.com/resources/wiki/start/topics/tutorials/install
# vim /etc/apt/sources.list.d/nginx.list
##############################################
deb http://nginx.org/packages/ubuntu/ xenial nginx
deb-src http://nginx.org/packages/ubuntu/ xenial nginx
##############################################
# apt-get update
# apt-key adv --keyserver keyserver.ubuntu.com --recv-keys ABF5BD827BD9BF62
添加仓库(源)信任的另一种方式
# vim /etc/apt/sources.list.d/nginx.list
##########################################################
deb [trusted=yes] http://nginx.org/packages/ubuntu/ xenial nginx
deb-src [trusted=yes] http://nginx.org/packages/ubuntu/ xenial nginx
###########################################################
# apt-get update
列出添加的nginx源中软件包
# ll /var/lib/apt/lists/*nginx*
# grep -E "Package:|Version:" Package /var/lib/apt/lists/nginx.org_packages_ubuntu_dists_xenial_nginx_binary-amd64_Packages
# grep -E "Package:|Version:" Package /var/lib/apt/lists/nginx.org_packages_ubuntu_dists_xenial_nginx_binary-amd64_Packages | awk '{print $2}'
# apt-cache madison nginx
# apt-cache policy nginx
2. 下载nginx主包及其依赖
# mkdir -p /root/nginxDeps
# cd /root/ngixDeps
# apt-cache depends --recurse --no-recommends --no-suggests --no-conflicts --no-breaks --no-replaces --no-enhances nginx=1.16.0-1~xenial | grep -v i386| grep "^\w" | sort -u
# apt-get download $(apt-cache depends --recurse --no-recommends --no-suggests --no-conflicts --no-breaks --no-replaces --no-enhances nginx=1.16.0-1~xenial | grep -v i386| grep "^\w" | sort -u)
# dpkg-scanpackages . | gzip -9c > Packages.gz
3.将/root/nginxDeps目录打包,拷贝到serverB离线机器
# tar -zcf nginxDeps.tar.gz nginxDeps
# tar -zxf nginxDeps.tar.gz -C /tmp
注意:不能解压在/root 目录下,否则会报没有读取权限!
# cp /etc/apt/sources.list /etc/apt/sources.list.bak
# echo > /etc/apt/sources.list
# vi /etc/apt/sources.list
#########################################
deb [trusted=yes] file:///tmp/nginxDeps/ ./
#########################################
# apt-get clean all
# apt-get update
# apt -y install nginx
还原仓库文件
# cp /etc/apt/sources.list.bak /etc/apt/sources.list
# rm -rf /tmp/nginxDeps
如上,我们实现了nginx在Ubuntu上的离线安装,其他软件的安装方法类似,触类旁通,举一反三。
Get Docker CE for Ubuntu
https://docs.docker.com/install/linux/docker-ce/ubuntu
How To Build Local APT Repositories?
https://www.centos.bz/2017/07/apt-mirror-deploy-ubuntu-local-repositry
https://odzangba.wordpress.com/2006/10/13/how-to-build-local-apt-repositories
How To Install Softwares Offline In Ubuntu 16.04
https://www.ostechnix.com/install-softwares-offline-ubuntu-16-04
https://www.jb51.net/article/99570.htm
Is there an apt command to download a deb file from the repositories to the current directory?
https://askubuntu.com/questions/30482/is-there-an-apt-command-to-download-a-deb-file-from-the-repositories-to-the-curr
How to list/download the recursive dependencies of a debian package?
https://stackoverflow.com/questions/22008193/how-to-list-download-the-recursive-dependencies-of-a-debian-package
How to download all dependencies and packages to directory?
https://stackoverflow.com/questions/13756800/how-to-download-all-dependencies-and-packages-to-directory
How To Create A Local Debian/Ubuntu Mirror With apt-mirror?
https://www.howtoforge.com/local_debian_ubuntu_mirror
How do I download a package from apt-get without installing it?
https://stackoverflow.com/questions/4419268/how-do-i-download-a-package-from-apt-get-without-installing-it
How to Force update from unsigned repository Ubuntu 16.04 ?
https://askubuntu.com/questions/732985/force-update-from-unsigned-repository-ubuntu-16-04
How to download .deb package and all dependencies?
https://superuser.com/questions/876727/how-to-download-deb-package-and-all-dependencies
Ubuntu 16.04: Download deb package and all package dependencies
https://www.hiroom2.com/2016/08/10/ubuntu-16-04-download-deb-package-and-all-package-dependencies
How to list all packages from a repository in ubuntu / debian?
https://serverfault.com/questions/252333/list-all-packages-from-a-repository-in-ubuntu-debian
https://www.linuxuprising.com/2018/11/how-to-list-all-packages-in-repository.html
https://askubuntu.com/questions/220478/how-to-get-list-of-installable-packages-from-repositories
THE DEB AND DEB-SRC TYPES: GENERAL FORMAT
https://manpages.debian.org/testing/apt/sources.list.5.en.html