当前位置: 首页>>技术教程>>正文


如何创建本地APT存储库?

, ,

问题描述

我想在我的LAN上构建自己的本地存储库,以便局域网上的计算机可以更新并从中升级。我想下载这些软件包并将它们存储在本地服务器上,以便我可以在不使用互联网的情况下更新,升级,安装等。

最佳解决思路

Ubuntu Help wiki

There are 4 steps to setting up a simple repository for yourself

1.Install dpkg-dev
2.Put the packages in a directory
3.Create a script that will scan the packages and create a file apt-get update can read
4. Add a line to your sources.list pointing at your repository

Install dpkg-dev

Type in a terminal

sudo apt-get install dpkg-dev 

The Directory

Create a directory where you will keep your packages. For this example, we’ll use /usr/local/mydebs.

sudo mkdir -p /usr/local/mydebs 

Now move your packages into the directory you’ve just created.

Previously downloaded Packages are generally stored on your system in the /var/cache/apt/archives directory. If you have installed apt-cacher you will have additional packages stored in its /packages directory.

The Script update-mydebs

It’s a simple three liner:

#! /bin/bash  cd /usr/local/mydebs  dpkg-scanpackages . /dev/null | gzip -9c > Packages.gz 

Cut and paste the above into gedit, and save it as update-mydebs in ~/bin. (the tilde ‘~’ means your home directory. If ~/bin does not exist, create it: Ubuntu will put that directory in your PATH. It’s a good place to put personal scripts). Next, make the script executable:

chmod u+x ~/bin/update-mydebs  How the script works: 

dpkg-scanpackages looks at all the packages in mydebs, and the output is compressed and written to a file (Packages.gz) that apt-get update can read (see below for a reference that explains this in excruciating detail). /dev/null is an empty file; it is a substitute for an override file which holds some additional information about the packages, which in this case is not really needed. See deb-override(5) if you want to know about it.

Sources.list

add the line

deb file:/usr/local/mydebs ./ 

to your /etc/apt/sources.list, and you’re done.

CD Option

You can burn the directory containing the debs to a CD and use that as a repository as well (good for sharing between computers). To use the CD as a repository, simply run

sudo apt-cdrom add 

Using the Repository

Whenever you put a new deb in the mydebs directory, run

sudo update-mydebs sudo apt-get update 

Now your local packages can be manipulated with Synaptic, aptitude and the apt commands: apt-get, apt-cache, etc. When you attempt to apt-get install, any dependencies will be resolved for you, as long as they can be met.

Badly made packages will probably fail, but you won’t have endured dpkg hell.

次佳解决思路

*通过局域网建立离线版本库*安装本地Apache Web服务器

# apt-get install apache2

默认情况下,Debian的Apache包将在您的系统上的/var/www下建立一个网站。就我们的目的而言,没关系,所以没有理由再做任何事情。您可以通过将您最喜欢的浏览器指向http://localhost来轻松测试它您应该看到实际存储在/var/www/index.html中的默认post-installation网页创建Debian包存储库目录选择为此创建一个目录/var/www/debs。在它下面,你应该创建”architecture”目录,一个用于你需要支持的每个体系结构。如果您仅使用一台计算机(或计算机类型),则只需要一台计算机(通常为32位系统的”i386″或64位的”amd64″)。如果您使用其他架构,我会假设您可能已经知道这一点。现在只需将给定架构的”.deb”包文件复制到适当的目录中即可。如果您现在将您喜爱的Web浏览器指向http://localhost/debs/amd64(例如),您将看到64位系统的软件包列表。创建一个Packages.gz文件现在我们需要创建一个供APT使用的目录文件。这是通过一个名为”dpkg-scanpackages”的实用程序完成的。以下是我用来更新LAN上的AMD64软件包的命令:

# cd /var/www/debs/

# dpkg-scanpackages amd64 | gzip -9c > amd64/Packages.gz

使存储库知道APT现在唯一要做的就是让APT知道您的存储库。你可以通过更新你的/etc/apt/sources.list文件来做到这一点。你需要一个这样的条目:

deb http://localhost/debs/ amd64/

我使用了我的系统的实际主机名而不是本地主机 – 这样,我的局域网上的所有计算机的代码都是相同的,但是如果只运行一台计算机,localhost将会很好。现在,更新APT:

# apt-get update

第三种解决思路

创建一个经过身份验证的存储库

我已经看过这里和其他网站的答案,并且大多数人都有(恕我直言)大的缺点,即您正在设置未经认证的存储库。这意味着您需要使用--allow-unauthenticated运行apt-get以从中安装软件包。这可能会带来安全风险,特别是在您安装的软件包可能并非都来自本地存储库的脚本中。

请注意,我没有在这里介绍如何通过局域网提供它,但这是使用Apache或nginx的相当通用的配置(请参阅此处的其他答案)。

设置repo目录

mkdir /home/srv/packages/local-xenial
cd /home/srv/packages/local-xenial

然后将这样的一行添加到sources.list中:

deb file:/home/srv/packages/local-xenial/ ./

添加和删​​除软件包

删除软件包

rm /home/srv/packages/local-xenial/some_package_idont_like

添加软件包

cp /some/dir/apackage.deb /home/srv/packages/local-xenial

现在运行以下脚本,它将生成Packages,Release和InRelease文件,并使用您的gpg私钥对它们进行签名:

#!/bin/bash

if [ -z "$1" ]; then
       echo -e "usage: `basename $0` DISTRO
