You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何用单行命令修改/etc/ntp.conf中的服务器配置?

One-Liner Methods to Modify Server Entries in /etc/ntp.conf

Absolutely! Just like you handle SSH config tweaks with single commands, there are straightforward one-liners to update the server settings in /etc/ntp.conf. Here are the most practical approaches:

Replace All Existing Server Entries

If you want to overwrite every server line with a new NTP server (e.g., pool.ntp.org), use sed with in-place editing:

sudo sed -i 's/^server .*/server pool.ntp.org iburst/' /etc/ntp.conf
  • sudo is required since /etc/ntp.conf is a system-owned file
  • -i edits the file in place (no need for temp files)
  • ^server .* matches any line starting with server
  • The replacement string sets your new server with the iburst flag for faster initial sync

Comment Out Old Servers & Add a New One

If you prefer to keep old configs commented instead of deleting them, use this two-part sed command:

sudo sed -i -e '/^server/s/^/#/' -e '$a server pool.ntp.org iburst' /etc/ntp.conf
  • -e '/^server/s/^/#/' adds a # at the start of every server line to comment them out
  • -e '$a server pool.ntp.org iburst' appends the new server line to the end of the file

Replace a Specific Server Only

If you just need to swap one particular old server for a new one (e.g., replace old.ntp.example.com with new.ntp.example.com):

sudo sed -i 's/^server old.ntp.example.com.*/server new.ntp.example.com iburst/' /etc/ntp.conf

Alternative: Using awk

If you're more comfortable with awk, this command achieves the same "replace all server lines" effect:

awk '/^server/ {print "server pool.ntp.org iburst"; next}1' /etc/ntp.conf > temp.conf && sudo mv temp.conf /etc/ntp.conf
  • /^server/ {print ...; next} replaces matching lines with your new server entry
  • 1 prints all non-matching lines as-is
  • We redirect to a temp file first, then overwrite the original (since awk doesn't support in-place editing natively)

Important Post-Modification Step

After updating the config, don't forget to restart the NTP service to apply changes:

# For systemd-based distros (Ubuntu 16.04+, Debian 9+, etc.)
sudo systemctl restart ntp

# For SysVinit-based distros
sudo service ntp restart

内容的提问来源于stack exchange,提问作者Jasmine Lognnes

火山引擎 最新活动