1

I am trying to make a bash script that will set up a proxy on my computer running ubuntu studio. This [1] tells me that I should set up the proxies for apt-get and Update Manager by creating a file 95proxies in /etc/apt/apt.conf.d/.

the problem is when I run this code.

sudo echo "Acquire::http::proxy "http://myproxy.server.com:8080/";
Acquire::ftp::proxy "ftp://myproxy.server.com:8080/";
Acquire::https::proxy "https://myproxy.server.com:8080/";
" > /etc/apt/apt.conf.d/95proxies

I get:

bash: /etc/apt/apt.conf.d/95proxies: Permission denied

I was able to create the file with touch by

sudo touch /etc/apt/apt.conf.d/95proxies

but when I go to put data in the file I still get the error message above.

[1] https://askubuntu.com/questions/150210/how-do-i-set-systemwide-proxy-servers-in-xubuntu-lubuntu-or-ubuntu-studio

Community
  • 1
  • 1
Andrew Pullins
  • 496
  • 1
  • 14
  • 24

3 Answers3

1

This is essentially the same problem as described in "How do I use sudo to redirect output to a location I don't have permission to write to?". That is, even though you're running echo with sudo, it's not echo that's writing to the file, it's your shell, and your shell doesn't have permission to write to that file.

One possible solution would be to use sudo with a program such as tee that actually does write to the file itself rather than relying on the shell:

echo "Acquire::http::proxy \"http://myproxy.server.com:8080/\";" | sudo tee /etc/apt/apt.conf.d/95proxies > /dev/null

For more possible solutions to this problem, see How do I use sudo to redirect output to a location I don't have permission to write to?

Community
  • 1
  • 1
Ajedi32
  • 45,670
  • 22
  • 127
  • 172
0

Try the below command,

sudo sh -c '(echo "Acquire::http::proxy \"http://myproxy.server.com:8080/\";"; echo "Acquire::ftp::proxy \"ftp://myproxy.server.com:8080/\";"; echo "Acquire::https::proxy \"https://myproxy.server.com:8080/\";") > /etc/apt/apt.conf.d/95proxies'

Because of the directory /etc/apt/apt.conf.d owned by root, you have to run the echo command in a separate subshell.

To add multiple lines to a file through echo command, the syntax would be

(echo first line; echo second line) >> output file
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0
sudo printf 'Acquire::http::proxy "http://myproxy.server.com:8080/;\nAcquire::ftp::proxy "ftp://myproxy.server.com:8080/;\nAcquire::https::proxy "https://myproxy.server.com:8080/;\n"' > /etc/apt/apt.conf.d/95proxies

To print new line character (\n) in a file you must use printf not echo.

In your case bash will use each line of the command as a new command (because of ;, so > /etc/apt/apt.conf.d/95proxies will be executed as a command without sudo.

agilob
  • 6,082
  • 3
  • 33
  • 49