Annotated edit history of
IORedirectionNotes version 9 showing authors affecting page license.
View with all changes included.
Rev |
Author |
# |
Line |
4 |
BenStaz |
1 |
!Append to a file owned by root |
|
|
2 |
|
|
|
3 |
In this example I will use the ''sources.list'' file that contains a list of the repositories from which we can get packages. |
|
|
4 |
|
|
|
5 |
Firstly: |
|
|
6 |
|
|
|
7 |
*sudo echo "#TEST REPOSITORY" >> /etc/apt/sources.list |
|
|
8 |
|
9 |
LawrenceDoliveiro |
9 |
results in a "Permission denied error". This is because the ''>> /etc/apt/sources.list'' redirection is processed by your shell _before_ spawning <tt>sudo</tt>, so it is trying to open the file with your (ordinary) user privileges, not root privileges. |
8 |
BenStaz |
10 |
|
4 |
BenStaz |
11 |
Ways around this: |
|
|
12 |
|
|
|
13 |
1) |
|
|
14 |
|
|
|
15 |
*sudo bash -c 'echo "#TEST REPOSITORY" >> /etc/apt/sources.list' |
|
|
16 |
|
9 |
LawrenceDoliveiro |
17 |
The shell spawned by <tt>sudo</tt> reads its command from the string after the ''-c'' switch, so the redirection is done in a process which is already running as root. |
4 |
BenStaz |
18 |
|
|
|
19 |
2) |
|
|
20 |
|
|
|
21 |
*echo "#TEST REPO" | sudo tee -a /etc/apt/sources.list |
|
|
22 |
|
9 |
LawrenceDoliveiro |
23 |
The 'tee' command reads from standard input and the ''-a'' switch appends it to the file. Again, the file is opened by the process spawned by <tt>sudo</tt>, which is running as root. |
4 |
BenStaz |
24 |
|
|
|
25 |
3) |
|
|
26 |
|
|
|
27 |
*sudo tee -a /etc/apt/sources.list <<< "#TEST REPO" |
9 |
LawrenceDoliveiro |
28 |
|
|
|
29 |
An alternative formulation of 2), using a [HereString|HereStrings]. |