一、介紹
1、背景
最近在工作中有遇到這樣的情況:某台機器搭建環境的時候,需要安裝第三方的軟件,但是這台機器是內網的,內網源沒有這個軟件包,
那么怎么安裝呢?
2、准備工作
這個時候就想到了一種方法,先去一台能聯網的機器,最好是一台新的機器,這是為了避免我們打包的時候把之前安裝的其他三方包也打包進來了,
雖然沒有什么影響,但是打包太大,上傳比較麻煩。
假如我要下載的三方包是 chromium ,國內的機器可以更改ubuntu的更新源為阿里雲。
用你熟悉的編輯器打開: vim /etc/apt/sources.list 替換默認的 http://archive.ubuntu.com/ 為 mirrors.aliyun.com # ubuntu 16.04 配置如下 deb http://mirrors.aliyun.com/ubuntu/ xenial main deb-src http://mirrors.aliyun.com/ubuntu/ xenial main deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main deb http://mirrors.aliyun.com/ubuntu/ xenial universe deb-src http://mirrors.aliyun.com/ubuntu/ xenial universe deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates universe deb http://mirrors.aliyun.com/ubuntu/ xenial-security main deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security universe # 然后更新源,執行命令 apt-get update
然后在這台新機器上,apt-get install chromium-browser
下載chromium-browser
執行完上述指令后,XXXX軟件的安裝包就下載到了/var/cache/apt/archives目錄下
二、開始制作離線包
1、生成依賴關系並打包
# 在新機器(已經下載了chromium-browser的機器下操作) 1.新建一個文件夾 在項目根目錄新建文件夾offlinePackage mkdir /offlinePackage 2.拷貝下載的deb包 將下載的deb包拷貝到上述新建的文件夾下 cp -r /var/cache/apt/archives /offlinePackage 3.修改文件夾的權限,可讀可寫可執行 sudo chmod 777 -R /offlinePackage/ 4.建立deb包的依賴關系 sudo dpkg-scanpackages /offlinePackage/ /dev/null |gzip >/offlinePackage/Packages.gz 如果出現錯誤:sudo: dpkg-scanpackages: command not found 則需要安裝dpkg-dev工具 apt-get install dpkg-dev 5.打包成壓縮包 tar -zcvf offlinePackage.tar.gz /offlinePackage/ 保存offlinePackage.tar.gz文件到本地或者其他能上傳到公司內網的服務器
2、其他不能聯網的機器安裝
# 在另外一台Ubuntu上離線安裝 1.拷貝文件到根目錄 將剛才制作的offlinePackage.tar.gz復制到根目錄下,解壓 tar -zxvf offlinePackage.tar.gz / 2.先備份下原本的源,避免炸了,哈哈 cp /etc/apt/sources.list /etc/apt/sources.list.back 3.更新系統源 # 將安裝包所在和源路徑添加到系統源source.list vim /etc/apt/sources.list # 內容如下 deb file:// /offlinePackage/ # 更新 sudo apt-get update 4.結果 W: The repository 'file: offlinePackage/ Release' does not have a Release file. N: Data from such a repository can't be authenticated and is therefore potentially dangerous to use. N: See apt-secure(8) manpage for repository creation and user configuration details. 這個是正常的,已經更新成功了,提示我們這是不安全的更新源 5.離線安裝 這時我們就可以下載我們剛才打包好的軟件了, 因為剛才更新源的時候已經提示不安全了,所以安裝軟件時,加上這個參數:--allow-unauthenticated apt-get -y install chromium-browser --allow-unauthenticated
三、將自己的deb項目添加到私有倉庫,使用apt方式管理
我還看到了別人寫的一篇好像挺不錯的方法,這里記錄下來:
參考文章1:https://www.jianshu.com/p/ee870d63c175
參考文章2:https://www.cnblogs.com/xiao987334176/p/9875480.html
上線使用的是file方式,只能本機使用。那么其他服務器要使用,就不行了!
這個時候,需要使用http方式。可以讓局域網的其他服務器使用!
1、安裝nginx
sudo apt-get install -y nginx
2、搭建項目索引頁
這里不使用域名,直接訪問IP地址作為主頁!
注釋掉nginx的默認首頁
sudo vim /etc/nginx/nginx.conf
找到以下內容,將sites-enabled注釋掉
include /etc/nginx/conf.d/*.conf; #include /etc/nginx/sites-enabled/*;
進入目錄conf.d,新建文件deb.conf
vim /etc/nginx/conf.d/deb.conf
內容如下:
server { listen 80; server_name localhost; root /offlinePackage; location / { autoindex on; } }
檢查配置文件是否正確
sudo nginx -t
如果出現以下提示,表示ok
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
加載配置
nginx -s reload
3、訪問索引頁
訪問url: http://192.168.91.128/ ,效果如下:
4、更新ubuntu數據庫
編輯配置文件
sudo vim /etc/apt/sources.list
最后一行增加
deb http://192.168.91.128 /
注意:保證有空格,否則會提示格式錯誤。
最后一個是斜杠
使用apt-get update來更新一下
sudo apt-get update
之后,就可以安裝軟件了!
務必注意:使用apt-get install -y 軟件名,后面一定要帶--allow-unauthenticated,因為它是私有的,還沒有簽名!