skip to main | skip to sidebar Linux Commando This blog is about the Linux Command Line Interface (CLI), with an occasional foray into graphical user interface territory. Instead of just giving you information like some man page, I hope to illustrate each command in real-life scenarios. Tuesday, May 20, 2008 Run ifconfig as non-root user for read-only access to network interfaces It is a frequent scenario that you are logged in to the console of a Linux system, and you need to know its IP address. If you are the root user, that is easy: $ ifconfig eth0 Link encap:Ethernet HWaddr 00:0B:6B:E1:BC:14 inet addr:192.168.0.103 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::20b:6aff:fed0:bb04/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:8100 errors:0 dropped:0 overruns:0 frame:0 TX packets:7727 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:5385440 (5.1 MiB) TX bytes:1454259 (1.3 MiB) Interrupt:177 Base address:0xdc00 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:68 errors:0 dropped:0 overruns:0 frame:0 TX packets:68 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:5204 (5.0 KiB) TX bytes:5204 (5.0 KiB) However, if you are not root.... $ ifconfig bash: ifconfig: command not found At this point, you are probably ready to give up. Don't: there is always hope. A not well-publicized fact is that the ifconfig command is executable by anyone: it is just NOT on the default PATH for non-root users. To find out where ifconfig is: $ whereis ifconfig ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz Is it true that anyone can run ifconfig? $ ls -l /sbin/ifconfig -rwxr-xr-x 1 root root 66024 Aug 12 2006 /sbin/ifconfig The answer is yes. To run ifconfig, /sbin needs to be on your PATH, is it? $ echo $PATH /usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/opt/cdk4msp/bin:/home/peter/bin No, afraid not. No wonder you cannot run the ifconfig command. It is straight-forward to append that to your PATH. $ export PATH=$PATH:/sbin $ echo $PATH /usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/opt/cdk4msp/bin:/home/peter/bin:/sbin Let's give ifconfig another try. $ ifconfig eth0 Link encap:Ethernet HWaddr 00:0B:6B:E1:BC:14 inet addr:192.168.0.103 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::20b:6aff:fed0:bb04/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:8590 errors:0 dropped:0 overruns:0 frame:0 TX packets:8218 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:5530730 (5.2 MiB) TX bytes:1509759 (1.4 MiB) Interrupt:177 Base address:0xdc00 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:68 errors:0 dropped:0 overruns:0 frame:0 TX packets:68 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:5204 (5.0 KiB) TX bytes:5204 (5.0 KiB) To save some typing, you can combine the setting of the PATH, and the ifconfig command as follow: $ PATH=$PATH:/sbin ifconfig Now, non-root users are happy. Note that only non-root users can only get/read interface data, but not set/write it. Setting interface parameters as a non-root user will generate errors: $ ifconfig eth0 192.168.0.155 SIOCSIFADDR: Permission denied SIOCSIFFLAGS: Permission denied 3diggsdiggStumbleUpon Toolbar Posted by Peter Leung at 8:16 PM 9 comments: Benji W said... ifconfig was deprecated in kernels newer than 2.2.x See http://www.linux-foundation.org/en/Net:Iproute2 http://en.wikipedia.org/wiki/Ifconfig You can use "ip" instead ("ip a" lists interfaces). ip is in the default path, at least on suse. May 21, 2008 12:18 AM Peter Leung said... Thanks, benji. ip will do the job. On my Centos 4, /sbin/ip runs into the same problem as ifconfig: /sbin is not on the default path. Same solution. May 21, 2008 8:21 AM Anonymous said... I didn't know you can call ifconfig from the user level. As I was looking for such a solution, I looked into using 'ping'. This works: ping -c 1 `hostname` As I was playing around with dnsmasq and try to test it, I learned that the 'hostname' command has the '-i' option to retrieve once own IP address. Cheers, Rainer May 26, 2008 3:59 AM Peter Leung said... hostname -i certainly works. And in my opinion, even simpler than the ifconfig idea. Thanks Peter May 26, 2008 8:12 AM Anonymous said... found some good ifconfig examples March 24, 2009 1:38 PM doncbex said... Adding sbin to the PATH may be too much just for ifconfig. You could simply make a softlink to it in a directory in your path, for example: # ln -s /sbin/ifconfig /usr/bin/ifconfig Keep up with the good work! ^^ February 12, 2010 2:21 AM dare to solve puzzle said... thanks, really good info, helped me play with ifconfig... October 12, 2010 1:37 AM dare to solve puzzle said... @doncbex as a non-root user it says "permission denied" @anonymous it gives localhost or 127.0.0.1 instead of giving ip address.. October 12, 2010 2:25 AM Bharath said... It's very nice. Very helpful. January 25, 2012 9:56 PM Post a Comment Newer Post Older Post Home Subscribe to: Post Comments (Atom) Followers Subscribe in a reader Enter your email address: Delivered by FeedBurner CONTACT ME: commandolinux@gmail.com Popular Pages Today 1. Linux Commando: How to count number of files in a directory 19.39% 2. Linux Commando: How to disable SSH host key checking 16.92% 3. Linux Commando: Using sed to extract lines in a text file 11.28% 4. Linux Commando: How to check the exit status code 10.81% 5. Linux Commando: Compare Directories using Diff in Linux 10.11% 6. Linux Commando: Show progress during dd copy 9.75% 7. Linux Commando: How To Mount USB flash drive from Command Line 7.76% 8. Linux Commando: How to Display Routing Table 4.82% 9. Linux Commando: Using awk to extract lines in a text file 4.82% 10. Linux Commando: How to find and delete all hard links to a file 4.35% [Click to Get Popular Pages for your Blog or Website] Blog Archive * ? 2010 (1) o ? April (1) + How to insert a file at a specific line and column... * ? 2009 (9) o ? November (1) + Fun with Date Arithmetic o ? July (2) + How to move print jobs from one printer queue to a... + Search and replace line feeds using emacs o ? June (1) + loook: A light-weight text search tool for OpenOff... o ? May (5) + Is PHP supported on my Web Server? + More on Inserting Arguments from Previous Commands... + Open a file from the command line using its defaul... + Find all (non-)empty files in a directory + A surefire shortcut to Insert the Last Argument of... * ? 2008 (51) o ? December (1) + Auto Start Applications at Login to GNOME Desktop o ? November (3) + How to increase number of disk mounts before next ... + How to open and close the CD DVD tray + Two additional ways to tail a log file o ? October (1) + How to disable SSH host key checking o ? September (3) + Upgrade individual packages for Debian-based syste... + How to get the process start date and time + How to find and delete all hard links to a file o ? August (3) + How I repaired a corrupted grub menu.lst config fi... + How to show apt log history + Learn more about a command when no man info page i... o ? July (3) + Pondus: A personal weight management software + How to do reverse DNS lookup + How to count number of files in a directory o ? June (7) + Dual pane Linux file managers: mc and emelfm + Run emacs in batch mode to byte-compile elisp file... + Create File of a Given Size ... with random conten... + Smart case-insensitive, incremental search using v... + How to find a file and cd to its dirname using com... + Show progress during dd copy + How to number each line in a text file on Linux o ? May (12) + How to disable vim syntax highlighting and colorin... + Use the OR operator in grep to search for words an... + Root edit a file using emacs in the same session + Delete Windows/DOS carriage return characters from... + Run ifconfig as non-root user for read-only access... + Ping or nmap to identify machines on the LAN + How to indent lines in text files using sed, awk, ... + How to convert text files to all upper or lower ca... + How to prevent Linux man pages from clearing after... + bash quicksand 2: Quotes needed in string tests + How to Display Routing Table + Compare Directories using Diff in Linux o ? April (5) + Using awk to extract lines in a text file + Fast way to execute sequential commands from the c... + Quick hex / decimal conversion using CLI + Extracting columns and fields from a text file + Use sed or perl to extract every nth line in a tex... o ? March (7) + bash quicksand 1: whitespaces in variable assignme... + Trick grep not to report itself in a process searc... o ? February (5) o ? January (1) * ? 2007 (21) o ? December (3) o ? November (8) o ? October (10)