[SOLVED] Why do I have to use sudo before iwconfig when logged in as root in Debian 10?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Why do I have to use sudo before iwconfig when logged in as root in Debian 10?
iwconfig gives error message unless I use sudo command. I understand that this is normal if I'm logged in as a regular user, but it happens when I'm root as well. Why is Debian requiring me to use the sudo command for iwconfig when I'm logged in as root and how would I fix this?
user@debian:~$ iwconfig
bash: iwconfig: command not found
user@debian:~$ sudo iwconfig
[sudo] password for user:
lo no wireless extensions.
eth0 no wireless extensions.
wlan0 IEEE 802.11 ESSID:off/any
Mode:Managed Access Point: Not-Associated Tx-Power=off
Retry short limit:7 RTS thr:off Fragment thr:off
Encryption key:off
Power Management:off
user@debian:~$ su
Password:
root@debian:/home/user# iwconfig
bash: iwconfig: command not found
root@debian:/home/user# sudo iwconfig
lo no wireless extensions.
eth0 no wireless extensions.
wlan0 IEEE 802.11 ESSID:off/any
Mode:Managed Access Point: Not-Associated Tx-Power=off
Retry short limit:7 RTS thr:off Fragment thr:off
Encryption key:off
Power Management:off
root@debian:/home/user# cd /
root@debian:/# iwconfig
bash: iwconfig: command not found
root@debian:/# sudo iwconfig
lo no wireless extensions.
eth0 no wireless extensions.
wlan0 IEEE 802.11 ESSID:off/any
Mode:Managed Access Point: Not-Associated Tx-Power=off
Retry short limit:7 RTS thr:off Fragment thr:off
Encryption key:off
Power Management:off
root@debian:/#
Because the command line is counter intuitive gibberish. My advice for you is don't try to make sense of it because no one who is sane or not on drugs can.
As michaelk has pointed out, when you use su to become root, you are still using the environment of the regular user and that environment does not include the PATH to reach it (typically /sbin/iwconfig, may be different on your debian). Using su - (su followed by a dash) switches to the normal login environment of the user you are switching to, root if no other name is given.
From man su:
Code:
DESCRIPTION
The su command is used to become another user during a login session. Invoked without a username, su
defaults to becoming the superuser. The optional argument - may be used to provide an environment
similar to what the user would expect had the user logged in directly.
Some people choose to not avail themselves of the added power and ease of use provided by the shell prompt which is an integral, necessary and intended feature of the underlying Unix-like design philospohy of GNU/Linux. You are always advised to understand how the system was intended to work (it is not difficult, and certainly not gibberish) and doing so will make your Linux experience much more enjoyable.
Users who have nothing useful to contribute should refrain from commenting.
Last edited by astrogeek; 06-04-2020 at 10:44 PM.
Reason: tpoys
So then, does using cd / only change the directory and not the environment, even though the prompt changes from root@debian:/home/user# to root@debian:/# ?
Code:
root@debian:/home/user# cd /
root@debian:/# iwconfig
bash: iwconfig: command not found
root@debian:/# sudo iwconfig
lo no wireless extensions.
eth0 no wireless extensions.
wlan0 IEEE 802.11 ESSID:off/any
Mode:Managed Access Point: Not-Associated Tx-Power=off
Retry short limit:7 RTS thr:off Fragment thr:off
Encryption key:off
Power Management:off
root@debian:/#
So then, does using cd / only change the directory and not the environment, even though the prompt changes from root@debian:/home/user# to root@debian:/# ?
[
Yes. cd does not change the environment. Also cd by itself will switch to the logged in user’s home directory, which is /root for root, not /
cd - will change the environment and switch to the /root directory. You can see where you are with the pwd command...but again, which directory you’re in says nothing about the environment.
When you issue a command without path it's searched in the current directory, then in directories pointed to by environment variable PATH.
When you change to the filesystem root (/), iwconfig is not there. So it's searched on PATH. PATH for regular users doesn't include /sbin directory where iwconfig is located.
When you gain root privileges via su or sudo -s, startup scripts for root (/etc/profile, /root/.profile, /root/.bashrc, etc.) are not executed. Those scripts among other things set environment. For the root user, they amend PATH so that it includes /sbin and /usr/sbin. iwconfig is not found because it's not on PATH. But you can invoke it with full path specified: /sbin/iwconfig. But sudo -s is different from su, see below.
OTOH, when you gain root privileges via su - or sudo -i it's the same as if you were logging in as root: all startup scripts for root are executed and /sbin and /usr/sbin get included on PATH.
When you execute a single command via sudo, a minimal environment is set anew by sudo. This includes PATH.
And here is where the difference between su and sudo -s comes into play. sudo -s uses settings in /etc/sudoers just like sudo command does. Because /etc/sudoers on Debian sets PATH (see secure_path in /etc/sudoers), iwconfig gets found even though the startup scripts for root weren't executed.
When you issue a command without path it's searched in the current directory, then in directories pointed to by environment variable PATH.
That is true for Windows but not linux. That is why you need the ./ (or use absolute path) if you want to run a program that is not located in a directory in your path environment.
shruggy is correct in regards that environment does not matter if using the absolute path to run the command i.e /sbin/iwconfig.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.