Linux入门-软件管理篇(APT)

APT 软件安装管理系统,Debian下的系统软件安装管理工作

除非你确实知道你在什么,请一直保持使用APT相关工具来管理软件,而不要使用其它工具或方式。

准备工作

使用APT安装更新系统前,首先

  • 确保网络或其它来源(光盘等)可用
  • 如果使用网络镜像,请手动或自动选择最快的网络镜像来源
    #自动方式
    #参见下面的apt-spy
    
    #手动方式
    vim /etc/apt/sources.list
    

    如何安装一个软件包: apt-get

    安装,获取一个软件包

    apt-get install xxxx   #安装XXX
    
    apt-get update         #根据sources.list,更新软件包索引
    apt-get upgrade        #安装所有可用更新
    apt-get dist-upgrade   #增强版的upgrade,更好的处理包依赖关系,升级内核等等
    #上面这两步可以经常做,来保持系统的最新版本
    
    

    另外还有aptitude,一个半图像化的APT工具,可以代替apt-get

    关于 sources.list

    /etc/sources.list 这个文件保存着Debian系统的软件包来源安装镜像的来源信息,下面是一个示例

    deb http://site.example.com/debian distribution component1 component2 component3
    deb-src http://site.example.com/debian distribution component1 component2 component3
    
    deb     http://httpredir.debian.org/debian stretch-updates main
    deb-src http://httpredir.debian.org/debian stretch-updates main
    
    deb http://security.debian.org/debian-security stretch/updates main contrib non-free
    
    
    deb http://www.anheng.com.cn/debian/ etch main contrib non-free
    deb-src http://www.anheng.com.cn/debian/ etch main
    deb http://security.debian.org/ etch/updates main contrib non-free

    几个部分:

    • Archive type : deb 二进制代码源,deb-src源代码源,大部分情况deb-src是不必须的
    • Repository URL: 仓库的URL,也可以是File,CD等等;(http://httpredir.debian.org/debian  )是一个支持自动转向的仓库接口,会尝试把你导向更加高效、快速的仓库URL
    • Distribution: 制定具体版本可以是 stable,testing 等分级版本,也可以是 (jessie, stretch, buster, sid) 等固定的版本代号,一般我们使用版本代号而不使用分级版本。
      Debian的升级机制是完全平滑的,从Debian8.1.1升级到8.1.2和从8.1.1升级到9.1是没有任何区别的,都快通过apt-get来平滑实现,这也就意味着如果我们使用类似Stable的分级版本源,当Debian进行重要版本更替,把新的版本(etc. stretch)升级到Stable级别,替换原有发行版本(etc. jessie),这时我的系统会自动升级到新的版本。而这种升级由于变化较大,可能会引起一些应用软件的运行问题,需要做好一定准备,所以我们一般使用版本代号,以避免因为Debian的更新机制而发生出乎我们意外的重大版本升级。这也意味着,如果你希望进行一个重大发行版本升级,只需要修改这里 Distribution,然后进行Upgrade即可。
      Distribution还可以加一些后缀以启用几个子版本,比如 stretch-updates ,  stretch-backports ,。
      stretch-updates 指向的是一个推荐升级源 (ProposedUpdates),主要包括一些推荐进行的升级,而这些升级暂时还没有合并到当前运行的发行版中(before the next point release is made),简单说,就是一个稍微提前一些的升级源,可以不配置使用。
      stretch-backports 指向的是backports库,主要包括的是一些比较新的软件更新已经发布到下一个版本中去,而还没有集成(或者不准备集成到)当前运行的发行版中,主要目的是让不想进行重大发行版升级的系统,可以使用上比较新的软件,backports的使用是不推荐的,除非必要,不建议使用backports.
    • component:  指定使用的软件范围,可以是
      • main :  伴随Debian发行的,符合(DFSG-compliant)  的软件
      • contrib  : 符合(DFSG-compliant)  的软件,但需要依赖于一些其它包的软件
      • non-free : 不符合(DFSG-compliant)  的软件

    安全更新:

    Debian提供一个专门的安全更新机制,把安全更新和一般更新进行了分离,以保证安全更新可以更好的处理。很多Debain镜像是不提供安全更新的,同时Debian也建议不要使用其它服务器来源进行安全更新,所以一般来说我们只需要直接使用推荐安全更新源配置

    deb http://security.debian.org/debian-security some-like-stretch/updates main contrib non-free

    多个源:

    Debian会按照soureslist的顺序查找更新,更快的地址应该放在上面。同时不推荐使用相互交叉的多个更新源,这样会带来不可预料的问题!

    实例:

    根据上述解释,一个典型的、简洁的、推荐的sources.list代码如下(使用jessie发行版):

    #sources.list
    #1.non-free is ignored as default
    #2.some line can igored was marked with '#'
    deb     http://httpredir.debian.org/debian jessie main contrib
    #deb-src http://httpredir.debian.org/debian jessie main contrib
    
    deb     http://httpredir.debian.org/debian jessie-updates main contrib
    #deb-src http://httpredir.debian.org/debian jessie-updates main contrib
    
    deb     http://security.debian.org/debian-security/ jessie/updates main contrib non-free

    soures.list.d

    /etc/apt/soures.list.d 目录提供了一个扩展soureslist的机制,放在这个目录中的文件会自动附加到soures.list后面,一同被 apt处理。

    apt-spy

    一个测试工具,用来自动查找速度最快的更新服务器,它会自动改写/etc/apt/sources.list
    默认系统一般没安装,先用 apt-get install apt-spy 安装上

    apt-get install apt-spy #安装apt-spy
    mv /etc/apt/sources.list /etc/apt/sources.list.bak #备份来源列表文件
    apt-spy update #更新镜像目录
    apt-spy -d stable -a Asia -t 10 #测试目录,并生成sources.list

    发表评论