where DISTRO is the Ubuntu version codename (e.g. 14.04 is trusty)\n
The way to use this script is to do the changes to the repo first, i.e. delete or copy in the .deb file to /srv/packages/local-DISTRO, and then run this script\n
This script can be run as an unprivileged user - root is not needed so long as your user can write to the local repository directory"
else
    cd /srv/packages/local-"$1"

    # Generate the Packages file
    dpkg-scanpackages . /dev/null > Packages
    gzip --keep --force -9 Packages

    # Generate the Release file
    cat conf/distributions > Release
    # The Date: field has the same format as the Debian package changelog entries,
    # that is, RFC 2822 with time zone +0000
    echo -e "Date: `LANG=C date -Ru`" >> Release
    # Release must contain MD5 sums of all repository files (in a simple repo just the Packages and Packages.gz files)
    echo -e 'MD5Sum:' >> Release
    printf ' '$(md5sum Packages.gz | cut --delimiter=' ' --fields=1)' %16d Packages.gz' $(wc --bytes Packages.gz | cut --delimiter=' ' --fields=1) >> Release
    printf '\n '$(md5sum Packages | cut --delimiter=' ' --fields=1)' %16d Packages' $(wc --bytes Packages | cut --delimiter=' ' --fields=1) >> Release
    # Release must contain SHA256 sums of all repository files (in a simple repo just the Packages and Packages.gz files)
    echo -e '\nSHA256:' >> Release
    printf ' '$(sha256sum Packages.gz | cut --delimiter=' ' --fields=1)' %16d Packages.gz' $(wc --bytes Packages.gz | cut --delimiter=' ' --fields=1) >> Release
    printf '\n '$(sha256sum Packages | cut --delimiter=' ' --fields=1)' %16d Packages' $(wc --bytes Packages | cut --delimiter=' ' --fields=1) >> Release

    # Clearsign the Release file (that is, sign it without encrypting it)
    gpg --clearsign --digest-algo SHA512 --local-user $USER -o InRelease Release
    # Release.gpg only need for older apt versions
    # gpg -abs --digest-algo SHA512 --local-user $USER -o Release.gpg Release

    # Get apt to see the changes
    sudo apt-get update
fi

conf /distributions文件的示例内容

Origin: My_Local_Repo
Label: My_Local_Repo
Codename: xenial
Architectures: i386 amd64
Components: main
Description: My local APT repository
SignWith: 12345ABC

链接

https://wiki.debian.org/RepositoryFormat

http://ubuntuforums.org/showthread.php?t=1090731

https://help.ubuntu.com/community/CreateAuthenticatedRepository

第四种思路

您还可以通过nginx和reprepro设置本地源服务器:

  1. 安装debian软件包

    sudo apt-get install reprepro nginx 
    
  2. 为reprepro制作目录并编辑它

    sudo mkdir -p /srv/reprepro/ubuntu/{conf,dists,incoming,indices,logs,pool,project,tmp}
    
    $ cd /srv/reprepro/ubuntu/
    $ sudo chown -R `whoami` . # changes the repository owner to the current user
    

    /SRV /reprepro /ubuntu的/CONF /分布

    Origin: Your Name
    Label: Your repository name
    Codename: karmic
    Architectures: i386 amd64 source
    Components: main
    Description: Description of repository you are creating
    SignWith: YOUR-KEY-ID
    

    /SRV /reprepro /Ubuntu的/conf目录/选项

    ask-passphrase
    basedir .
    
  3. 将它包含在reprepro中,然后构建它

    $ reprepro includedeb karmic /path/to/my-package_0.1-1.deb \
    # change /path/to/my-package_0.1-1.deb to the path to your package
    
  4. 配置nginx

    /etc/nginx/sites-available/vhost-packages.conf

    server {
      listen 80;
      server_name packages.internal;
    
      access_log /var/log/nginx/packages-access.log;
      error_log /var/log/nginx/packages-error.log;
    
      location / {
        root /srv/reprepro;
        index index.html;
      }
    
      location ~ /(.*)/conf {
        deny all;
      }
    
      location ~ /(.*)/db {
        deny all;
      }
    }
    
  5. 优化桶大小

    /etc/nginx/conf.d/server_names_hash_bucket_size.conf

    server_names_hash_bucket_size 64;
    

参考Install Guide Link

第五种思路

你可能想看看apt-mirrorapt-cacher

这里是关于how to install的指南并使用它。

第六种思路

There are several reasons you may want to create a local repository. The first is that you want to save on bandwidth if you have multiple Ubuntu machines to update. For example if you had 25 Ubuntu machines that all needed updating at least once a week, you would significantly save bandwidth because you could do all but the repository locally.

Most organizations have decent bandwidth for their network gateways but this bandwidth is a precious commodity that needs to be used wisely.

Many organizations still have routers with 10MB or 100MB limits at the gateway but 1 GB network connections internally so bandwidth could be better used internally. The second reason for creating your own repository is that you can control what applications are loaded on your internal Ubuntu machines.

You can remove any applications your organization does not want to use on the local network from the repository that updates the machines. Even better, you can create a test box and test applications and versions before you allow them to roll out into your network assuring security and stability.

You first have to setup a mirror, to do that you need to Just press Ctrl+Alt+T on your keyboard to open Terminal. When it opens, run the command below.

apt-get install apt-mirror  

Once you have your set up apt-mirror you can start your download of the repository with this command.

apt-mirror /etc/apt/mirror.list1

Read on

1来源:Create an Ubuntu Repository

第七种思路

创建脱机本地存储库1.创建一个可访问的目录(至少由root)

sudo mkdir /var/my-local-repo

  1. 将所有的deb文件复制到这个目录。

  2. 扫描目录

sudo dpkg-scanpackages /var/my-local-repo /dev/null > /var/my-local-repo/Packages

  1. 将本地存储库添加到源

echo “deb file:/var/my-local-repo ./” > /tmp/my-local.list

sudo mv /tmp/my-local.list /etc/apt/sources.list.d/my-local.list

sudo apt-get update

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/730.html,未经允许,请勿转载。