Welcome to the Linux Foundation Forum!
superuser
aznbun
Posts: 4
in Command Line
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
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.
0
Comments
-
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.0
-
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.0 -
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>/0 -
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
0 -
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.0 -
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.0 -
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.0 -
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"0
-
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.0
Categories
- All Categories
- 177 LFX Mentorship
- 177 LFX Mentorship: Linux Kernel
- 769 Linux Foundation IT Professional Programs
- 379 Cloud Engineer IT Professional Program
- 175 Advanced Cloud Engineer IT Professional Program
- 75 DevOps IT Professional Program - Discontinued
- 7 DevOps & GitOps IT Professional Program
- 102 Cloud Native Developer IT Professional Program
- 7.6K Training Courses & Learning Paths
- 5 AI & ML Training
- 1 Blockchain & Decentralized Identity Training
- 14 Cloud & Containers Training
- 1 Cybersecurity Training
- 2 DevOps & Site-Reliability Training
- 1 Linux Kernel Development Training
- 2 Networking Training
- 2 Open Source Best Practice Training
- 3 System Administration Training
- 1 System Engineering Training
- 2 Web & Application Development Training
- 796 Hardware
- 202 Drivers
- 68 I/O Devices
- 37 Monitors
- 95 Multimedia
- 173 Networking
- 91 Printers & Scanners
- 91 Storage
- 771 Linux Distributions
- 81 Debian
- 68 Fedora
- 23 Linux Mint
- 13 Mageia
- 24 openSUSE
- 151 Red Hat Enterprise
- 31 Slackware
- 13 SUSE Enterprise
- 356 Ubuntu
- 465 Linux System Administration
- 31 Cloud Computing
- 73 Command Line/Scripting
- Github systems admin projects
- 98 Linux Security
- 78 Network Management
- 101 System Management
- 46 Web Management
- 125 Mobile Computing
- 20 Android
- 90 Development
- 1.2K New to Linux
- 1K Getting Started with Linux
- 396 Off Topic
- 121 Introductions
- 31 Study Material
- 1K Programming and Development
- 310 Kernel Development
- 703 Software Development
- 1K Software
- 406 Applications
- 182 Command Line
- 5 Compiling/Installing
- 70 Games
- 318 Installation
- Archived
- 183 Small Talk
- 2 LFD140 Class Forum
- 1.4K LFS258 Class Forum
Upcoming Training
-
August 20, 2018
Kubernetes Administration (LFS458)
-
August 20, 2018
Linux System Administration (LFS301)
-
August 27, 2018
Open Source Virtualization (LFS462)
-
August 27, 2018
Linux Kernel Debugging and Security (LFD440)