Editing configuration files

Andrew J. Caines A.J.Caines at halplant.com
Sun Dec 2 05:14:07 UTC 2018


Ian,
> What is the preferred way to edit text configuration files that require
> root access?

As others have mentioned, there are wrappers for some configs which
provide degrees of safety. My preferred method for making simple
substitution or addition changes with minimal foot-shooting opportunity
is with sed, e.g.

Show the line(s) to change

$ sed -n '/\/tmp/p' /etc/fstab
tmpfs	/tmp	tmpfs	defaults,size=10%	0 0

Show how they will look after the change

$ sed -n '/\/tmp/s/10/25/p' /etc/fstab
tmpfs	/tmp	tmpfs	defaults,size=25%	0 0

Make the change

$ sudo sed -i '/\/tmp/s/10/25/' /etc/fstab

See the new line(s)

$ sed -n '/\/tmp/p' /etc/fstab
tmpfs	/tmp	tmpfs	defaults,size=25%	0 0

This also has the advantage of leaving no swap files, history, configs
or other unintended files owned by root. It also puts an explicit record
of the change conveniently in your shell history which may allow you to
easily revert the change, e.g.

$ sudo sed -i '/\/tmp/s/25/10/' /etc/fstab


-- 
-Andrew J. Caines-   Unix Systems Architect   A.J.Caines at halplant.com
  "Machines take me by surprise with great frequency" - Alan Turing




More information about the ubuntu-users mailing list