如何读取/var/lib/dhcpcd/interface.lease文件?为何读取时出现乱码?
interface.lease file shows garbled output The short answer is: dhcpcd stores lease information in a binary format, unlike dhclient which uses plain-text lease files. That's why when you run cat interface.lease, you see a mix of unreadable binary data and occasional plain-text strings (like domain and hostname—those are stored as human-readable strings within the binary structure).
Why does dhcpcd use binary instead of plain text?
Binary files are faster for dhcpcd to read and write, which is critical for quickly acquiring or renewing leases without unnecessary parsing overhead. This is a deliberate design choice compared to dhclient's human-readable format.
How to view your lease information properly
Instead of trying to read the binary lease file directly, use dhcpcd's built-in tools to get the lease details in a readable format:
Get current lease variables for an interface:
Run this command (replaceeth0with your actual network interface name):dhcpcd -U eth0This will output key-value pairs like:
ip_address=192.168.1.100 subnet_mask=255.255.255.0 router=192.168.1.1 domain_name=local hostname=my-deviceDump the contents of a lease file:
If you specifically need to read the lease file itself, use dhcpcd's--dumpleaseoption:dhcpcd --dumplease /var/lib/dhcpcd/interface.leaseThis will parse the binary file and output its contents in a human-readable way.
Summary
You don't need to switch back to dhclient—dhcpcd provides all the tools you need to access lease information without dealing with the binary lease file directly. The garbled output you're seeing is just the raw binary data that dhcpcd uses internally, which isn't meant to be read with standard text commands like cat.
内容的提问来源于stack exchange,提问作者Special Delivery

