76

I am new to linux, and just beginning to learn bash. I am using Ubuntu 9.04, and would like to add repositories to /etc/apt/sources.list from the command line. Basically, I would like to do this:

sudo echo "[some repository]" >> /etc/apt/sources.list

However, even when I use sudo, I get this error:

bash: /etc/apt/sources.list: Permission denied

How do I avoid this error?

Matthew
  • 28,056
  • 26
  • 104
  • 170
  • You need to mention 2 things: 1) Are you on the sudoers list, and is that set up correctly? 2) What are the permissions of your /etc/apt/sources.list file? – sykora May 12 '09 at 01:17
  • It's better to use a separate file in `/etc/apt/sources.list.d` rather than modifying `/etc/apt/sources.list`. See https://stackoverflow.com/questions/1584066/append-to-etc-apt-sources-list – Neil Mayhew Jan 08 '21 at 19:04

10 Answers10

142
echo "[some repository]" | sudo tee -a /etc/apt/sources.list

The tee command is called as the superuser via sudo and the -a argument tells tee to append to the file instead of overwriting it.

Your original command failed, as the IO redirection with >> will be done as the regular user, only your echo was executed with sudo.

Calling a sudo subshell like

sudo sh -c 'echo "[some repository]" >> /etc/apt/sources.list'

works, too as pointed out by others.

ghickman
  • 5,893
  • 9
  • 42
  • 51
lothar
  • 19,853
  • 5
  • 45
  • 59
  • Thanks for your answer--for future reference, where would this belong? Or do you mean that it doesn't belong on this site at all? – Matthew May 12 '09 at 02:04
  • 1
    @Matthew As it is not really programming related it probably would be a better fit on serverfault.com once it's open for everybody. Currently it's beta only (http://serverfault.com/beta-access) – lothar May 12 '09 at 03:16
  • 5
    I found this useful as a programmer installing some dependencies; I definitely think that this should be on StackOverflow. – wwwilliam Mar 23 '11 at 14:47
  • see also this answer to a similar question (using sed): [Append text to file from command line without using io redirection](http://stackoverflow.com/a/4640152/311288) – Thomas BDX Jul 17 '13 at 15:10
  • Seems to have a problem when attemmpting it on Amazon VPC/EC2. This is what I get: echo "deb http://deb.theforeman.org/ trusty 1.7" | sudo tee -a /etc/apt/sources.list.d/foreman.list sudo: unable to resolve host ip-10-0-0-91 deb http://deb.theforeman.org/ trusty 1.7 – Dennis Mar 29 '15 at 22:07
  • If after adding new repo you get a missing GPG key error, see this https://askubuntu.com/questions/235880/how-to-fix-gpg-in-updater – user1053510 Mar 25 '20 at 10:10
9

It's better to use a separate file in /etc/apt/sources.list.d rather than modifying /etc/apt/sources.list, as explained in this other answer. (Note that the file name MUST end in .list or it will be ignored.)

However, if you want to create it using echo the issue with permissions remains. You can use tee to create it like this:

echo '[some repository]' | sudo tee /etc/apt/sources.list.d/some-repository.list >/dev/null

or like this:

sudo tee /etc/apt/sources.list.d/some-repository.list >/dev/null <<EOF
[some repository]
EOF

Note that you don't need -a on the tee command (because you're not appending).

You can also create the file somewhere else and then copy it into place with:

sudo cp path/to/some-repository.list /etc/apt/sources.list.d/
Neil Mayhew
  • 14,206
  • 3
  • 33
  • 25
9

The shell processes ">", "<", ">>" etc itself before launching commands. So the problem is that "sudo >> /etc/foo" tries to open /etc/foo for append before gaining privileges.

One way round this is to use sudo to launch another shell to do what you want, e.g.:

sudo sh -c 'echo "[some repository]" >> /etc/apt/sources.list'

Or alternatively:

echo "[some repository]" | sudo sh -c 'cat >> /etc/apt/sources.list'

A simpler approach may simply be to use sudo to launch an editor on the /etc/file :)

araqnid
  • 127,052
  • 24
  • 157
  • 134
3

Following works for me

sudo echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" | sudo tee -a /etc/apt/sources.list.d/10gen.list
Salil
  • 46,566
  • 21
  • 122
  • 156
3

One way to solve this is to do the redirection in a subshell:

sudo sh -c 'echo "[some repository]" >> /etc/apt/sources.list'

That way, the sh process is executed under sudo and therefore has the necessary privileges to open the redirected output to /etc/apt/sources.list.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
1

In Karmic, you can just use the add-apt-repository command, at least for PPAs.

For example:

sudo add-apt-repository ppa:docky
Matthew
  • 28,056
  • 26
  • 104
  • 170
  • add-apt-repository isn't in base, to get it run: `apt-get install software-properties-common` – notpeter Jan 07 '14 at 20:57
  • And seems now to be available in Debian based systems, so is probably the correct way to do it. – Wilf Mar 06 '14 at 19:34
0

Here is solution without using piping, just simple in-place editing:

sudo ex +'$put = \"[some repository]\"' -cwq /etc/apt/sources.list

The ex is equivalent to vi -e.

kenorb
  • 155,785
  • 88
  • 678
  • 743
-1

If you were to switch to the root user the same command will work just fine. This is because sudo elevates the priveleges only for the [echo] command and does not elevate the right side of the output redirection.

sudo su
echo "[some repository]" >> /etc/apt/sources.list

If you do it this way be sure to exit from su so that you are not running unnecessary programs as root (superuser)

Shapeshifter
  • 510
  • 6
  • 11
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Uyghur Lives Matter May 25 '15 at 14:18
  • I tried to better the answer, not sure how it doesn't provide an answer. It would be helpful to know more as to why you came to this conclusion – Shapeshifter May 25 '15 at 15:59
  • Your answer read as just a comment to me instead of answering the question because your question basically just said use Lothar's answer, and if the OP was already a super user, they wouldn't have encountered this issue (misunderstanding of sudo with I/O redirection). – Uyghur Lives Matter May 25 '15 at 17:03
  • I see your point, however my answering is just stating that the OP can run that command as superuser instead of using sudo and it will work. – Shapeshifter May 25 '15 at 17:46
-2

first open or create the file you want to edit it by the following command

1- sudo nano file_name

2- edit the file after it opens

3- ctrl+x

4- press 'Y' to say yes

and you are done.

-2

interesting, 1- remove the file with rm , 2 create the file again with touch, 3 use printf to print formatted, 4 pipe with tee to the file(THIS IS FOR DEBIAN) replace to your tastes and likes

sudo rm /etc/apt/sources.list && sudo touch /etc/apt/sources.list && sudo chmod +rwx /etc/apt/sources.list && sudo printf "deb http://deb.debian.org/debian buster main contrib non-free
deb-src http://deb.debian.org/debian buster main contrib non-free
deb http://deb.debian.org/debian-security/ buster/updates main contrib non-free
deb-src http://deb.debian.org/debian-security/ buster/updates main contrib non-free
deb http://deb.debian.org/debian buster-updates main contrib non-free
deb-src http://deb.debian.org/debian buster-updates main contrib non-free" | sudo tee -a /etc/apt/sources.list