凌峰创科服务平台

ubuntu搭建pppoe服务器

核心概念

  • PPPoE (Point-to-Point Protocol over Ethernet):一种在以太网上进行点对点连接的协议,它常用于宽带拨号上网,你的家庭路由器很可能就是作为 PPPoE 客户端连接到运营商的服务器。
  • Ubuntu 服务器:我们将它配置成一个 PPPoE 服务器,为客户端(如电脑、路由器)提供拨号接入服务。
  • 认证方式:我们将使用最常用的 PAP (Password Authentication Protocol),因为它配置简单。

准备工作

  1. 一台 Ubuntu Server:推荐使用 Ubuntu 22.04 LTS 版本,本教程基于此版本,确保系统已更新。
  2. 静态 IP 地址:你的服务器必须有一个固定的、静态的 IP 地址,PPPoe 服务配置依赖于这个 IP。
  3. 管理员权限:需要 sudo 权限来执行安装和配置命令。
  4. 客户端设备:用于测试连接,可以是另一台电脑或一台路由器。

第一步:更新系统并安装必要软件

更新你的软件包列表并安装 ppp 包,它包含了 pppd (PPP 守护进程) 和 pppoeconf (配置向导)。

ubuntu搭建pppoe服务器-图1
(图片来源网络,侵删)
sudo apt update
sudo apt upgrade -y
sudo apt install -y ppp

第二步:配置静态 IP 地址

PPPoe 服务器需要监听在特定的网络接口上,最常见的情况是使用 eth0ens33(根据你的实际网卡名称而定),你需要为这个接口设置一个静态 IP。

  1. 查看当前网络配置和接口名称

    ip addr show

    找到你要用于提供 PPPoE 服务的网卡,eth0

  2. 编辑 Netplan 配置文件 在 Ubuntu 20.04/22.04 中,网络配置通常使用 Netplan,配置文件位于 /etc/netplan/ 目录下。

    ubuntu搭建pppoe服务器-图2
    (图片来源网络,侵删)
    ls /etc/netplan/

    你可能会看到一个类似 01-network-manager-all.yaml50-cloud-init.yaml 的文件,我们使用 vimnano 来编辑它。

    sudo nano /etc/netplan/50-cloud-init.yaml
  3. 修改配置文件 文件内容可能如下,我们需要将其修改为静态 IP。请务必将 eth0 和 IP 地址 168.100.1/24 替换为你自己的信息。

    # This file is generated from information provided by the datasource. Changes
    # to it will not persist across an instance reboot. To disable cloud's
    # managing of this host's network interfaces, write a file
    # /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
    # network: {config: disabled}
    network:
      version: 2
      ethernets:
        eth0:  # <-- 替换为你的网卡名称
          dhcp4: no
          dhcp6: no
          addresses: [192.168.100.1/24]  # <-- 设置服务器的静态IP
          gateway4: 192.168.100.1       # <-- 通常与服务器IP相同,或指向网关
          nameservers:
              addresses: [8.8.8.8, 1.1.1.1]
  4. 应用网络配置 保存文件后,运行以下命令使配置生效。

    sudo netplan apply

    你可以使用 ip addr show eth0 来验证 IP 是否已成功设置。

    ubuntu搭建pppoe服务器-图3
    (图片来源网络,侵删)

第三步:使用 pppoeconf 进行交互式配置

这是最核心的一步,pppoeconf 会引导你完成所有必要的设置。

sudo pppoeconf

你会看到一系列的交互式提示:

  1. Ethernet interface:

    • 提示:Do you want to start the PPPoE server on eth0? (Y/n)
    • 选择:Y (选择你的网卡,默认通常是 eth0)
  2. User name for the PPPoE server:

    • 提示:Enter the user name for the PPPoE server. This is the name that will be sent to the clients when they connect.
    • 输入:isp 或任何你喜欢的服务器名称,ubuntu-pppoe,这只是一个标识符。
  3. IP address for the PPPoE server:

    • 提示:Enter the IP address for the PPPoE server. This should be on the same network as your Ethernet interface.
    • 输入:168.100.1 (这里必须是你第二步中设置的静态 IP 地址)。
  4. IP pool for the PPPoE server:

    • 提示:Enter the IP pool for the PPPoE server. This is a range of IP addresses that will be assigned to clients.
    • 输入:168.100.100-192.168.100.200 (这是你分配给拨号客户端的 IP 地址范围,确保它不与你的局域网其他设备冲突)。
  5. DNS servers:

    • 提示:Enter the DNS servers for the PPPoE server. You can enter multiple servers separated by spaces.
    • 输入:8.8.8 8.8.4.4 (Google 的公共 DNS,你也可以使用你的路由器或本地 DNS 服务器的 IP)。
  6. Authentication:

    • 提示:Do you want to use the /etc/ppp/pap-secrets file for authentication? (Y/n)
    • 选择:Y (我们使用 PAP/CHAP 认证方式)。
  7. Create the PPPoE server:

    • 提示:Do you want to create the PPPoE server? (Y/n)
    • 选择:Y

