Linux aria2c命令详解

aria2c是一个轻量级、多协议、多源、跨平台的下载工具,支持HTTP/HTTPS、FTP、SFTP、BitTorrent和磁力链接。它最大的特点是支持多线程下载和RPC远程控制。

核心优势:支持最多协议 | 多线程加速 | BT磁力下载 | JSON-RPC远程控制 | 轻量高效

核心特性

多协议支持

HTTP/HTTPS, FTP, SFTP, BitTorrent, 磁力链接, Metalink

多线程下载

同时从多个源下载,支持分段下载和智能文件合并

RPC控制

支持JSON-RPC/XML-RPC远程控制,可搭建离线下载服务器

安装方法

Ubuntu/Debian
sudo apt update
sudo apt install aria2
CentOS/RHEL
sudo yum install aria2
# 或使用epel源
sudo yum install epel-release
sudo yum install aria2
Fedora
sudo dnf install aria2
macOS
brew install aria2

基本语法

aria2c [选项] [URL | 磁力链接 | .torrent文件...]

常用选项

选项 描述 默认值
-x, --max-connection-per-server 每个服务器的最大连接数 1
-s, --split 每个文件的分段数 5
-j, --max-concurrent-downloads 最大同时下载任务数 5
-d, --dir 下载文件保存目录 当前目录
-o, --out 下载文件重命名 -
-c, --continue 断点续传 启用
-l, --log 日志文件路径 -
-D, --daemon 后台运行模式
--seed-ratio BT做种分享率 1.0
--listen-port BT监听端口范围 6881-6999
--rpc-listen-port RPC监听端口 6800

基础下载示例

HTTP 普通文件下载

使用多线程下载文件:

# 每个服务器5个连接,文件分5段下载
aria2c -x 5 -s 5 http://example.com/file.zip

# 限制下载速度为1M/s
aria2c --max-download-limit=1M http://example.com/large.iso
HTTPS 加密下载

下载HTTPS资源:

# 跳过证书验证(不安全,仅测试用)
aria2c --check-certificate=false https://example.com/file.zip

# 使用指定CA证书
aria2c --ca-certificate=/path/to/ca.pem https://example.com/file.zip
FTP FTP下载

下载FTP服务器上的文件:

# 匿名下载
aria2c ftp://ftp.example.com/pub/file.tar.gz

# 需要认证的FTP
aria2c --ftp-user=username --ftp-passwd=password ftp://example.com/file.zip
MULTI 批量下载

从文件读取URL列表批量下载:

# 创建下载列表文件
echo "http://example.com/file1.zip" >> list.txt
echo "http://example.com/file2.iso" >> list.txt
echo "http://example.com/file3.tar.gz" >> list.txt

# 批量下载
aria2c -i list.txt -j 3

# 或直接指定多个URL
aria2c http://example.com/file1.zip http://example.com/file2.iso

BitTorrent和磁力链接

BT 种子文件下载
# 下载.torrent文件
aria2c --seed-time=0 --bt-enable-lpd=true ubuntu-22.04.torrent

# 指定下载目录
aria2c -d ~/Downloads --bt-seed-unverified=true file.torrent

# 强制加密连接
aria2c --bt-require-crypto=true --bt-min-crypto-level=arc4 file.torrent
MAGNET 磁力链接下载
# 下载磁力链接
aria2c "magnet:?xt=urn:btih:TORRENT_HASH"

# 指定tracker服务器
aria2c --bt-tracker="udp://tracker.opentrackr.org:1337/announce" magnet_uri

# 限制上传速度
aria2c --max-upload-limit=50K magnet_uri
磁力链接格式: magnet:?xt=urn:btih:[HASH]&dn=[文件名]&tr=[tracker地址]
DHT BT高级配置
# 启用DHT和本地对等发现
aria2c --enable-dht --dht-listen-port=6881 --bt-enable-lpd=true file.torrent

# 指定外部DHT节点文件
aria2c --dht-file-path=/path/to/dht.dat file.torrent

# 设置BT监听端口范围
aria2c --listen-port=6881-6891 file.torrent

# 做种设置(分享率达到2.0后停止)
aria2c --seed-ratio=2.0 --seed-time=60 file.torrent

RPC远程控制

