网站搜索

在 Ubuntu 26.04 中安装 Dante Socks v5 代理服务器


这是一个逐步指南,展示如何在 Ubuntu 26.04 LTS 中安装和设置 Dante socks5 代理服务器。

Dante 是一个免费的开源软件,允许在客户端和服务器之间路由网络流量。使用它,你可以绕过网络限制,例如访问通过直接访问无法使用的网络资源。

您也可以使用它来隐藏您的原始 IP 地址,这样目标服务器只会看到 Dante 服务器的 IP。在我的情况下,我使用 Dante SOCKS 代理来降低延迟,以获得更好的游戏体验。

先决条件:

要遵循本教程,您需要一台具有 sudo 权限的 Ubuntu 26.04 服务器。

在我的情况下,我有一个 VPS 服务器,配备了 1 核 CPU、1 GB 内存和 20 GB 存储。

步骤 1:安装 Dante 服务器

Dante 可在 Ubuntu 及大多数其他 Linux 发行版的系统仓库中获得。要安装它,请连接到您的 Ubuntu 服务器,然后依次运行以下命令:

sudo apt update
sudo apt install dante-server

安装服务器包后,它将自动创建一个 systemd 服务并在后台静默运行。

要检查服务状态,请使用命令:

systemctl status danted.service

对于 Debian,如果服务未运行,请运行 systemctl enable --now danted.service 来启用并启动该服务。

如果服务已启用但未能运行,不用担心,按照步骤2编辑配置文件。

步骤 2:配置 Dante 服务器

Dante 默认会加载 /etc/danted.conf 文件作为服务器配置。该文件默认情况下所有配置都被禁用,导致服务无法运行。

要编辑配置文件,你可能首先需要通过运行以下命令来备份(或者如果你愿意可以删除文件):

sudo mv /etc/danted.conf /etc/danted.conf.backup

接下来,运行下面的命令来编辑文件:

sudo nano /etc/danted.conf

如果你删除了原始文件或使用 mv 命令进行备份,该命令将自动创建一个内容为空的文件。

当文件打开时,添加以下内容并进行相应编辑。

logoutput: /var/log/danted.log
errorlog: /var/log/danted.errlog
user.privileged: root
user.unprivileged: nobody

# The listening network interface or address.
internal: 0.0.0.0 port=1080
internal: :: port = 1080

# The proxying network interface or address.
external: eth0

# socks-rules determine what is proxied through the external interface.
socksmethod: username

# client-rules determine who can connect to the internal interface.
clientmethod: none

client pass {
    from: 0.0.0.0/0 to: 0.0.0.0/0
    log: error connect disconnect
}

socks pass {
    from: 0.0.0.0/0 to: 0.0.0.0/0
    log: error connect disconnect
}

这里有关于规则的更多内容:

  • logoutput: /var/log/danted.log - 设置访问日志位置(可选)。
  • errorlog: /var/log/danted.errlog - 设置错误日志位置(可选)。
  • user.privileged: root - 以 root 身份运行后台服务,之后将降低权限。
  • user.unprivileged: nobody - 使用用户 nobody 运行普通代理操作。
  • internal: 0.0.0.0 port=1080 - 在端口 1080 上监听所有 IPv4 接口。如果需要,你可以相应地更改 IP 和端口号。
  • internal: :: port=1080 - 监听 IPv6(可选)。
  • external: eth0 - 将 eth0 替换为你的服务器网络接口名称。在服务器上运行 ip route get 1.1.1.1 | awk '{print $5; exit}' 命令来查看。
  • clientmethod: none - 意味着 client pass {} 中的任何 IP 都可以启动 TCP 连接。
  • socksmethod: username - 你需要提供用户名和密码进行认证,即使使用 clientmethod: none。使用 socksmethod: none(尽管不安全)可以让 socks pass {} 部分中的任何 IP 在不进行认证的情况下访问。
  • client pass { from: 0.0.0.0/0 to: 0.0.0.0/0 ...} - 客户端尝试访问 dante 服务器时的第一个关卡(TCP 连接)。它意味着允许从任何 IP 访问服务器的任何网络接口。你可以使用 from: 23.168.111.125/32 to: 0.0.0.0/0,这样只有特定的 IP(在这个例子中是你的公共 IP 23.168.111.125)可以访问。
  • socks pass { from: 0.0.0.0/0 to: 0.0.0.0/0 ...} - 控制允许的客户端可以做什么。使用 socksmethod: username 需要密码验证。另外,你也可以相应地替换 IP。

