在Linux系统中,软件包通常存放于软件源(Repository)中。添加软件源后,您可以利用Linux系统提供的包管理工具查找、安装或更新软件源中包含的软件,从而快速完成系统运维或满足应用开发需求。本文以添加阿里云软件源为例,为您介绍在不同Linux发行版本上管理及使用软件源的操作步骤。
背景信息
不同软件源的访问速度会受到地域及距离等因素的影响,阿里巴巴开源镜像站为互联网用户提供免费、高速的一站式镜像服务,提供包括CentOS、Debian、Ubuntu、Fedora、Arch Linux、Gentoo、openSUSE及Scientific Linux等主流发行版的软件源镜像。该软件源对阿里云用户和非阿里云用户均免费开放。更多信息,请访问阿里巴巴开源镜像站。
阿里云官方提供的公共镜像利用
cloud-init
工具,自动将常见Linux发行版的软件源替换为阿里云镜像站ECS内网软件源地址,ECS用户可直接使用,无需手动进行配置,如果您需要为Linux发行版手动添加或修复其软件源,请参考本文进行操作,具体操作,请参见为Linux实例添加阿里云软件源。本文添加的镜像地址为阿里云镜像站公网软件源地址
http://mirrors.aliyun.com
,适用于具备公网访问能力的Linux系统,如果您的服务器为阿里云ECS实例,建议您使用阿里云镜像站ECS内网软件源地址http://mirrors.cloud.aliyuncs.com
,以获得更快的下载速度。
为Linux实例添加阿里云软件源
远程连接Linux实例
具体操作,请参见通过密码或密钥认证登录Linux实例。本文以使用ecs-user用户登录目标主机为例,您也可以选择其他登录方式。
添加阿里云软件源
说明以下列举了几种常见的Linux发行版添加阿里云软件源的操作方法。在实际操作中,请以您的系统版本为准。如需为其他版本的Linux发行版添加阿里云软件源,请在阿里巴巴开源镜像站页面,找到并单击您使用的操作系统,以查看具体操作步骤。
Alibaba Cloud Linux 3/2
运行以下命令,备份原软件源。
sudo mv /etc/yum.repos.d/AliYun.repo /etc/yum.repos.d/AliYun.repo.backup
运行以下命令,重新添加软件源。
Alibaba Cloud Linux 3
sudo rpm -Uvh --reinstall https://mirrors.aliyun.com/alinux/3/updates/x86_64/Packages/alinux-repos-3.2104.10-2.al8.x86_64.rpm
Alibaba Cloud Linux 2
sudo rpm -Uvh --reinstall https://mirrors.aliyun.com/alinux/2/updates/x86_64/Packages/alinux-release-2.1903-11.al7.x86_64.rpm
运行以下命令,生成本地缓存,加快搜索、安装软件的速度。
sudo yum clean all && sudo yum makecache
CentOS 8/7/6
运行以下命令,备份原软件源。
sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
运行以下命令,添加阿里云软件源。
CentOS 8
url=https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo;if [ -f /usr/bin/curl ];then sudo curl -sSO $url;else sudo wget -O /etc/yum.repos.d/Centos-vault-8.5.2111.repo $url; fi
CentOS 7
url=https://mirrors.aliyun.com/repo/Centos-7.repo;if [ -f /usr/bin/curl ];then sudo curl -sSO $url;else sudo wget -O /etc/yum.repos.d/Centos-7.repo $url; fi
CentOS 6
url=https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo;if [ -f /usr/bin/curl ];then sudo curl -sSO $url;else sudo wget -O /etc/yum.repos.d/Centos-vault-6.10.repo $url; fi
运行以下命令,生成本地缓存,加快搜索、安装软件的速度。
sudo yum clean all && sudo yum makecache
Ubuntu
运行以下命令,备份原软件源。
sudo mv /etc/apt/sources.list /etc/apt/sources.list.backup
运行以下命令,新建并打开配置文件。
sudo vim /etc/apt/sources.list
按
i
进入编辑模式,为配置文件添加以下信息。Ubuntu 24.04
deb https://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ noble-security main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ noble-security main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ noble-updates main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ noble-updates main restricted universe multiverse # deb https://mirrors.aliyun.com/ubuntu/ noble-proposed main restricted universe multiverse # deb-src https://mirrors.aliyun.com/ubuntu/ noble-proposed main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ noble-backports main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ noble-backports main restricted universe multiverse
Ubuntu 23.04
deb https://mirrors.aliyun.com/ubuntu/ lunar main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ lunar main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ lunar-security main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ lunar-security main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ lunar-updates main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ lunar-updates main restricted universe multiverse # deb https://mirrors.aliyun.com/ubuntu/ lunar-proposed main restricted universe multiverse # deb-src https://mirrors.aliyun.com/ubuntu/ lunar-proposed main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ lunar-backports main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ lunar-backports main restricted universe multiverse
Ubuntu 22.04
deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse # deb https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse # deb-src https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
Ubuntu 20.04
deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse # deb https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse # deb-src https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
Ubuntu 18.04
deb https://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse # deb https://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse # deb-src https://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
按
Esc
键,输入:wq
,按Enter
键关闭并保存配置文件。运行以下命令,更新软件包信息库。
sudo apt update
Debian
运行以下命令,备份原软件源。
sudo mv /etc/apt/sources.list /etc/apt/sources.list.backup
运行以下命令,新建并打开配置文件。
sudo vim /etc/apt/sources.list
按
i
进入编辑模式,为配置文件添加以下信息。Debian 11.x
deb https://mirrors.aliyun.com/debian/ bullseye main non-free contrib deb-src https://mirrors.aliyun.com/debian/ bullseye main non-free contrib deb https://mirrors.aliyun.com/debian-security/ bullseye-security main deb-src https://mirrors.aliyun.com/debian-security/ bullseye-security main deb https://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib deb-src https://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib deb https://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib deb-src https://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib
Debian 10.x
deb https://mirrors.aliyun.com/debian/ buster main non-free contrib deb-src https://mirrors.aliyun.com/debian/ buster main non-free contrib deb https://mirrors.aliyun.com/debian-security buster/updates main deb-src https://mirrors.aliyun.com/debian-security buster/updates main deb https://mirrors.aliyun.com/debian/ buster-updates main non-free contrib deb-src https://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
Debian 9.x
deb https://mirrors.aliyun.com/debian-archive/debian stretch main contrib non-free #deb https://mirrors.aliyun.com/debian-archive/debian stretch-proposed-updates main non-free contrib #deb https://mirrors.aliyun.com/debian-archive/debian stretch-backports main non-free contrib deb https://mirrors.aliyun.com/debian-archive/debian-security stretch/updates main contrib non-free deb-src https://mirrors.aliyun.com/debian-archive/debian stretch main contrib non-free #deb-src https://mirrors.aliyun.com/debian-archive/debian stretch-proposed-updates main contrib non-free #deb-src https://mirrors.aliyun.com/debian-archive/debian stretch-backports main contrib non-free deb-src https://mirrors.aliyun.com/debian-archive/debian-security stretch/updates main contrib non-free
Debian 8.x
deb https://mirrors.aliyun.com/debian-archive/debian/ jessie main non-free contrib deb-src https://mirrors.aliyun.com/debian-archive/debian/ jessie main non-free contrib
按
Esc
键,输入:wq
,按Enter
键关闭并保存配置文件。运行以下命令,更新软件包信息库。
sudo apt update
Fedora
运行以下命令,备份原软件源。
sudo mv /etc/yum.repos.d/fedora.repo /etc/yum.repos.d/fedora.repo.backup sudo mv /etc/yum.repos.d/fedora-updates.repo /etc/yum.repos.d/fedora-updates.repo.backup
运行以下命令,添加阿里云Fedora软件源。
url=http://mirrors.aliyun.com/repo/fedora.repo;if [ -f /usr/bin/curl ];then sudo curl -sSO $url;else sudo wget -O /etc/yum.repos.d//etc/yum.repos.d/fedora.repo $url; fi
运行以下命令,添加阿里云fedora-updates软件源。
url=http://mirrors.aliyun.com/repo/fedora-updates.repo;if [ -f /usr/bin/curl ];then sudo curl -sSO $url;else sudo wget -O /etc/yum.repos.d//etc/yum.repos.d/fedora-updates.repo $url; fi
运行以下命令,生成本地缓存。
sudo yum clean all && sudo yum makecache
openSUSE 15.6
运行以下命令,备份原软件源。
sudo rename '.repo' '.repo.backup' /etc/zypp/repos.d/openSUSE*.repo
运行以下命令,添加阿里云软件源。
sudo zypper addrepo -f http://mirrors.aliyun.com/opensuse/distribution/leap/15.6/repo/oss/ openSUSE-15.6-Oss sudo zypper addrepo -f http://mirrors.aliyun.com/opensuse/distribution/leap/15.6/repo/non-oss/ openSUSE-15.6-Non-Oss sudo zypper addrepo -f http://mirrors.aliyun.com/opensuse/update/leap/15.6/oss/ openSUSE-15.6-Update-Oss sudo zypper addrepo -f http://mirrors.aliyun.com/opensuse/update/leap/15.6/non-oss/ openSUSE-15.6-Update-Non-Oss
运行以下命令,生成本地缓存。
sudo zypper ref
使用包管理工具管理软件
在Linux系统中,您可以使用包管理工具对软件源中包含的软件进行管理。常见的包管理工具包括yum
、dnf
、apt
、zypper
等。本文为您介绍不同操作系统如何通过包管理工具查询、安装、更新和删除软件。在实际使用中,您可以按照自身需求替换示例软件包名称。
Linux发行版中的软件包会不断更新,以添加新功能、修复错误、提供安全更新等,您可以根据自身需求选择更新软件包至最新版本。
Alibaba Cloud Linux / CentOS / Fedora
查找软件包
运行以下命令,使用yum包管理工具查找软件包。
sudo yum search <软件包关键字>
示例:运行以下命令,搜索包含
nginx
关键字的软件包。sudo yum search nginx
安装软件包
运行以下命令,使用yum包管理工具安装软件包。
sudo yum install <软件包名称>
示例:运行以下命令,安装
nginx
软件包。sudo yum install nginx
更新软件包
运行以下命令,使用yum包管理工具更新软件包。
sudo yum update <软件包名称>
示例:运行以下命令,更新
nginx
软件包。sudo yum update nginx
删除软件包
运行以下命令,使用yum包管理工具删除软件包。
sudo yum remove <软件包名称>
示例:运行以下命令,删除
nginx
软件包。sudo yum remove nginx
Ubuntu / Debian
查找软件包
运行以下命令,使用apt包管理工具查找软件包。
sudo apt search <软件包关键字>
示例:运行以下命令,搜索包含
nginx
关键字的软件包。sudo apt search nginx
安装软件包
运行以下命令,使用apt包管理工具安装软件包。
sudo apt install <软件包名称>
示例:运行以下命令,安装
nginx
软件包。sudo apt install nginx
更新软件包
运行以下命令,使用apt包管理工具更新软件包。
sudo apt upgrade <软件包名称>
示例:运行以下命令,更新
nginx
软件包。sudo apt upgrade nginx
删除软件包
运行以下命令,使用yum包管理工具删除软件包。
sudo apt remove <软件包名称>
示例:运行以下命令,更新
nginx
软件包。sudo apt remove nginx
openSUSE
查找软件包
运行以下命令,使用zypper包管理工具查找软件包。
sudo zypper search <软件包关键字>
示例:运行以下命令,搜索包含
nginx
关键字的软件包。sudo zypper search nginx
安装软件包
运行以下命令,使用zypper包管理工具安装软件包。
sudo zypper install <软件包名称>
示例:运行以下命令,安装
nginx
软件包。sudo zypper install nginx
更新软件包
运行以下命令,使用zypper包管理工具更新软件包。
sudo zypper update <软件包名称>
示例:运行以下命令,更新
nginx
软件包。sudo zypper update nginx
删除软件包
运行以下命令,使用zypper包管理工具删除软件包。
sudo zypper remove <软件包名称>
示例:运行以下命令,更新
nginx
软件包。sudo zypper remove nginx