Command without sudo works but requires sudo - linux

I have already solved this problem but I do not know why this solves the problem and I don't like not knowing what went wrong. I'm using the terminal on Ubuntu
Here was the issue...
If I run $ ngm -args it runs but fails because it requires sudo
If I run $ sudo ngm -args I get the error like ngm not found
If I run $ sudo /usr/local/lib/ngm -args it runs with sudo and works perfectly.
I don't understand why the 3 works and 2 doesn't work.

When trying to run $ ngm -args, the shell will look for the executable in it's $PATH variable, and it finds it. When trying to run the executable it finds that it has to be sudo and exits.
When you run $ sudo ngm -args, the shell will look for the executable in the $PATH environment of the Root user, and it can't find it.
When running it like the last option, the shell doesn't need to look in the $PATH of the root user, because it finds it in the path that you specified /usr/local/lib/ngm, so both issues are gone.
This might be a starting point in order to understand the PATH

Related

"sudo: k0s: command not found": even though its script is executable and its location is in the path

What I am trying to do is:
To play with K0s. So, first I download the K0s scrips and make it executable:
$ curl -sSLf https://get.k0s.sh | sudo sh
.. and here is the terminal output:
> Downloading k0s from URL:
> https://github.com/k0sproject/k0s/releases/download/v1.20.6+k0s.0/k0s-v1.20.6+k0s.0-amd64
> k0s is now executable in /usr/local/bin`
At this point, when I type k0s in the terminal, I get the help page. Also, when I run type k0s, I get the /usr/local/bin/k0s
Again, when I run $ echo $PATH, I see that /usr/local/bin/ is included in the path variables.
The problem is:
When I try to run:
$ sudo k0s install controller --single
I get:
> sudo: k0s: command not found
But it works when I put the full path of the k0s:
$ sudo /usr/local/bin/k0s install controller --single
I tried this answer and this answer but both did not work. Both suggest changing the mode and erload the shell hash-table.
The question is:
what is the probelm, and how to fix it ?

linux deploy, kali, start an app service after its boots up

Hi there does anybody knows how to make an apache2 service to run at startup?
After Kali linux boots up,
I have tried editing rc.local, rc.init and rc file.
Adding
service apache start
command I have also made bash script, giving its permissions to run it with chmod +x b.sh - placed in bin folder for ability to run in anywhere in command line.
I am able to run it in terminal, it does what I need. But can't get it running on start up.
Also I tried:
update-rc.d mystartup.sh defaults 100
still no success. Any ideas?
Thanks.
Place your script inside init /etc/init.d/ , then place a link to it inside /etc/rc5.d/ (be sure to get your accurate run-level)...
Start the second link with the string S71 like S71apastart
I.e do the following:
Place the text:
#!/bin/bash
sudo service apache start
inside /etc/init.d/apaStart (a new file created by you)
Then
sudo chmod +x /etc/init.d/apaStart
sudo ln -s /etc/init.d/tomcat /etc/rc5.d/S71apaStart (change rc5.d to rc3.d if needed depending on your runlevel)
I won't suggest this method but it'll work with your situation.

rundeck - switch to root user in job script

Logging via terminal I can switch to root user fine:
ubuntu#ip-10-0-0-70:~$ sudo -s
root#ip-10-0-0-70:~# whoami
root
So I created in rundeck a job script with this:
whoami;
echo "1st step";
sudo -s;
echo "2nd step";
And when I run this, it prints:
ubuntu
1st step
After print '1st step' it get stucked forever. Seems a problem with sudo -s command.
tried sudo -i but the same happens
tried sudo su - root but the same happens
rundeck is logging as ubuntu user, me too
any idea to switch to root in rundeck script?
This is the expected behaviour.
You are running a shell via 'sudo -s' and then not leaving/exiting it ! So it waits forever for somethig that won't come.
You can probably add 'sudo' as an Advanced option of your script (where it says "Run script with an interpreter or prefix. E.g.: sudo, time:").
But it will run your whole script as root.
If you just want a specific command to be run as root , just prefix your command with sudo as so:
sudo "enter_your_command_to_be_run_as_root_here"
Entering the command prefixed by Sudo will generate the following error on most linux distributions.
sudo: sorry, you must have a tty to run sudo
You can enable sudo without tty by running 'visudo' and commenting out the defaults line or removing 'requiretty' from the defaults line.
Details can be found here:
http://www.cyberciti.biz/faq/linux-unix-bsd-sudo-sorry-you-must-haveattytorun/

starting apachectl from bash

I am writing a bash file. I need to start apachectl from my bash file. so i wrote:
apachectl start
When I run it with root, an error occurred:
apachectl: command not found
I searched and I found that, I should be super user with su - not su
Now, I want to know:
why this error occurred?
How could i run it with su?
In shell scripts you should use full paths in order to execute command unless directory with executable already in $PATH.
For instance, find where apachectl binary is located:
which apachectl
or
whereis apachectl
and you will get something like:
/usr/local/sbin/apachectl
So, use that.
The command not found error is because "apachectl" is not in your path. Simply use the full path of the command, e.g.
/etc/init.d/apachectl start
If you get a permission denied error, then you need to run as a different user. That is a different problem though.
Use the find command to first locate apachecetl
find / -name apachectl
Then you can test it by running the status command (assuming this is the location from the find command)
/usr/local/sbin/apachectl status
Then you may need to restart apache if there's an issue
/usr/local/apache/bin/apachectl restart
It seems, that the command apachectl is not in your environments path. Locate the directory, where apachectl resides and add this to your PATH or start it with the full path. Most modern distributions use sudo to allow users gain elevated rights, so you should use sudo, if available to you.
Below command worked for me:
sudo /usr/sbin/apachectl -kstart
The answer above helped me a lot but the commands should be:
sudo netstat -tanp
sudo ss -tanp 'sport = 80'
sudo apt-get remove lighttpd
sudo <path>/apachectl -kstart
First kill all httpd service using...
sudo killall -9 httpd
Second, find apachectl. Press ctrl+r into terminal enter a "apachectl" word To find the path of "apachectl".
After select:
sudo <path>/apachectl stop|start
This question is old but comes up in Google, so for future readers: Please notice on some distributions such as Debian it is a sudo-only command and trying to run it without sudo leads to the error: command not found. So in order to run it simply use sudo. Also if you want to know where the binary is located at too, the simplest way I recommend (if using APT) is:
$ dpkg -L apache2 | grep apachectl
/usr/sbin/apachectl
/usr/share/man/man8/apachectl.8.gz
As you see, it's under sbin which means is exclusive to admins space.
I had the same problem. You may run the following command first:
export PATH=$PATH:/sbin
Then use apachectl restart or etc.
well, it happens why you port be used now other service. for you know that
service is use write next comand of netstat:
sudo netstat -tanp.
if you wanna to know of port 8080 use next comand:
trong textsudo ss -ntlp 'sport = 80'.
in my case was run lighttpd so apply next comand:
sudo apt-get remove lighttpd.

Running a file as nonroot from a root bash script

Okay, I currently use an eggdrop IRC bot on my server. I want to make a bash script to start it up as well as a few other scripts at the same time. All that's left is to make it start, but it won't run as root.
However, I also need to be in the current directory of the file to run it, or it displays an error.
For example:
/home/eggdrop/eggdropbot/eggdrop will display an error, so to run it I need to
cd /home/eggdrop/eggdropbot and then ./eggdrop
So I can't just use "sudo -u eggdrop /home/eggdrop/eggdropbot/eggdrop" and as you probably know, sudo won't cd, either, since that would be pointless.
Any suggestions?
Why not just cd first and then sudo -u ./eggdrop .?
What about doing the cd, and, only then, launch the command with sudo ?
I suppose something like this should do the trick :
cd /home/eggdrop/eggdropbot && sudo -u eggdrop ./eggdrop
You can cd to the directory as the root user and then use sudo -u to invoke the program from the working directory.

Resources