Jan 5, 2009 0
Run ifconfig as a non-root user
Recently I needed to extract the ip number of the box on which my script was running, and had to decide the behaviour of tool.
Upon digging around, worked out a command snippet that worked, as given below.
ifconfig eth0|sed -n "/inet addr:.*/{s/.*inet addr://; s/ .*//; p}"
But the problem was ifconfig only works as a `root` user if you try it as a normal user you would get a command not found.
$ ifconfig -bash: ifconfig: command not found
The problem is the `sbin` folder under which the `ifconfig` command resides is not added to the`PATH` environment variable by default.
all you have to do is the following.
$ export PATH=$PATH:/sbin $ echo $PATH /usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/raja/bin:/sbin
Now as we have added the `sbin` folder to the `PATH` variable, try the command again it will work.
You don’t want to set the path each time you login. You can add it to your `.bashrc` file in your home folder to update the path
automatically each time you login.
Here is how you do it.,
$ vi ~/.bashrc
add the following line at the end of the file.
export PATH=$PATH:/sbin
Save and close the file. From the next time you log-in the `sbin` folder will be available to your `PATH` variable.
In case you need to reload the .bashrc file immediately issue the following command
$ source ~/.bashrc
NOTE : It works only on a readonly mode. Trying to set the interface parameters as a non root user will generate error.
$ ifconfig eth0 192.168.0.1 SIOCSIFADDR: Permission denied SIOCSIFFLAGS: Permission denied