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 sudo spawns a new subshell and doesn't get the >> /etc/apt/sources.list because it is owned by the parent shell which is you.

Ways around this:

1)

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

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"