Getting "Permission denied" even when using sudo command
You are running the cat
command as root, but the output redirection takes place in your current shell which runs as your normal user account.
You have at least three options to achieve your goal of adding lines to your apt
sources:
Running the whole command including output redirection in a separate Bash root shell:
sudo bash -c 'cat >> /etc/apt/sources.list'
Using the
tee
command that copies output to a file (-a
option to append to instead of overwrite existing files) and running that as root:cat | sudo tee -a /etc/apt/sources.list
Using a terminal editor application like
nano
as root to modify the file:sudo nano /etc/apt/sources.list
However, it is recommended to leave your /etc/apt/sources.list
file as it is and add additional sources by creating new *.list
files inside /etc/apt/sources.list.d/
.
Related videos on Youtube
Steven Brailsford
Updated on September 18, 2022Comments
-
Steven Brailsford over 1 year
I'm attempting to add a file to my
/etc/apt/sources.list.d
folder and for some reason despite using sudo it still says permission denied. Here's the code I'm trying to run.$ sudo cat >> /etc/apt/sources.list
I've also tried simply using gedit but that doesn't work either.
-
user4556274 over 7 yearsThe
sudo
applies only to the command (cat
) not to opening the file via the redirection operator. To append to the restricted file, you need to elevate privilege on the file operation, not thecat
, for example,echo "# foo" | sudo tee -a /etc/apt/sources.list
-
-
Steven Brailsford over 7 yearsI'm not really sure where to go from any of those commands... I'm extremely new to Linux
-
Byte Commander over 7 yearsWell, the first two do exactly the same as what you had, just that the correct part is running as root. The third command opens
nano
, a simple command-line editor. You can try it and see if you like it. Just use whichever method you like most. -
Steven Brailsford over 7 yearsOkay but none of them did anything...
-
Byte Commander over 7 yearsIf you run any variant of
cat >> /path/to/file
, it will just wait for input and let you enter text, until you exit wit CTRL+D. Then all the lines you entered will be saved to the specified file. There will be no prompt or output though. -
Steven Brailsford over 7 yearsOkay new problem... I think I somehow accidentally added too much to the file because now when I'm trying to run the other commands to install it, I'm getting this...
-
Steven Brailsford over 7 yearsE: Type '$' is not known on line 55 in source list /etc/apt/sources.list E: The list of sources could not be read.
-
Byte Commander over 7 yearsThen open the file in an interactive editor (like
nano
, as described above) and fix it. -
edwinksl over 7 years@ByteCommander You might want to add
-a
totee
since it looks like OP wants to append to the file. -
Byte Commander over 7 years@edwinksl Oh, thanks! Good catch. I totally missed that. Edited the answer.