ubuntu搭建APT源簡單方法
一、為什么需要搭建APT源
原因如下:
1、在公司內網離線情況下,ubuntu無法通過apt原生源進行下載
2、有些源國內無法正常訪問,需要FQ
基於以上原因,需要自建APT源
二、准備條件
- 需要能夠上網的電腦,最好能夠登錄github、google等外網電腦
- 需要一台ubuntu的機器,磁盤要足夠大
三、APT環境搭建
3.1、安裝apt-mirror軟件
sudo apt-get install apt-mirror
3.2、修改mirror.list配置文件
sudo mkdir -p /var/www/html
sudo vi /etc/apt/mirror.list
我的配置文件如下:
############# config ##################
# 設置數據存儲目錄,路徑必須提前存在
# set base_path /var/spool/apt-mirror
set base_path /var/www/html
# 設置鏡像存儲位置
# set mirror_path $base_path/mirror
# 設置臨時下載索引位置
# set skel_path $base_path/skel
# 設置日志,URLs和MD5校驗信息存儲位置
# set var_path $base_path/var
# 配置刪除過期源腳本位置(默認不刪除,方便安裝舊版本軟件)
# set cleanscript $var_path/clean.sh
# 設置默認架構,i386/amd64,默認下載與本機相同的架構
# set defaultarch <running host architecture>
set defaultarch amd64
# 設定下載后運行的腳本位置
# set postmirror_script $var_path/postmirror.sh
# 設置是否執行 下載后的腳本操作,默認是1(但是默認沒有postmirror.sh腳本)
# set run_postmirror 0
# 設置下載線程數
set nthreads 20
# 是否替換URL中的波浪線,替換成%7E(HTML代碼),否則會跳過不進行下載
set _tilde 0
#
############# end config ##############
# ubuntu的APT軟件源
#deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
#deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
#deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
#deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
#deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
#deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
#deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
#deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
#deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
#deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
#clean http://mirrors.ustc.edu.cn ubuntu
# nvidia-docker2 源
#deb https://nvidia.github.io/libnvidia-container/stable/ubuntu18.04/$(ARCH) /
#直接使用nvidia-docker2 源地址會有問題,需要修改如下才可以
deb https://nvidia.github.io/libnvidia-container/stable/ubuntu18.04/amd64 /
#deb https://nvidia.github.io/libnvidia-container/experimental/ubuntu18.04/$(ARCH) /
deb https://nvidia.github.io/nvidia-container-runtime/stable/ubuntu18.04/amd64 /
#deb https://nvidia.github.io/nvidia-container-runtime/experimental/ubuntu18.04/$(ARCH) /
deb https://nvidia.github.io/nvidia-docker/ubuntu18.04/amd64 /
# docker-ce源
deb https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic stable
# ros源
deb http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/ bionic main
# vscode源
#deb [arch=amd64,arm64,armhf] http://packages.microsoft.com/repos/code stable main
# bazel源
deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8
clean https://nvidia.github.io ubuntu
clean https://mirrors.tuna.tsinghua.edu.cn ubuntu
clean http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu
clean http://packages.microsoft.com/repos/code
3.3、開始下載鏡像軟件
sudo apt-miiror
下載鏡像過程時間比較長,需要耐心等待。執行過程如下:
root@HANZHIWEI02:~# apt-mirror
Downloading 57 index files using 20 threads...
Begin time: Fri Jan 28 14:20:18 2022
[20]... [19]... [18]... [17]... [16]... [15]... [14]... [13]... [12]... [11]... [10]... [9]... [8]... [7]... [6]... [5]... [4]... [3]... [2]... [1]... [0]...
End time: Fri Jan 28 14:20:29 2022Processing translation indexes: [TTTTTT]
Downloading 0 translation files using 0 threads...
Begin time: Fri Jan 28 14:20:29 2022
[0]...
End time: Fri Jan 28 14:20:29 2022Processing DEP-11 indexes: [DDDDDD]
Downloading 0 dep11 files using 0 threads...
Begin time: Fri Jan 28 14:20:29 2022
[0]...
End time: Fri Jan 28 14:20:29 2022Processing indexes: [PPPPPP]
2.2 GiB will be downloaded into archive.
Downloading 707 archive files using 20 threads...
Begin time: Fri Jan 28 14:20:30 2022
[20]... [19]... [18]... [17]... [16]... [15]... [14]... [13]... [12]... [11]... [10]... [9]... [8]... [7]... [6]... [5]... [4]... [3]... [2]... [1]... [0]...
End time: Fri Jan 28 14:31:46 20224.1 GiB in 1406 files and 2 directories can be freed.
Run /mnt/e/ubuntu-apt/var/clean.sh for this purpose.Running the Post Mirror script ...
(/var/www/html/var/postmirror.sh)Post Mirror script has completed. See above output for any possible errors.
3.4、安裝nginx
sudo apt-get install nginx
3.5、配置nginx的服務
sudo vi /etc/nginx/sites-available/default
我的配置文件如下:
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 80 default_server;
# 設置中文編碼支持
charset utf-8;
# listen [::]:80 default_server;
# SSL configuration
#
#listen 443 ssl default_server;
#listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
#ssl_certificate /mnt/e/ubuntu-apt/key/server.crt;
#ssl_certificate_key /mnt/e/ubuntu-apt/key/server.key;
#ssl_session_timeout 5m;
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL;
#ssl_prefer_server_ciphers on;
#ssl_ciphers HIGH:!aNULL:!MD5;
# 顯示目錄
autoindex on;
# 鏡像所在的目錄
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
# server_name _;
server_name <本機ip地址>;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
#autoindex on;
#autoindex_exact_size off;
#autoindex_localtime on;
}
# pass PHP scripts to FastCGI server
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
3.6、重啟 nginx 服務
sudo service nginx restart
在瀏覽器打開http://IP,能夠考到以下頁面,這表示搭建成功
四、修改內網APT源
經過第三章內容, 已經搭建好了簡易的APT鏡像源,現在只需要將內網電腦上的APT源修改為自己搭建的源即可
修改 /etc/apt/sources.list
文件
sudo vi /etc/apt/sources.list
將下面內容:
deb https://repo.t3caic.com/repository/apt-aliyun/ bionic main restricted universe multiverse
deb https://repo.t3caic.com/repository/apt-aliyun/ bionic-security main restricted universe multiverse
deb https://repo.t3caic.com/repository/apt-aliyun/ bionic-updates main restricted universe multiverse
deb https://repo.t3caic.com/repository/apt-aliyun/ bionic-proposed main restricted universe multiverse
deb https://repo.t3caic.com/repository/apt-aliyun/ bionic-backports main restricted universe multiverse# docker
#deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic stable# nvidia-docker2
deb https://nvidia.github.io/libnvidia-container/stable/ubuntu18.04/$(ARCH) /
deb https://nvidia.github.io/libnvidia-container/experimental/ubuntu18.04/$(ARCH) /
deb https://nvidia.github.io/nvidia-container-runtime/stable/ubuntu18.04/$(ARCH) /
deb https://nvidia.github.io/nvidia-container-runtime/experimental/ubuntu18.04/$(ARCH) /
deb https://nvidia.github.io/nvidia-docker/ubuntu18.04/$(ARCH) /# ros
deb https://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/ bionic main# vscode
deb [arch=amd64,arm64,armhf] https://packages.microsoft.com/repos/code stable main# bazel
deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8
替換為以下內容即可:
- 在url中添加自己的ip地址
https
需要替換為http
,因為搭建的nginx服務沒有啟用ssl驗證
deb https://repo.t3caic.com/repository/apt-aliyun/ bionic main restricted universe multiverse
deb https://repo.t3caic.com/repository/apt-aliyun/ bionic-security main restricted universe multiverse
deb https://repo.t3caic.com/repository/apt-aliyun/ bionic-updates main restricted universe multiverse
deb https://repo.t3caic.com/repository/apt-aliyun/ bionic-proposed main restricted universe multiverse
deb https://repo.t3caic.com/repository/apt-aliyun/ bionic-backports main restricted universe multiverse# docker
#deb [arch=amd64] http://<自己搭建的ip地址>/mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu bionic stable# nvidia-docker2
deb http://<自己搭建的ip地址>/nvidia.github.io/libnvidia-container/stable/ubuntu18.04/$(ARCH) /
deb http://<自己搭建的ip地址>/nvidia.github.io/libnvidia-container/experimental/ubuntu18.04/$(ARCH) /
deb http://<自己搭建的ip地址>/nvidia.github.io/nvidia-container-runtime/stable/ubuntu18.04/$(ARCH) /
deb http://<自己搭建的ip地址>/nvidia.github.io/nvidia-container-runtime/experimental/ubuntu18.04/$(ARCH) /
deb http://<自己搭建的ip地址>/nvidia.github.io/nvidia-docker/ubuntu18.04/$(ARCH) /# ros
deb http://<自己搭建的ip地址>/mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/ bionic main# vscode
deb [arch=amd64,arm64,armhf] http://<自己搭建的ip地址>/packages.microsoft.com/repos/code stable main# bazel
deb [arch=amd64] http://<自己搭建的ip地址>/storage.googleapis.com/bazel-apt stable jdk1.8
經過以上步驟,被隔離在內網的電腦能夠通過可以訪問外網服務電腦來安裝所需的apt。
五、rosdep離線源的搭建部署
5.1、解決rosdep init問題
在內網離線執行rosdep init的時候有可能會遇到以下問題:
hanzhiwei@T4-046:~$ sudo rosdep init
ERROR: cannot download default sources list from:
https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/sources.list.d/20-default.list
Website may be down.
解決方案一:
-
將
20-default.list
下載下來 -
將
20-default.list
文件放到/etc/ros/rosdep/sources.list.d
目錄即可,如果該目錄不存在可以新建
解決方案二:
-
將
20-default.list
下載下來 -
按照apt源的方式,放到nginx的
/var/www/html/raw.githubusercontent.com/ros/rosdistro/master/rosdep/sources.list.d
目錄下 -
修改
/usr/lib/python2.7/dist-packages/rosdep2/sources_list.py
文件,將:DEFAULT_SOURCES_LIST_URL = 'https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/sources.list.d/20-default.list'
修改為:
DEFAULT_SOURCES_LIST_URL = 'http://<自己搭建的ip地址>/raw.githubusercontent.com/ros/rosdistro/master/rosdep/sources.list.d/20-default.list'
即可
5.2、解決rosdep update問題
在內網環境新執行rosdep update匯報以下問題:
hanzhiwei@T4-046:~$ rosdep update
reading in sources list data from /etc/ros/rosdep/sources.list.d
ERROR: unable to process source [https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/osx-homebrew.yaml]:
<urlopen error [Errno 0] Error> (https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/osx-homebrew.yaml)
ERROR: unable to process source [https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/base.yaml]:
<urlopen error [Errno 0] Error> (https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/base.yaml)
ERROR: unable to process source [https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/python.yaml]:
<urlopen error [Errno 0] Error> (https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/python.yaml)
ERROR: unable to process source [https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/ruby.yaml]:
<urlopen error [Errno 0] Error> (https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/ruby.yaml)
解決方法如下:
-
將以下內容下載下來,按照APT源搭建方式,放到nginx
/var/www/html
下的對應目錄下https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/osx-homebrew.yaml
https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/base.yaml
https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/python.yaml
https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/ruby.yaml
https://raw.githubusercontent.com/ros/rosdistro/master/releases/fuerte.yaml
https://raw.githubusercontent.com/ros/rosdistro/master/index-v4.yaml
https://raw.github.com/ros/rosdistro/master/releases/targets.yaml
-
修改
20-default.list
文件內容sudo vi /etc/ros/rosdep/sources.list.d/20-default.list
將以下內容:
# os-specific listings first
yaml https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/osx-homebrew.yaml osx# generic
yaml https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/base.yaml
yaml https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/python.yaml
yaml https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/ruby.yaml
gbpdistro https://raw.githubusercontent.com/ros/rosdistro/master/releases/fuerte.yaml fuerte# newer distributions (Groovy, Hydro, ...) must not be listed anymore, they are being fetched from the rosdistro index.yaml instead
修改為:
# os-specific listings first
yaml http://<自己搭建的ip地址>/raw.githubusercontent.com/ros/rosdistro/master/rosdep/osx-homebrew.yaml osx# generic
yaml http://<自己搭建的ip地址>/raw.githubusercontent.com/ros/rosdistro/master/rosdep/base.yaml
yaml http://<自己搭建的ip地址>/raw.githubusercontent.com/ros/rosdistro/master/rosdep/python.yaml
yaml http://<自己搭建的ip地址>/raw.githubusercontent.com/ros/rosdistro/master/rosdep/ruby.yaml
gbpdistro http://<自己搭建的ip地址>/raw.githubusercontent.com/ros/rosdistro/master/releases/fuerte.yaml fuerte# newer distributions (Groovy, Hydro, ...) must not be listed anymore, they are being fetched from the rosdistro index.yaml instead
-
修改
rep3.py
文件sudo vi /usr/lib/python2.7/dist-packages/rosdep2/rep3.py
將以下內容:
REP3_TARGETS_URL = 'https://raw.github.com/ros/rosdistro/master/releases/targets.yaml'
修改為:
REP3_TARGETS_URL = 'http://<自己搭建的ip地址>/raw.github.com/ros/rosdistro/master/releases/targets.yaml'
-
修改
__init__.py
文件sudo vi /usr/lib/python2.7/dist-packages/rosdistro/__init__.py
將以下內容:
DEFAULT_INDEX_URL = 'https://raw.githubusercontent.com/ros/rosdistro/master/index-v4.yaml'
修改為:
DEFAULT_INDEX_URL = 'http://<自己搭建的ip地址>/raw.githubusercontent.com/ros/rosdistro/master/index-v4.yaml'
-
已上修改好后,如果直接執行
rosdep update
,會發現以下錯誤root@T4-046:~# rosdep update
reading in sources list data from /etc/ros/rosdep/sources.list.d
Warning: running 'rosdep update' as root is not recommended.
You should run 'sudo rosdep fix-permissions' and invoke 'rosdep update' again without sudo.
Hit http://<自己搭建的ip地址>/raw.githubusercontent.com/ros/rosdistro/master/rosdep/osx-homebrew.yaml
Hit http://<自己搭建的ip地址>/raw.githubusercontent.com/ros/rosdistro/master/rosdep/base.yaml
Hit http://<自己搭建的ip地址>/raw.githubusercontent.com/ros/rosdistro/master/rosdep/python.yaml
Hit http://<自己搭建的ip地址>/raw.githubusercontent.com/ros/rosdistro/master/rosdep/ruby.yaml
Hit http://<自己搭建的ip地址>/raw.githubusercontent.com/ros/rosdistro/master/releases/fuerte.yaml
Query rosdistro index http://<自己搭建的ip地址>/raw.githubusercontent.com/ros/rosdistro/master/index-v4.yaml
Add distro "ardent"
ERROR: error loading sources list:
HTTP Error 404: Not Found (http://<自己搭建的ip地址>/raw.githubusercontent.com/ros/rosdistro/master/ardent/distribution.yaml) -
這是因為在
index-v4.yaml
文件中還有部分文件需要進行下載,根據錯誤提示和對index-v4.yaml
文件進行內容解讀,發現還需要下載以下文件,並放到nginx/var/www/html
下的對應目錄下https://raw.githubusercontent.com/ros/rosdistro/master/ardent/distribution.yaml
https://raw.githubusercontent.com/ros/rosdistro/master/bouncy/distribution.yaml
https://raw.githubusercontent.com/ros/rosdistro/master/crystal/distribution.yaml
https://raw.githubusercontent.com/ros/rosdistro/master/dashing/distribution.yaml
https://raw.githubusercontent.com/ros/rosdistro/master/eloquent/distribution.yaml
https://raw.githubusercontent.com/ros/rosdistro/master/foxy/distribution.yaml
https://raw.githubusercontent.com/ros/rosdistro/master/galactic/distribution.yaml
https://raw.githubusercontent.com/ros/rosdistro/master/groovy/distribution.yaml
https://raw.githubusercontent.com/ros/rosdistro/master/hydro/distribution.yaml
https://raw.githubusercontent.com/ros/rosdistro/master/indigo/distribution.yaml
https://raw.githubusercontent.com/ros/rosdistro/master/jade/distribution.yaml
https://raw.githubusercontent.com/ros/rosdistro/master/kinetic/distribution.yaml
https://raw.githubusercontent.com/ros/rosdistro/master/lunar/distribution.yaml
https://raw.githubusercontent.com/ros/rosdistro/master/melodic/distribution.yaml
https://raw.githubusercontent.com/ros/rosdistro/master/noetic/distribution.yaml
https://raw.githubusercontent.com/ros/rosdistro/master/rolling/distribution.yaml -
以上文件下載好后,再執行
rosdep update
即可完成相關操作,執行過程如下root@T4-046:~# rosdep update
reading in sources list data from /etc/ros/rosdep/sources.list.d
Warning: running 'rosdep update' as root is not recommended.
You should run 'sudo rosdep fix-permissions' and invoke 'rosdep update' again without sudo.
Hit http://<自己搭建的ip地址>/raw.githubusercontent.com/ros/rosdistro/master/rosdep/osx-homebrew.yaml
Hit http://<自己搭建的ip地址>/raw.githubusercontent.com/ros/rosdistro/master/rosdep/base.yaml
Hit http://<自己搭建的ip地址>/raw.githubusercontent.com/ros/rosdistro/master/rosdep/python.yaml
Hit http://<自己搭建的ip地址>/raw.githubusercontent.com/ros/rosdistro/master/rosdep/ruby.yaml
Hit http://<自己搭建的ip地址>/raw.githubusercontent.com/ros/rosdistro/master/releases/fuerte.yaml
Query rosdistro index http://<自己搭建的ip地址>/raw.githubusercontent.com/ros/rosdistro/master/index-v4.yaml
Add distro "ardent"
Add distro "bouncy"
Add distro "crystal"
Add distro "dashing"
Add distro "eloquent"
Add distro "foxy"
Add distro "galactic"
Add distro "groovy"
Add distro "hydro"
Add distro "indigo"
Add distro "jade"
Add distro "kinetic"
Add distro "lunar"
Add distro "melodic"
Add distro "noetic"
Add distro "rolling"
updated cache in /root/.ros/rosdep/sources.cache
5.3、nginx下的目錄結構
下載對應文件,存放在nginx目錄結構如下,僅供參考:
└── var
└── www
└── html
├── raw.github.com
│ └── ros
│ └── rosdistro
│ └── master
│ └── releases
│ └── targets.yaml
└── raw.githubusercontent.com
└── ros
└── rosdistro
└── master
├── ardent
│ └── distribution.yaml
├── bouncy
│ └── distribution.yaml
├── crystal
│ └── distribution.yaml
├── dashing
│ └── distribution.yaml
├── eloquent
│ └── distribution.yaml
├── foxy
│ └── distribution.yaml
├── galactic
│ └── distribution.yaml
├── groovy
│ └── distribution.yaml
├── hydro
│ └── distribution.yaml
├── index-v4.yaml
├── indigo
│ └── distribution.yaml
├── jade
│ └── distribution.yaml
├── kinetic
│ └── distribution.yaml
├── lunar
│ └── distribution.yaml
├── melodic
│ └── distribution.yaml
├── noetic
│ └── distribution.yaml
├── releases
│ └── fuerte.yaml
├── rolling
│ └── distribution.yaml
└── rosdep
├── base.yaml
├── osx-homebrew.yaml
├── python.yaml
├── ruby.yaml
└── sources.list.d
└── 20-default.list
參考文檔
http://t.zoukankan.com/superbi-p-13409529.html
https://www.cnblogs.com/zhjblogs/p/14875296.html