
Configure a UPS on Linux with NUT
Install and configure Network UPS Tools (NUT) on Linux to detect your UPS, load the right driver, and trigger a safe automatic shutdown when battery runs low.
以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。
Install and configure Network UPS Tools (NUT) on Linux to detect your UPS, load the right driver, and trigger a safe automatic shutdown when battery runs low.
A UPS (Uninterruptible Power Supply) protects servers and workstations from sudden power loss, but hardware alone is not enough. Without software monitoring the UPS, your system will still crash hard when the battery runs out. Network UPS Tools (NUT) is the standard open-source solution: it speaks to hundreds of UPS models, monitors battery state, and triggers a clean shutdown before the battery dies. This guide walks through a single-machine NUT setup — one host talking directly to its own UPS — the most common scenario for a home lab or small server.
NUT has three moving parts you need to understand before touching config files:
For a single machine, all three run on the same host. The UPS connects via USB (most common for consumer and prosumer units) or a serial port.
sudo apt update sudo apt install nut nut-client nut-server
Fedora / RHEL 9 / Rocky Linux
sudo dnf install nut
On RHEL-family systems the package ships both server and client components together.
sudo pacman -S nut
After installation NUT's config files live in /etc/nut/ on all distributions.
Plug in the UPS via USB, then run the scanner. It probes USB and other buses and prints the driver block you need:
sudo nut-scanner -U
Output will look roughly like this (yours will differ):
[nutdev1] driver = "usbhid-ups" port = "auto" vendorid = "051d" productid = "0002" product = "Back-UPS ES 700" vendor = "American Power Conversion"
Copy this block — you will paste most of it into ups.conf. If nut-scanner finds nothing, check that the USB cable is data-capable (not charge-only) and that you ran the command with sudo.
Edit /etc/nut/ups.conf. Replace any placeholder content with your detected block, giving the UPS a short name (here, myups):
sudo nano /etc/nut/ups.conf
[myups] driver = usbhid-ups port = auto desc = "APC Back-UPS ES 700"
If nut-scanner returned vendorid and productid, add them too — they prevent NUT from grabbing the wrong USB device if you ever connect multiple UPS units.
[myups] driver = usbhid-ups port = auto vendorid = 051d productid = 0002 desc = "APC Back-UPS ES 700"
Common drivers: usbhid-ups covers most USB HID-compliant units (APC, Eaton, CyberPower); blazer_usb covers many cheaper USB units from Mecer, Voltronic, and rebrands; nutdrv_qx covers Q-series protocol units. The NUT hardware compatibility list at networkupstools.org maps models to drivers.
This single file tells NUT what role this machine plays:
sudo nano /etc/nut/nut.conf
MODE=standalone
standalone means one machine, one UPS, no network slaves. Use netserver only if other machines will monitor this UPS over the network.
upsd.conf controls what interfaces upsd listens on. For a standalone setup, localhost is sufficient:
sudo nano /etc/nut/upsd.conf
LISTEN 127.0.0.1 3493 LISTEN ::1 3493
Then define at least one user in upsd.users. You need an admin-type user for upsmon to authenticate with:
sudo nano /etc/nut/upsd.users
[upsmon_local] password = changeme_strong_password upsmon master
Pick a real password. Even though this only listens on localhost, it is still a credential stored in a plain-text file — restrict its permissions:
sudo chmod 640 /etc/nut/upsd.users sudo chown root:nut /etc/nut/upsd.users
Configure the Monitor — upsmon.conf
upsmon is what actually calls shutdown when the battery hits a critical level:
sudo nano /etc/nut/upsmon.conf
The critical line is MONITOR. Its format is upsname@host powervalue username password type:
MONITOR myups@localhost 1 upsmon_local changeme_strong_password master
MINSUPPLIES 1 SHUTDOWNCMD "/sbin/shutdown -h +0" NOTIFYFLAG ONLINE SYSLOG+WALL NOTIFYFLAG ONBATT SYSLOG+WALL NOTIFYFLAG LOWBATT SYSLOG+WALL NOTIFYFLAG COMMOK SYSLOG NOTIFYFLAG COMMBAD SYSLOG POLLFREQ 5 POLLFREQALERT 5 HOSTSYNC 15 DEADTIME 25 RBWARNTIME 43200 NOCOMMWARNTIME 300 FINALDELAY 5
SHUTDOWNCMD is what runs when the battery is critically low. /sbin/shutdown -h +0 halts immediately. Adjust to shutdown -h +2 if you want a two-minute grace window. The powervalue 1 means this host needs 1 power supply from this UPS to stay alive — correct for a standard desktop or server.
sudo systemctl enable --now nut-server nut-monitor
On Debian/Ubuntu you may also need:
sudo systemctl enable --now nut-driver@myups
Fedora / RHEL / Rocky
sudo systemctl enable --now ups nut-server nut-monitor
The RHEL family uses a ups target that groups the driver and server. If the service name differs on your release, use systemctl list-units 'nut*' to see what is available.
upsc myups@localhost
You should see a wall of variables — the important ones:
OB means on battery, LB means low battery (shutdown imminent). Check that upsmon is connected:
sudo upsc myups@localhost ups.status sudo systemctl status nut-monitor
To test driver communication without triggering a real shutdown, briefly unplug the UPS mains cable and watch the status flip to OB in the output of a looping upsc:
watch -n 2 upsc myups@localhost ups.status
Troubleshooting
Driver fails to start — permission denied on USB device
The nut user needs access to the USB device node. On most distros the NUT package ships a udev rule at /lib/udev/rules.d/62-nut-usbups.rules that handles this. If it is missing, reload udev and replug the UPS:
sudo udevadm control --reload-rules sudo udevadm trigger
upsc returns "Error: Driver not connected"
The driver has not started or crashed. Check its journal:
sudo journalctl -u nut-driver@myups -n 50
Common causes: wrong driver = value in ups.conf, typo in the UPS name, or a USB permission issue.
If upsd is not reachable, upsmon logs COMMBAD but will not shut down until DEADTIME seconds have elapsed. Check that nut-server is running and that the credentials in upsmon.conf match upsd.users exactly — including case.
Check that MINSUPPLIES is set to 1 and that upsc myups@localhost ups.status shows OL before starting services. If NUT cannot reach the UPS at startup it may treat it as a lost supply and trigger shutdown.
Configure Prometheus Alertmanager with routing trees, receivers, inhibition rules, grouping, Go templates, and PagerDuty/Slack on-call integrations.
Set up a complete small-office intranet on one Linux box: Nginx web server, dnsmasq local DNS, Samba file sharing, and a Wiki.js team wiki.
Build a complete nftables firewall from scratch: tables, chains, sets, default-deny input policy, service allowlisting, and persistent systemd configuration.
Set up Caddy as a reverse proxy with automatic HTTPS, load balancing, WebSocket passthrough, reusable snippets, and header control — no certbot required.
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。