21

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.

  • 1
    The 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 the cat, for example, echo "# foo" | sudo tee -a /etc/apt/sources.list – user4556274 Sep 28 '16 at 14:23
  • https://stackoverflow.com/questions/82256/how-do-i-use-sudo-to-redirect-output-to-a-location-i-dont-have-permission-to-wr – tirenweb Apr 07 '23 at 09:15

1 Answers1

29

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/.

Byte Commander
  • 107,489
  • I'm not really sure where to go from any of those commands... I'm extremely new to Linux – Steven Brailsford Sep 28 '16 at 14:39
  • Well, 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. – Byte Commander Sep 28 '16 at 14:44
  • Okay but none of them did anything... – Steven Brailsford Sep 28 '16 at 14:45
  • If 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. – Byte Commander Sep 28 '16 at 14:47
  • Okay 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 Sep 28 '16 at 14:52
  • E: Type '$' is not known on line 55 in source list /etc/apt/sources.list E: The list of sources could not be read. – Steven Brailsford Sep 28 '16 at 14:52
  • Then open the file in an interactive editor (like nano, as described above) and fix it. – Byte Commander Sep 28 '16 at 14:54
  • @ByteCommander You might want to add -a to tee since it looks like OP wants to append to the file. – edwinksl Oct 01 '16 at 08:12
  • @edwinksl Oh, thanks! Good catch. I totally missed that. Edited the answer. – Byte Commander Oct 01 '16 at 10:23
  • Are there different origins of the permission denied? I get permission denied when running 'sudo bash -c 'du -h --max-depth=1'. – Kvothe Sep 15 '22 at 16:23