Welcome to the Linux Foundation Forum!

superuser

Hi, I am new.

I have a simple question.

On Ubuntu, I made a script:

#!/bin/bash

if [ "$(id -u)" = "0" ]; then
echo "superuser"

else
echo "no"
fi


I called the file "boo".

When I run "boo" as a regular user it says "no". But when I sudo su it, it says "boo" is not a command. So, how do I make it output "superuser".

Hope this make sense.

Comments

  • mfillpot
    mfillpot Posts: 2,177
    After you run sudo su your system switches you to the root user and places you in the root home directory. Most likely you launched sudo su when in your chosen directory, but attempted to just call the script name while in the root home directory.
  • aznbun
    aznbun Posts: 4
    So, how do I go back to my home directory.

    On the command prompt is says I am at:

    root@ubuntu:/home/bun/bin


    When I "ls" in that directory I see all my files and also "boo".
    But when I type "boo" in the command prompt I still get:

    boo: command not found


    I am stuck, think I am missing something very simple.
  • Goineasy9
    Goineasy9 Posts: 1,114
    If you want to stay in root, just type cd /home/<username>/

    If you want to be as user, type exit <enter> first, then cd /home/<username>/
  • marc
    marc Posts: 647
    It sounds like Ubuntu is placing the current directory ( $PWD) in the PATH thus you can call the program named "boo" that is in the current directory.

    whenever you switch to root the PATH variable must be different.

    Try with
    ./boo
    
  • aznbun
    aznbun Posts: 4
    Thank you.

    The "./boo" worked. But I thought "./" was implied.

    I thought by default "./boo" = "boo".


    Now, I can move on to the next section. Guess I will use "./" when I am a superuser mode.
  • saqman2060
    saqman2060 Posts: 777
    After you type sudo su, before you execute your script, type the path to your script.

    Example, root@ubuntu:cd <path to your script> boo.
  • marc
    marc Posts: 647
    aznbun wrote:
    Thank you.

    The "./boo" worked. But I thought "./" was implied.

    I thought by default "./boo" = "boo".

    No. In fact, I find that to be a security flaw.
  • or put the commands in a path defined in your path section , for regular files will be ok in /usr/local/bin , this will make the command launchable simply via "boo"
  • spixx
    spixx Posts: 9
    Quick note:
    The reason boo works for user X should be because of the fact that you have added /home/X/bin to the PATH (you can check the command out by writing echo $PATH in terminal). When switching to root the user no longer has any PATH entry to that particular folder thus writing boo checks folder /bin /sbin and such but cannot find the script. This is not true when using sudo since sudo is a nice way of doing su -c '/path/to/script/ sudo does the command as another user (namely root) and there the PATH is different :)

    Hope that helped any other who wondered.

    echo $PATH as user:
    /usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/XXX/bin

    And sudo:
    /usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/XXX/bin

    And as sudo su:
    /sbin:/bin:/usr/sbin:/usr/bin

    Note the difference. I'm using Centos so I'm not sure that sudo behaves in the same way on any other dist though.

Categories

Upcoming Training