启动RPC服务
# 启动RPC服务
aria2c --enable-rpc --rpc-listen-all=true --rpc-allow-origin-all \
       --rpc-listen-port=6800 --dir=/path/to/downloads

# 带认证的RPC
aria2c --enable-rpc --rpc-secret=mysecret \
       --rpc-listen-port=6800 -D

# 保存session(重启后恢复任务)
aria2c --enable-rpc --save-session=/path/to/session.txt \
       --input-file=/path/to/session.txt
RPC客户端使用示例
# 使用aria2p Python客户端
pip install aria2p
aria2p add http://example.com/file.zip

# 使用curl添加任务
curl http://localhost:6800/jsonrpc -X POST \
  -d '{"jsonrpc":"2.0","id":"1","method":"aria2.addUri",
       "params":["token:mysecret",["http://example.com/file.zip"]]}'

# 查看活动任务
curl http://localhost:6800/jsonrpc -X POST \
  -d '{"jsonrpc":"2.0","id":"1","method":"aria2.tellActive",
       "params":["token:mysecret"]}'

配置文件

aria2配置文件示例(~/.aria2/aria2.conf)
# 下载目录
dir=/home/user/Downloads

# 日志
log-level=warn
log=/home/user/.aria2/aria2.log

# 网络设置
max-concurrent-downloads=5
max-connection-per-server=16
split=16
min-split-size=1M

# 磁盘缓存
disk-cache=32M
file-allocation=prealloc

# RPC设置
enable-rpc=true
rpc-listen-all=true
rpc-listen-port=6800
rpc-secret=your_secret_here

# BT设置
bt-enable-lpd=true
enable-dht=true
dht-listen-port=6881-6999
bt-seed-unverified=true
seed-ratio=1.0
seed-time=60

# 代理设置(可选)
#all-proxy=http://proxy:8080
#all-proxy-user=user
#all-proxy-passwd=passwd

使用配置文件:aria2c --conf-path=/path/to/aria2.conf

高级技巧

# 1. Metalink下载(多镜像自动选择)
aria2c http://example.com/file.metalink

# 2. 从网页提取链接下载
aria2c --force-sequential=false --max-connection-per-server=4 \
  --max-concurrent-downloads=2 --summary-interval=0 \
  --follow-metalink=mem --check-certificate=false \
  --max-file-not-found=5 --min-split-size=5M \
  --http-no-cache=true \
  "http://example.com/download-page.html"

# 3. 下载YouTube视频(需yt-dlp配合)
yt-dlp -f best --get-url "https://youtu.be/VIDEO_ID" | aria2c -i -

# 4. 创建下载守护进程
aria2c --enable-rpc --rpc-listen-all --dir=/data/downloads \
  --log=/var/log/aria2.log --daemon=true

# 5. 监控下载进度
watch -n 1 'aria2c -i list.txt --summary-interval=1 2>&1 | tail -20'

# 6. 使用代理下载BT
aria2c --all-proxy=http://proxy:8080 --bt-tracker-connect-timeout=10 \
  --bt-tracker-timeout=10 file.torrent

# 7. 限制总下载速度
aria2c --max-overall-download-limit=2M file1.iso file2.zip

# 8. 下载完成后执行脚本
aria2c --on-download-complete=/path/to/script.sh http://example.com/file.zip
常见问题解决
  • 下载速度慢:尝试增加 -x-s 参数值
  • BT无速度:添加公共tracker:--bt-tracker="udp://tracker.opentrackr.org:1337/announce,..."
  • 内存占用高:调整 --disk-cache 大小,默认32M
  • RPC连接失败:检查防火墙是否开放6800端口
  • 磁盘空间不足:使用 --file-allocation=falloc 预分配空间

性能对比

功能对比 aria2c axel wget
协议支持 HTTP/HTTPS, FTP, SFTP, BT, 磁力 HTTP/HTTPS, FTP HTTP/HTTPS, FTP
多线程 支持(可配置分段) 支持(固定线程) 不支持
BT下载 完整支持 不支持 不支持
RPC控制 JSON-RPC/XML-RPC 不支持 不支持
配置文件 支持完整配置 基础配置 支持配置
适用场景 全能下载、离线下载、BT下载 HTTP/FTP多线程加速 简单下载、网页抓取