LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 06-04-2020, 03:09 PM   #1
Trent29
Member
 
Registered: Dec 2018
Posts: 44

Rep: Reputation: Disabled
Question 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?
 
Old 06-04-2020, 05:07 PM   #2
Trent29
Member
 
Registered: Dec 2018
Posts: 44

Original Poster
Rep: Reputation: Disabled
Here's the Code

Code:
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:/#
 
Old 06-04-2020, 05:30 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 24,138

Rep: Reputation: 5344Reputation: 5344Reputation: 5344Reputation: 5344Reputation: 5344Reputation: 5344Reputation: 5344Reputation: 5344Reputation: 5344Reputation: 5344Reputation: 5344
iwconfig command is not in a regular users path environment.

Using su still uses the regular users path but if using su - instead will switch to roots.

Using sudo will run the command as root using roots environment.

Last edited by michaelk; 06-04-2020 at 05:32 PM.
 
2 members found this post helpful.
Old 06-04-2020, 05:41 PM   #4
Crippled
Member
 
Registered: Sep 2015
Distribution: MX Linux 21.3 Xfce
Posts: 581

Rep: Reputation: Disabled
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.
 
Old 06-04-2020, 10:40 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,035
Blog Entries: 23

Rep: Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984
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
 
3 members found this post helpful.
Old 06-05-2020, 02:16 PM   #6
Trent29
Member
 
Registered: Dec 2018
Posts: 44

Original Poster
Rep: Reputation: Disabled
Thumbs up

Thanks for the great explanations.

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:/#
 
Old 06-05-2020, 02:48 PM   #7
sevendogsbsd
Senior Member
 
Registered: Sep 2017
Distribution: FreeBSD
Posts: 2,252

Rep: Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010
Correct. You are simply changing the current directory; that doesn't affect the environment.
 
2 members found this post helpful.
Old 06-05-2020, 02:49 PM   #8
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,586

Rep: Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184Reputation: 2184
Quote:
Originally Posted by Trent29 View Post
Thanks for the great explanations.

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.
 
1 members found this post helpful.
Old 06-05-2020, 03:05 PM   #9
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,578

Rep: Reputation: Disabled
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.

Last edited by shruggy; 06-05-2020 at 03:29 PM.
 
2 members found this post helpful.
Old 06-05-2020, 03:15 PM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 24,138

Rep: Reputation: 5344Reputation: 5344Reputation: 5344Reputation: 5344Reputation: 5344Reputation: 5344Reputation: 5344Reputation: 5344Reputation: 5344Reputation: 5344Reputation: 5344
Quote:
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.

Last edited by michaelk; 06-05-2020 at 03:50 PM.
 
3 members found this post helpful.
Old 06-05-2020, 03:35 PM   #11
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,578

Rep: Reputation: Disabled
@michaelk. Thanks for correcting me. You're right of course.
 
1 members found this post helpful.
Old 06-05-2020, 04:22 PM   #12
Trent29
Member
 
Registered: Dec 2018
Posts: 44

Original Poster
Rep: Reputation: Disabled
Thumbs up

Wow, thanks for the quick and extraordinarily detailed explanations.

Much appreciated and makes sense.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about the sudo command, specifically how to have sudo act as if user is root slacker_ Linux - Newbie 17 09-22-2013 03:48 PM
'sudo ls /root/monitor/' outputs, 'sudo ls /root/monitor/*' does not stf92 Slackware 10 07-19-2012 05:20 PM
Why am I told I'm logged out after I've logged in? Airidh LQ Suggestions & Feedback 1 02-25-2011 10:35 AM
kde much slower to start when logged in as alan than logged in as root arubin Slackware 0 04-26-2004 04:27 PM
mozilla works fine when logged in as a user but crashes when logged in as root jimi Linux - General 6 04-02-2003 08:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration