Add folder to sudo path without -i - path

There has been a lot of talk already done on Stack Overflow about adding a folder to the sudo path. But, none of the other tutorials I've seen have really answered the following question:
How can I add a folder to the sudo PATH without using -i.
Here is my setup. The folder I want to add is "/var/folder". There is the bash script "/var/folder/script.sh". I added the following lines of code to the /root/.bashrc file:
if [ -d /var/folder ]; then
PATH=/var/folder:$PATH
fi
Now, when I type in the command "sudo echo $PATH" I get the following output:
/var/folder:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
The problem is, when I run the command "sudo script.sh", the script can't seem to be found. The output is as follows:
sudo: script.sh: command not found
This is in spite of the fact that tab-auto-complete works on "sudo script.sh".

All though the $PATH is defined when you do an echo of it, for running the script it is not actually defined. So to see what is happening, you can run the following:
sudo -s
echo $PATH
You will notice that it will be:
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
Where is it getting the $PATH from?
It is defined in your sudoers file:
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Solution
You can update the Defaults secure_path in the sudoers file to have the correct value.
Defaults secure_path="/var/folder:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Going back to how you were seeing the correct value when you ran:
sudo echo $PATH
Since you had $PATH defined with /var/folder before you ran that command, it just replaced the $PATH with the value, but your actual path for sudo was
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
so you were effectively running
sudo echo /var/folder:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

Related

/usr/bin/sudo: Permission denied when calling sudo from sh script via telegra-cli with lua script

Im trying to run my .sh scipt status.sh via a telegram message:
Ubuntu 20.04.1 LTS server
Telegram-cli with a lua script to action status.sh script
when i send the message "status" to my server via telegram it actions the status.sh script, in this script i have a bunch of stuff that gathers info for me and sends it back to telegram so i can see what the status of my server is, however (i recently did a fresh install of the server) for some reason if the script has a line of code starting with sudo i get:
line 38: /usr/bin/sudo: Permission denied
if i run the script from the command line ./status.sh it runs without any problem!? so im thinking its because it is being called from telegram or lua!?
example of code that generates the error: sudo ifconfig enp0s25 >> file
on the other hand this line works without a problem:
sudo echo Time: $(date +"%H:%M:%S") > file
/usr/bin has 0755 permission set
sudo has 4755 permission set
The following command
sudo ifconfig enp0s25 >> file
would not work if file requires root privilege to be modified.
sudo affects ifconfig but not the redirection.
To fix it:
sudo sh -c 'ifconfig enp0s25 >> file'
As mentioned in Egor Skriptunoff's answer, sudo only affects the command being run with sudo, and not the redirect.
Perhaps nothing is being written to file in your case because ifconfig is writing the output you are interested in to stderr instead of to stdout.
If you want to append both stdout and stderr to file as root, use this command:
sudo sh -c 'ifconfig enp0s25 >> file 2>&1'
Here, sh is invoked via sudo so that the redirect to file will be done as root.
Without the 2>&1, only ifconfig's stdout will be appended to file. The 2>&1 tells the shell to redirect stderr to stdout.
If file can be written to without root, this may simplify to
sudo ifconfig enp0s25 >> file 2>&1

'bash' : no such file or directory

I just joined the windows insider program so I could install Ubuntu and install Ruby via bash commands. Everytime I open the the ubuntu command prompt it will not allow me to run any commands without throwing errors. examples
Command 'sudo' is not availiable in '/usr/bin/sudo'
At the top of the Ubuntu command prompt it says:
/usr/bin/env: 'bash': no such file or directory
How can I resolve this issue. I need to set up ruby so I can start developing.
You can check content of PATH variable by executing command $ echo $PATH
If you do not find /usr/bin in the output than you can append /usr/bin in PATH variable by executing command
$ export PATH=$PATH:/usr/bin

Dockerfile's RUN command doesn't find script

Using Docker Toolbox on Windows 10, Docker cannot build an image from my Dockerfile because it doesn't find a script (install-composer) that was copied to the image.
FROM php:7.2.5-apache
COPY scripts/install-composer /usr/bin
RUN chmod +x /usr/bin/install-composer
RUN /usr/bin/install-composer
The error I get, when reating the last RUN command, is:
/bin/sh: 1: /usr/bin/install-composer: not found
The chmod command does work however, indicating the file does actually exist in the image.
A very simple problem but a very misleading error.
The problem was caused by wrong file endings. Git was set up to convert the project files into Windows (CRLF) file endings. I reinstalled Git with the setting "Checkout as-is, commit Unix-style", deleted and recloned the repository, and it fixed the problem.
When it comes to explaining the misleading and confusing error message, my guess is that the file install-composer was actually found and executed. What it is actually saying was that was not found. This empty name was simply the CR caught between two LF (in other words, an empty line) and sh interpreted it as a call to a script file.
Try and group those RUN commands:
RUN chmod +x /usr/bin/install-composer && \
ls -alrth /usr/bin/install* && \
/usr/bin/install-composer
That way, you will see if the file is indeed copied and present.
You can also try, for your second RUN:
RUN /bin/bash -c "/usr/bin/install"
(assuming your script uses bash, and you have a bash installed in your image)

Where to set LEIN_ROOT?

When using sudo lein run (because some of the files changed by that command need priveleges) I get this message:
WARNING: You're currently running as root; probably by accident.
Press control-C to abort or Enter to continue as root.
Set LEIN_ROOT to disable this warning.
Any idea how or where to set LEIN_ROOT in order to avoid getting this message?
Add LEIN_ROOT=true to the end of /etc/profile. For this change to take effect, enter source /etc/profile to a terminal. Then run the command with sudo -E lein run to preserve environment variables.
If you are doing this over ssh you would need to do all of the above on the server then add source /etc/profile to the start of the ssh command run on the local machine.
ssh user#123.456.789 "source /etc/profile; sudo -E lein run"

Why is capistrano interpreting a flag passed with a command to `run` as input?

I'm trying to do this:
run "echo -n 'foo' > bar.txt"
and the contents of bar.txt ends up being:
-n foo \n
(With \n representing an actual newline)
I use run for other commands like rm -rf and, to my knowledge, it works fine.
I just found this in man echo:
Some shells may provide a builtin echo command which is similar or identical to this utility. Most notably, the builtin echo in sh(1) does not accept the -n option. Consult the builtin(1) manual page.
My version of bash has an echo builtin but seems to be respecting the -n flag. It looks like the shell on your deployment machine doesn't, in which case using the full path to the echo binary might do what you want here:
run "/bin/echo -n 'foo' > bar.txt"
It appears as though the -n flag isn't being interpreted as a flag by the shell. If, from the command line, one executes echo -Y hi, the output will be -Y hi.

Resources