上述规则适用于 TCP 连接。您可以添加更多 client pass {} 和 socks pass {} 部分,规则如下:

  • from: ::/0 to: ::/0 - 允许从任何 IPv6 地址访问。
  • command: udpreply - 允许 UDP 关联。
  • udp.portrange: 40000-45000 - 设置UDP端口范围。

所以,它可以看起来像这样:

client pass {
  from: 0.0.0.0/0 to: 0.0.0.0/0
}
client pass {
  from: ::/0 to: ::/0
}

socks pass {
  from: 0.0.0.0/0 to: 0.0.0.0/0
  command: connect udpassociate
  log: connect disconnect ioop
}
socks pass {
  from: ::/0 to: ::/0
  command: connect udpassociate
  log: connect disconnect ioop
}

# Log UDP replies from remote hosts back to clients
socks pass {
  from: 0.0.0.0/0 to: 0.0.0.0/0
  command: udpreply
  log: connect disconnect ioop
}
socks pass {
  from: ::/0 to: ::/0
  command: udpreply
  log: connect disconnect ioop
}

编辑文件后,按 Ctrl+S 保存,按 Ctrl+X 退出。

第3步:创建用于身份验证的用户

出于身份验证的目的,您需要在服务器端创建一个用户,方法是运行以下命令:

sudo useradd -M -s /usr/sbin/nologin USER_NAME_HERE

然后为用户设置密码:

sudo passwd USER_NAME_HERE

将 USER_NAME_HERE 替换为您想要的任何名称。然后,您可以在连接过程中使用用户名和密码进行身份验证。

第4步:创建日志文件并更改所有权

如果你启用了日志,那么你还需要编辑日志文件的所有权。

首先,运行命令以创建日志文件:

touch /var/log/danted.log
touch /var/log/danted.errlog

然后,将所有者更改为 noboby 和 nogroup:

chown nobody:nogroup /var/log/danted.log
chown nobody:nogroup /var/log/danted.errlog

步骤5:配置防火墙

如果你的 Ubuntu 服务器启用了防火墙(运行 sudo ufw status),那么运行以下命令以为 Dante 代理服务器开放端口:

sudo ufw allow 1080/tcp

这里根据你使用的端口替换 1080,如果你启用了 UDP 关联,也运行下面的命令:

sudo ufw allow 40000:45000/udp

第6步:应用更改

最后,重启 dante 服务以应用更改:

sudo systemctl restart danted

并且,运行下面的命令以检查服务状态:

sudo systemctl status danted --no-pager

/etc/danted.conf 中的任何配置错误都可能导致服务启动失败。

第7步:尝试连接到Dante服务器

在正确设置服务器后,您可以尝试在本地 Linux 计算机上运行以下命令以启动连接:

curl -v -x socks5://USER_NAME:PASSWORD@SERVER_IP:1080 http://www.google.com/
It should output something looks like:
*   Trying xx.xxx.xxx.xxx:1080...
* Host www.google.com:80 was resolved.
* IPv6: 2001:4860:482a:7700::, 2001:4860:4826:7700::, 2001:4860:482b:7700::, 2001:4860:4827:7700::, 2001:4860:4829:7700::, 2001:4860:482d:7700::, 2001:4860:4828:7700::, 2001:4860:482c:7700::
* IPv4: 142.251.153.119, 142.251.156.119, 142.251.150.119, 142.251.157.119, 142.251.152.119, 142.251.154.119, 142.251.151.119, 142.251.155.119
* Opened SOCKS connection from 192.168.0.104 port 49060 to www.google.com port 80 (via 23.106.152.194 port 1138)
* Established connection to 23.106.152.194 (23.106.152.194 port 1138) from 192.168.0.104 port 49060 
* using HTTP/1.x
...

对于图形化的 socks5 客户端,我使用 Proxifier,但它不是免费软件。你可以尝试免费开源的 ProxyBridge,它可在 Linux、Windows 和 macOS 上运行。