Penguin
Note: You are viewing an old revision of this page. View the current version.

Append to a file owned by root

In this example I will use the sources.list file that contains a list of the repositories from which we can get packages.

Firstly:

  • sudo echo "#TEST REPOSITORY" >> /etc/apt/sources.list

Results in a "Permission denied error". This is because anything after the IO redirection characters, is inside a new subshell which is executed as you and not root. ie: sudo only applies to the echo command. Ways around this:

1)

  • sudo bash -c 'echo "#TEST REPOSITORY" >> /etc/apt/sources.list'ers

Commands are read from string after the -c switch.

2)

  • echo "#TEST REPO" | sudo tee -a /etc/apt/sources.list

The 'tee' command reads from standard input and the -a switch appends it to the file.

3)

  • sudo tee -a /etc/apt/sources.list <<< "#TEST REPO"