ubuntu22.04.5本地apt源部署

很多情况下,内网服务器无法连接互联网,这样如果原始系统只是最基本的下载安装包,因为存在依赖包不全的情况,难以对其进行更新及通过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 multiverse

deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse

deb 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 multiverse

deb 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

shell 复制代码
chmod +x OfflineBuild.sh
./OfflineBuild.sh common

注:common为常用包,也可以在脚本的PACKAGES加入自己需要的包名

shell 复制代码
#!/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
        ;;
esac

downloadlog() {
    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; then
            downloadlog "下载: $pkg"
            apt-get download "$pkg" || downloadlog "警告: $pkg 下载失败"
        else
            downloadlog "已存在: $pkg"
        fi
    done
}
for package in $PACKAGES; do
        download_package_with_deps "$package"
done

downloadlog "下载完成,共下载 $(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基本上如下:

shell 复制代码
#!/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 包名
相关推荐
点点滴滴的记录6 天前
线上常见问题案例及排查工具
运维开发
我的运维人生7 天前
Docker实践与应用举例:构建高效开发与部署环境
docker·容器·eureka·运维开发·技术共享
Linux运维老纪7 天前
python文件夹显示在桌面(Python Folder Displayed on Desktop)
linux·服务器·开发语言·网络·python·云计算·运维开发
wuweihao1234568 天前
DataX与DataX-Web安装与使用
数据仓库·运维开发
软考通8 天前
软考高级难度排名,哪个科目容易过?
网络·职场和发展·系统架构·运维开发·集成学习
凌鲨9 天前
DevOps工程师成长路线图
运维·运维开发·devops
kuankeTech11 天前
解读外贸制单软件:功能全面,操作便捷
大数据·产品运营·运维开发·软件开发·erp
学习编程之路11 天前
【Linux】深入理解进程管理与fork系统调用的实现原理
linux·运维·服务器·运维开发·进程