很多情况下,内网服务器无法连接互联网,这样如果原始系统只是最基本的下载安装包,因为存在依赖包不全的情况,难以对其进行更新及通过apt安装包
所以为解决不能联网的问题,首先先通过可以联网的机器制造好源,再通过存储介质传输到无互联网的环境就解决了。
首先,先在为了下载网速快一些可连接互联网的机器配置阿里源
该方法比较灵活,不需要把镜像源所有的镜像都拉下来,只需要设置自己想要的包就可以
可以连接互联网的机器操作
修改/etc/apt/source.list
deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiversedeb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiversedeb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse# deb https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiversedeb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
运行下面的shell
chmod +x OfflineBuild.sh
./OfflineBuild.sh common
注:common为常用包,也可以在脚本的PACKAGES加入自己需要的包名
#!/bin/bash
# 包名称
package_name=$1
# ubuntu版本
ubuntu_version="22.04"# 安装更新软件包
ADDR=$PWD
mkdir -p $ADDR/offline-packages/debs
apt-get update -y
apt-get --download-only -y dist-upgrade
cp /var/cache/apt/archives/*.deb $ADDR/offline-packages/debs
apt install -y apt-rdepends dpkg-dev genisoimage
cd $ADDR/offline-packages/debs# 输入要安装的软件包名称
case "$1" in"docker")PACKAGES="docker.io docker-compose";;"common")PACKAGES="build-essential gcc g++ make cmake gdb net-tools wget curl ssh openssh-server openssh-client htop iftop iotop lsof tcpdump zip unzip tar gzip bzip2 vim nano less tree dos2unix telnet nmap netcat iproute2 git python3 python3-pip subversion docker.io docker-compose fdisk parted lvm2 lightdm linux-header-$(uname -r)";;*)echo "Usage: $0 {docker|common}"exit 1;;
esacdownloadlog() {echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
}
download_package_with_deps() {local package="$1"downloadlog "处理包: $package"apt-rdepends "$package" 2>/dev/null | grep -v "^ " | while read pkg; do# 检查是否已下载(更精确的匹配)if ! ls "${pkg}_"*.deb >/dev/null 2>&1; thendownloadlog "下载: $pkg"apt-get download "$pkg" || downloadlog "警告: $pkg 下载失败"elsedownloadlog "已存在: $pkg"fidone
}
for package in $PACKAGES; dodownload_package_with_deps "$package"
donedownloadlog "下载完成,共下载 $(ls *.deb | wc -l) 个包"REPO_ROOT=$ADDR/local-repo
mkdir -p ${REPO_ROOT}/{conf,dists/stable/main/binary-amd64,pool/main}
cp $ADDR/offline-packages/debs/*.deb ${REPO_ROOT}/pool/main/
cat << EOF > ${REPO_ROOT}/dists/stable/Release
Origin: Local Repository
Label: Local Repo
Suite: stable
Codename: stable
Version: 1.0
Architectures: amd64
Components: main
Description: Local package repository
Date: $(date -R)
EOF
cd ${REPO_ROOT}
dpkg-scanpackages pool/main /dev/null > dists/stable/main/binary-amd64/Packages
gzip -k dists/stable/main/binary-amd64/Packages
genisoimage -o $ADDR/ubuntu_$ubuntu_version-$package_name.iso -R -J ${REPO_ROOT}cat << EOF > $ADDR/OfflineLoad.sh
#!/bin/bash
mkdir /mnt/local-repo
mount -o loop ubuntu_$ubuntu_version-$package_name.iso /mnt/local-repo
mv /etc/apt/sources.list /etc/apt/sources.list.bak
bash -c 'echo "deb [trusted=yes] file:///mnt/local-repo stable main"' > /etc/apt/sources.list
apt-get update -y
EOF
脚本运行后,会生成OfflineLoad.sh及一个iso文件 将其复制到离线设备,运行OfflineLoad.sh
无法连接互联网的机器操作
chmod +x ./OffinelLoad.sh
./OfflineLoad.sh
注:该文件与上步生成的ubuntu_22.04-common.iso
OfflineLoad.sh基本上如下:
#!/bin/bash
mkdir /mnt/local-repo
mount -o loop ubuntu_22.04-common.iso /mnt/local-repo
mv /etc/apt/sources.list /etc/apt/sources.list.bak
bash -c 'echo "deb [trusted=yes] file:///mnt/local-repo stable main"' > /etc/apt/sources.list
apt-get update -y
完全运行后基本上就实现了本地源
安装上面想要的包:
apt install -y 包名