The problem with perl -i and sed -i is that they try to create a backup/temporary file in /etc, and $USER doesn't have write permission on /etc. You could:
cp /etc/myfile $HOME/myfile sed -i ... $HOME/myfile cp $HOME/myfile /etc/myfile This will leave the backup file in $HOME. Backup files (old configs) don't belong in /etc.
Or, if you set up the VISUAL (for GUI terminals) and EDITOR (for non-GUI terminals) environment variables to point to your favorite editor (in ~/.bashrc), you can use the sudoedit command to edit files to which $USER has no normal access.
After sudo authentication, sudoedit does it thusly:
- As
root,sudoeditmakes a temporary copy of the file-to-be-edited. - As
$USER, invokes$VISUALin the GUI environment,$EDITORin non-GUI, on the temporary file. - As
root, copies the temporary file back to the file-to-be-edited, if the editor succeeded.