对于许多以太坊爱好者、开发者或希望参与网络验证的节点运营者来说,仅仅在电脑上运行以太坊客户端是不够的,为了确保你的节点能够持续稳定地为以太坊网络做出贡献,并随时同步最新数据,实现开机自动启动是至关重要的一步,本文将为你详细讲解如何配置以太坊客户端,使其在电脑开机后自动启动,无论是Windows、macOS还是Linux系统,都能找到适合你的方法。
在深入配置之前,我们先明确一下为什么这个功能如此重要:
在配置自动启动之前,你必须已经成功安装并运行了一个以太坊客户端,目前主流的客户端包括:
本指南将以最经典的 Geth 客户端为例进行讲解,但其核心思路(即创建一个服务或计划任务)同样适用于其他客户端。

不同操作系统有不同的自动启动机制,下面我们分别介绍在Windows、macOS和Linux上的实现方法。
在Windows上,最推荐的方式是使用“任务计划程序”(Task Scheduler),它功能强大且灵活。
步骤 1:找到你的Geth可执行文件 假设你已经下载了Geth,geth.exe,并了解你常用的启动命令。 geth --http --http.addr "0.0.0.0" --http.port "8545" --syncmode "snap" 这个命令会启动一个支持HTTP API的节点,并使用快速同步模式。
步骤 2:创建基本任务
Win R 键,输入 taskschd.msc 并回车,打开“任务计划程序”。AutoStart Ethereum Node。步骤 3:设置触发器

步骤 4:设置操作
geth.exe 文件并选中。--http --http.addr "0.0.0.0" --http.port "8545" --syncmode "snap"。geth.exe 文件所在的目录路径。步骤 5:完成设置
每次你的Windows电脑启动,Geth就会自动在后台运行了。
在macOS上,我们可以使用 launchd,这是macOS和iOS上用于管理后台服务的守护进程系统,最简单的方式是创建一个 plist 文件。
步骤 1:创建plist文件

打开“终端”(Terminal)。
创建一个plist文件,geth.plist,并放在 ~/Library/LaunchAgents/ 目录下(用户级启动)或 /Library/LaunchDaemons/ 目录下(系统级启动,需要管理员权限),这里我们使用用户级。
nano ~/Library/LaunchAgents/geth.plist
粘贴到文件中,并根据你的实际情况进行修改:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.user.ethereum.geth</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/geth</string> <!-- 请替换为你的geth实际路径 -->
<string>--http</string>
<string>--http.addr</string>
<string>0.0.0.0</string>
<string>--http.port</string>
<string>8545</string>
<string>--syncmode</string>
<string>snap</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
</dict>
</plist> 注意:请务必将 <string>/usr/local/bin/geth</string> 替换为你系统中 geth 的实际路径,你可以使用 which geth 命令来查找。
步骤 2:加载并启动服务
nano(按 Ctrl X,Y,再回车)。launchctl load ~/Library/LaunchAgents/geth.plist
launchctl list | grep geth
每次你的macOS开机或用户登录时,Geth都会自动启动。
在Linux上,最标准、最强大的方式是创建一个 systemd 服务,几乎所有现代Linux发行版(如Ubuntu, Debian, CentOS, Fedora)都使用systemd。
步骤 1:创建服务文件
以root权限或使用 sudo 打开一个文本编辑器,创建一个新的服务文件:
sudo nano /etc/systemd/system/ethereum.service
粘贴到文件中,并进行相应修改:
[Unit] Description=Ethereum Geth Client After=network.target [Service] User=your_username <!-- 替换为你的Linux用户名 --> Type=simple Restart=always RestartSec=5 ExecStart=/usr/local/bin/geth --http --http.addr "0.0.0.0" --http.port "8545" --syncmode "snap" ExecStop=/usr/local/bin/geth --exit [Install] WantedBy=multi-user.target
注意:
User=your_username:请替换为运行Geth的用户。ExecStart:请确保 /usr/local/bin/geth 是你的geth可执行文件的正确路径,并填入你的启动参数。ExecStop:用于优雅地停止Geth进程。步骤 2:启用并启动服务
sudo systemctl daemon-reload
sudo systemctl enable ethereum.service
sudo systemctl start ethereum.service
sudo systemctl status