背景介绍
服务器工作环境为离线局域网环境,几乎无法与公网连通。
常用Ubuntu 系统,并且需要经常安装一些依赖。
搭建本地镜像源势在必行。
工具及现有条件
一台 联想Server 当前OS 为Ubuntu 18.04.2 LTS ,只有一个网卡。
一根外网接入网线。
配置方法
1.将目标server接入公网。
2.配置网络相关信息 /etc/netplan/50-cloud-init.yaml
network:
ethernets:
enp0s31f6:
addresses:
- xxx.xxx.xxx.xxx/xx
gateway4: xxx.xx.xxx.xxx
nameservers:
addresses:
- 114.114.114.114
version: 2
3.配置Ubuntu Server 的镜像源
修改/etc/apt/mirror.list 更新镜像地址
############# config ##################
#
set base_path /home/apt-mirror
#
# set mirror_path $base_path/mirror
# set skel_path $base_path/skel
# set var_path $base_path/var
# set cleanscript $var_path/clean.sh
# set defaultarch <running host architecture>
# set postmirror_script $var_path/postmirror.sh
# set run_postmirror 0
set defaultarch amd64
set nthreads 20
set _tilde 0
#Ubuntu 18.10
deb http://mirrors.aliyun.com/ubuntu/ cosmic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ cosmic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ cosmic-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ cosmic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ cosmic-proposed main restricted universe multiverse
#Ubuntu 18.04
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
clean http://mirrors.aliyun.com/ubuntu
注释:
a.关于Ubuntu 各个版本的名字(Like cosmic and bionic ) 请参考:https://wiki.ubuntu.com/Releases
b.上文以阿里云为例,如有其他镜像源可自行更改
4.创建存放路径
#cd /home;mkdir apt-mirror
5.同步镜像源
apt-mirror
下图仅用于展示正常同步的效果
BTW:
a.18.10和18.04LTS 需要同步180G多的数据,同步的数据会根据配置存放,比如上文配置的/home/apt-mirror路径.
b.同步操作支持断点续传。比如同步过程中想看下同步进度,可以Ctrl+C退出,再重新同步就可以看到剩下需要同步的数据量了
5.安装apache2
为了方便client使用本地镜像源,需要安装apache以进行http调用
apt-get install apache2
Apache2的默认网页文件目录位于/var/www/html,为了方便访问需要做个软链接
ln -s /home/apt-mirror/mirror/mirrors.aliyun.com/ubuntu /var/www/html/ubuntu
6.将本地镜像源切换会局域网IP并接入局域网
Client 调用
配置/etc/apt/sources.list
加入如下配置 xxx.xxx.xxx.xxx 为局域网镜像源的ip
deb http://xxx.xxx.xxx.xxx:80/ubuntu/ bionic main restricted universe multiverse
deb http://xxx.xxx.xxx.xxx:80/ubuntu/ bionic-updates main restricted universe multiverse
deb http://xxx.xxx.xxx.xxx:80/ubuntu/ bionic-backports main restricted universe multiverse
deb http://xxx.xxx.xxx.xxx:80/ubuntu/ bionic-security main restricted universe multiverse
deb http://xxx.xxx.xxx.xxx:80/ubuntu/ bionic-proposed main restricted universe multiverse
如果是18.10 则配置为如下内容:
deb http://xxx.xxx.xxx.xxx:80/ubuntu/ cosmic main multiverse restricted universe
deb http://xxx.xxx.xxx.xxx:80/ubuntu/ cosmic-backports main multiverse restricted universe
deb http://xxx.xxx.xxx.xxx:80/ubuntu/ cosmic-proposed main multiverse restricted universe
deb http://xxx.xxx.xxx.xxx:80/ubuntu/ cosmic-security main multiverse restricted universe
deb http://xxx.xxx.xxx.xxx/ubuntu/ cosmic-updates main multiverse restricted universe