配置完成后,pppoeconf 会自动创建和修改以下关键文件:

  • /etc/ppp/pptpd-options: PPPoE 服务器的主要配置选项。
  • /etc/ppp/pap-secrets: 存储用户名和密码的文件。
  • /etc/network/interfaces: 添加了启动 PPPoE 服务器的命令。
  • /etc/default/pppoe-server: PPPoE 服务器的启动脚本配置。

第四步:创建 PPPoE 拨号用户

用户名和密码存储在 /etc/ppp/pap-secrets 文件中,这个文件的格式是: 客户端名称 服务器名称 密码 IP地址

  • client-name: 客户端连接时使用的用户名。
  • server-name: 服务器名称,与你在 pppoeconf 中设置的名称一致。
  • password: 客户端的密码。
  • IP Address: 分配给该客户端的静态 IP,如果留空或使用 ,则从 IP 池中动态分配。

示例:添加一个名为 testuser,密码为 testpass123 的用户

# 使用 echo 命令追加到文件
echo "testuser isp testpass123 *" | sudo tee -a /etc/ppp/pap-secrets
# 或者使用 nano 手动编辑
sudo nano /etc/ppp/pap-secrets

在文件末尾添加一行:

testuser isp testpass123 *

保存并退出。 表示该用户将获得一个动态分配的 IP 地址。


第五步:启动和测试 PPPoE 服务器

  1. 启动服务 pppoeconf 通常会设置服务在开机时自动启动,你现在可以手动启动它。

    sudo systemctl start pppoe-server
  2. 检查服务状态

    sudo systemctl status pppoe-server

    如果看到绿色的 active (running) 字样,说明服务已成功启动。

  3. 测试连接 (客户端端) 这是最关键的一步,确保你的客户端(电脑或路由器)能够连接。

    • 在 Windows 客户端上测试:

      1. 打开 "网络和 Internet 设置" -> "网络和共享中心" -> "设置新的连接或网络"。
      2. 选择 "连接到 Internet"。
      3. 选择 "仍然设置新连接" -> "宽带 (PPPoE)"。
      4. 输入你在第四步创建的用户名 (testuser) 和密码 (testpass123)。
      5. 点击 "连接",如果成功,你应该能获取到 168.100.x 网段的 IP 地址,并且可以 ping 通服务器的 IP 168.100.1
    • 在 Linux (Ubuntu) 客户端上测试: 你可以使用 pppoeconf 来配置客户端,或者使用 pppd 命令行直接拨号。

      sudo pppd rp-pppoe:eth0 user testuser noauth noipdefault defaultroute usepeerdns

      连接成功后,可以用 ifconfigip addr 查看新创建的 ppp0 接口,并用 ping 192.168.100.1 测试。


第六步:故障排查

如果连接失败,请按以下步骤排查:

  1. 检查服务器防火墙 Ubuntu 默认有 ufw (Uncomplicated Firewall),PPPoE 通信需要 GRE 协议(有时是 UDP 1723),但通常 pppoeconf 会自动配置防火墙,如果手动修改过,请确保允许了流量。

    # 检查防火墙状态
    sudo ufw status
    # 如果防火墙开启,可以尝试临时关闭它来测试
    sudo ufw disable

    如果关闭防火墙后连接成功,说明是防火墙规则问题,你需要添加相应的规则。

  2. 检查日志 查看 /var/log/syslog/var/log/auth.log 文件,寻找与 ppppppoe-server 相关的错误信息。

    # 实时查看日志
    sudo tail -f /var/log/syslog | grep ppp
  3. 检查服务状态和配置文件

    • 确认 pppoe-server 服务正在运行。
    • 检查 /etc/ppp/pap-secrets 文件中的用户名、密码和服务器名称是否正确,格式是否无误。
    • 检查 /etc/ppp/pptpd-options 文件中的配置,特别是 localipremoteip 是否与你的设置匹配。
  4. 检查客户端配置

    • 确保客户端的用户名和密码完全正确(区分大小写)。
    • 确保客户端没有连接到其他网络或 VPN。
    • 在客户端上,尝试 ping 服务器的静态 IP 地址 (168.100.1)。ping 不通,说明网络层不通,检查物理连接、网线和防火墙。ping 通但无法上网,可能是 DNS 或默认路由问题。

第七步:开机自启

pppoeconf 默认会配置开机自启,你可以验证一下:

systemctl is-enabled pppoe-server

如果输出是 enabled,则表示已设置,如果不是,可以手动启用:

sudo systemctl enable pppoe-server

至此,你已经成功在 Ubuntu 服务器上搭建了一个功能齐全的 PPPoE 服务器!你可以根据需要添加更多用户或调整 IP 地址池。

分享:
扫描分享到社交APP
上一篇
下一篇