Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 514  / 3 Years ago, tue, may 25, 2021, 7:06:59

Hello I have to write a script with the name setip that can be run like this:


./setip <ip> <mask>

and that must set the IP address to <ip> and the mask to <mask> on the lo1 interface.
Below is the content of my script, but it when I try to run it I get the error
"permission denied".
What is wrong with my script and how should I proceed ?


#! /bin/bash
sudo echo
"
auto lo:1
iface lo:1 inet static
address $1
netmask $2" >/etc/network/interfaces

More From » networking

 Answers
7
sudo echo "  
auto lo:1
iface lo:1 inet static
address $1
netmask $2" | sudo tee /etc/network/interfaces


The last line on your script netmask $2" >/etc/network/interfaces attempts to edit the interfaces file which only root can write to. (Or at least it should be owned by root)

However, sudo echo "" > likely does not work how you want it to.



What it's doing is it is executing echo "" with root permissions, but the > operator is not executing with those permissions.



You may want to reformat your command along the lines of this example. In this case, the command that outputs to the file is being run as root, while the other commands do not have to be. I've shown you an example of that above.


[#22066] Wednesday, May 26, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
listeerrated

Total Points: 354
Total Questions: 112
Total Answers: 100

Location: Guam
Member since Fri, Jun 18, 2021
3 Years ago
;