Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: Terminal Path Command - echo $PATH - What Do You Know About Your PATH?

  1. #1
    Join Date
    Mar 2010
    Location
    Austin - Texas
    Posts
    10,140
    Blog Entries
    48

    Lightbulb Terminal Path Command - echo $PATH - What Do You Know About Your PATH?

    So I have been wondering just what might be going on that some commands work as a normal user while others only work as root and still others only work as a certain type of root. Also, I might add that any time we provide a command to someone else that requires you to be root, we need to indicate the kind of root command, because there is a difference. (The issue of needing to be root has to do with system wide modifications and the need for tight security, a subject that is outside the scope of this message today.)

    For instance look at the terminal command:

    Code:
    fdisk -l
    Absolute path to 'fdisk' is '/sbin/fdisk', so running it may require superuser privileges (eg. root).
    Open a terminal session and type this command and you are told of its location and that you might need to be root to get it to run. Humm, OK then how about this:

    Code:
    sudo fdisk -l
    root's password:
    sudo: fdisk: command not found
    Still not working. So what do you think our unknowing users are thinking? What about this command:

    Code:
    su
    Password:
    fdisk -l
    And Bam! I get my disk listing just as I was requesting. Humm, why does su work and sudo does not? Perhaps it has something to do with your path. So, I did a test using echo $PATH as a normal user and as different kinds of root and here is what I found:

    Code:
    Using echo $PATH
    
    With or Without using sudo (echo $PATH<enter> or sudo echo $PATH<enter>)
    
    01) /usr/lib64/mpi/gcc/openmpi/bin
    02) /home/james/bin
    03) /usr/local/bin
    04) /usr/bin
    05) /bin
    06) /usr/bin/X11
    07) /usr/X11R6/bin
    08) /usr/games
    09) /opt/kde3/bin
    10) /usr/lib64/jvm/jre/bin
    11) /usr/sbin
    12) /usr/sbin
    
    su (su<enter> and echo $PATH<enter>)
    
    01) /usr/lib64/mpi/gcc/openmpi/bin
    02) /home/james/bin
    03) /usr/local/bin
    04) /usr/bin
    05) /sbin
    06) /bin
    07) /usr/bin/X11
    08) /usr/X11R6/bin
    09) /usr/games
    10) /opt/kde3/bin
    11) /usr/lib64/jvm/jre/bin
    12) /usr/sbin
    13) /usr/sbin
    14) /usr/sbin
    
    su - (su -<enter> and echo $PATH<enter>)
    
    01) /usr/lib64/mpi/gcc/openmpi/bin
    02) /sbin
    03) /usr/sbin
    04) /usr/local/sbin
    05) /root/bin
    06) /usr/local/bin
    07) /usr/bin:/bin
    08) /usr/bin/X11
    09) /usr/X11R6/bin
    10) /usr/games
    11) /opt/kde3/bin
    12) /usr/lib64/jvm/jre/bin
    13) /usr/sbin
    
    su - root (su - root<enter> and echo $PATH<enter>)
    
    01) /usr/lib64/mpi/gcc/openmpi/bin
    02) /sbin
    03) /usr/sbin
    04) /usr/local/sbin
    05) /root/bin
    06) /usr/local/bin
    07) /usr/bin
    08) /bin
    09) /usr/bin/X11
    10) /usr/X11R6/bin
    11) /usr/games
    12) /opt/kde3/bin
    13) /usr/lib64/jvm/jre/bin
    14) /usr/sbin
    So in our very first attempt to run the fdisk command, we were told the program was located in the folder /sbin/fdisk. So the only way this command worked is when we used the su command which placed the /sbin folder in our path. Now, knowing the path, we could have run the program using sudo in this way:

    Code:
    sudo /sbin/fdisk -l
    This time, using the sudo command, the fdisk -l command works just fine. For a few other things about the path command. You can add to your path command for a single terminal session by using the following command:

    Code:
    PATH=${PATH}:/new/path/to/add
    Close the terminal session and it is gone however. What about making it there all of the time, but for your personnel sessions only. Edit the following file, if you use KDE:

    Code:
    kdesu kwrite ~/.profile
    And add your new path command there. So, have you learned anything about using the path command? Have I missed something you need to add (which is very likely)? Have I got anything wrong (then correct me)?

    If you post a command that needs root permissions, make sure your fellow users know how to make the command work as it takes only a little more effort to show this.

    Please, I welcome all comments and all points of view. Lets make this a good thread to read for everyone. Remember you can't learn if you don't try and you don't fail, so that you can see the error of your ways and make the required corrections. Its the only way to learn.

    I fail all of the time, even as we speak.

    Thank You,
    My Blog: https://forums.opensuse.org/blogs/jdmcdaniel3/

    Software efficiency halves every 18 months, thus compensating for Moore's Law

    Its James again from Austin, Texas

  2. #2
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Terminal Path Command - echo $PATH - What Do You Know About Your PATH?

    Use

    sudo -i fdisk -l

    Also note the "may" in cnf's message. Some programs in /sbin and /usr/sbin, e.g. ifconfig, don't require root privilege to run in certain modes, e.g. report only. I make it easy to use such programs by linking to it from ~/bin.

    ln -s /sbin/ifconfig ~/bin

  3. #3

    Default Re: Terminal Path Command - echo $PATH - What Do You Know About Your PATH?

    Hi James,
    Maybe you should highlight the difference between:
    Code:
    su
    echo $PATH
    and
    Code:
    su -l
    echo $PATH

  4. #4
    Join Date
    Mar 2010
    Location
    Austin - Texas
    Posts
    10,140
    Blog Entries
    48

    Smile Re: Terminal Path Command - echo $PATH - What Do You Know About Your PATH?

    Yet another way to use sudo.

    Code:
    sudo -i fdisk -l
    It is not clear to me why -i or -s would allow this command to work. Could you expand on this ken_yap? And thanks so much for your input here.

    and

    please_try_again, I was thinking the explanation of the difference between su & su -l was at least one of the reasons you were here? Right?

    Thank You,
    My Blog: https://forums.opensuse.org/blogs/jdmcdaniel3/

    Software efficiency halves every 18 months, thus compensating for Moore's Law

    Its James again from Austin, Texas

  5. #5
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Terminal Path Command - echo $PATH - What Do You Know About Your PATH?

    It reads the login files of the target user (in this case root).

  6. #6
    Join Date
    Jun 2008
    Location
    Berlin
    Posts
    2,061

    Default Re: Terminal Path Command - echo $PATH - What Do You Know About Your PATH?

    The command 'sudo' has not much in common with 'su'.

    Quote Originally Posted by man sudo
    sudo allows a permitted user to execute a command as the superuser or another
    user, as specified in the sudoers file.

  7. #7
    Join Date
    Jun 2008
    Location
    Earth - Denmark
    Posts
    10,730

    Default Re: Terminal Path Command - echo $PATH - What Do You Know About YourPATH?

    jdmcdaniel3 wrote:
    > I was thinking the explanation of the difference
    > between su & su -l was at least one of the reasons you were here?


    su is short for substitute user, some say switch user (but it is *not*
    for "super user")..

    remember, Linux roots are in Unix and it is born a multi-user
    system...think Big Iron...a main frame in the corporation basement or
    college administration building, and 1000 users scattered all
    over....and one (or a hand full of) system administrators or
    superusers or root(s)..

    one day an administrator might get a note from Tim in Personnel that
    his account is messed up and he gives instructions on how to see the
    mess..that super user might then wanna see exactly what Tim sees and
    at a terminal type:

    Code:
    su Tim
    and, poof that Administrator _is_ (as far as the machine knows) Tim!!

    think switch user when you use su, that is all it is...it is NOT a
    become Super User command...

    *except*, the default behavior for su alone is to ASSUME the typing
    person wants to be root..

    the 'real' way to become root is

    Code:
    su root
    try it!

    *however*, when you do JUST that, with no options stated, then you
    keep YOUR environment (including path)....that is if i am signed in as
    me and "su root" i will remain in my home directory and the path will
    remain my path...NOT root's path..

    to fully switch to root AND root's environment it is necessary to
    change the shell by using an option see "man su"...all three of these
    options are synonyms for each other and will result in that change:

    Code:
    su --login root
    su -l root
    su -
    entering any of those and i am magically transported to roots home
    with root's full environment..

    ok so far?

    now, Ken has already told you that sbin is not in the users path, so
    that is why you can't see them if you have only used su to become
    root, but keep your on path..

    sudo is different, (remember the mainframe) it is designed to allow
    the Admin(s) to give some certain LIMITED ability to do some things
    that normally only a system admin can do...see

    [CODE]
    man sudo
    [CODE]

    --
    DenverD
    When it comes to chocolate, resistance is futile.
    CAVEAT: http://is.gd/bpoMD [posted via NNTP w/openSUSE 10.3]

  8. #8
    Join Date
    Jun 2008
    Location
    Groningen, Netherlands
    Posts
    19,861
    Blog Entries
    14

    Default Re: Terminal Path Command - echo $PATH - What Do You Know About YourPATH?

    Let me put something straight in your line of thinking: there is only one root, no certain types of root. As you may have found by reading the above, it depends on the way you invoke root access.

    'su' is not restricted to root. Suppose you have 2 users, james and semaj, you're logged in as james, but want to edit a file with the other user's credentials:
    Code:
    james@machine:~>su - semaj
    Password: semajspasswordhere
    semaj@machine:>vi somefile
    If no user argument is given, root is the default.

    On a login the user's environment variables are read, they contain the PATH.
    ° Appreciate my reply? Click the star and let me know why.

    ° Perfection is not gonna happen. No way.

    https://en.opensuse.org/openSUSE:Board#Members
    http://en.opensuse.org/User:Knurpht
    http://nl.opensuse.org/Gebruiker:Knurpht

  9. #9

    Default Re: Terminal Path Command - echo $PATH - What Do You Know About YourPATH?

    Quote Originally Posted by Knurpht View Post
    Suppose you have 2 users, james and semaj
    @Knurpht, you're not just writing in English, German, French and Dutch. Now you're writing in verlan as well.
    Verlan - Wikipedia, the free encyclopedia

  10. #10
    Join Date
    Nov 2009
    Location
    West Virginia Sector 13
    Posts
    15,748

    Default Re: Terminal Path Command - echo $PATH - What Do You Know About YourPATH?

    If you are confused on who you are you can do a whoami command

Page 1 of 3 123 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •