command not found even when "which" shows its path with sudo - shell

I'm on Fedora release 25 with zsh 5.2
I am trying to use a command with sudo. (In this example, docker-compose)
Problem:
which command shows where it is.
$ sudo PATH=$PATH which docker-compose
/usr/local/bin/docker-compose
In spite of that, command not found
$ sudo PATH=$PATH docker-compose
sudo: docker-compose: command not found
I could make it work by sudo `which docker-compose` but I want to know why this occurs.
What I tried:
I double-quoted PATH=$PATH but got the same result.
$ sudo "PATH=$PATH" docker-compose
sudo: docker-compose: command not found

/usr/local/bin/ is not on root path. Check with
sudo bash -c 'echo "$PATH"'
/usr/sbin:/usr/bin:/sbin:/bin
Use absolute path to the command.
Adding /usr/local/bin to root path seems to be a security risk.

Related

installing and using conda within a sudo-called bash script

I have a bash script which installs some software with apt-get as well as download and installs miniconda3. Later I would like to utilize conda command without restarting the shell. This script is called with sudo but for all the things related to conda I want to pose as a regular user, see below:
#!/usr/bin/env bash
# we are operating in the user's home dir
sudo -u $SUDO_USER bash Miniconda3-latest-Linux-x86_64.sh -b -p miniconda3
source [path_to_the_user_home]/miniconda3/etc/profile.d/conda.sh
sudo -u $SUDO_USER -H -s eval $(conda shell.bash hook)
sudo -u $SUDO_USER conda --version
However, I get an error that the command conda is not recognized. Interestingly, if the last line would be just conda --version then it is correctly recognised. It seems that the 2nd to last line worked for root, but not the user (which is exactly what I want)

Command not found with sudo, but works without sudo

I've installed a binary dep in my GOPATH at /home/me/go/bin to be used.
Running dep successfully executes the binary, however running sudo dep results in sudo: dep: command not found:
$ dep
Dep is a tool for managing dependencies for Go projects
Usage: "dep [command]"
...
Use "dep help [command]" for more information about a command.
$ sudo dep
sudo: dep: command not found
The paths are not the issue here:
$ echo $PATH
/usr/share/Modules/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin:/home/me/.local/bin:/home/me/bin:/home/me/.local/bin:/home/me/bin:/home/me/go/bin:/home/me/.local/bin:/home/me/bin:/home/me/go/bin
$ sudo echo $PATH
/usr/share/Modules/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin:/home/me/.local/bin:/home/me/bin:/home/me/.local/bin:/home/me/bin:/home/me/go/bin:/home/me/.local/bin:/home/me/bin:/home/me/go/bin
The paths are identical as me and as superuser both referencing the key directory /home/me/go/bin.
Why does running dep without sudo succeed but with sudo results in command not found?
By default, sudo does NOT pass the user's original PATH into the superuser process, and it gets some default PATH defined on the system. That's easy to see if you run "sudo env" to see the entire environment of the sudo'ed process:
$ sudo env | grep PATH
PATH=/sbin:/bin:/usr/sbin:/usr/bin
The command you tried, "sudo echo $PATH" doesn't check anything, because the shell first translates the $PATH to whatever value this variable has - and only then calls the command (sudo), so it just prints your outer environment's value :-)
To get your PATH to pass inside sudo, you can do something like this:
$ sudo PATH=$PATH sh -c env | grep PATH
PATH=/usr/share/Modules/bin:/usr/lib64/ccache:/home/nyh/gaps:/home/nyh/bin:/usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:/usr/sbin:/sbin:/usr/games:/usr/local/android-sdk-linux/tools:/usr/local/android-sdk-linux/platform-tools:/home/nyh/google-cloud-sdk/bin
Basically the command I passed for sudo to run starts by setting PATH to $PATH (remember that $PATH is expanded by the outer shell, before sudo ever runs, so is the real path I want!) and running a shell (which will use this new PAT) to "env". As you can see, env did get the right path. You can replace "env" by whatever program you wanted to run.

kubectl bash completion doesn't work in ubuntu docker container

I'm using kubectl from within a docker container running on a Mac. I've already successfully configured the bash completion for kubectl to work on the Mac, however, it doesn't work within the docker container. I always get bash: _get_comp_words_by_ref: command not found.
The docker image is based on ubuntu:16.04 and kubectl is installed via the line (snippet from the dockerfile)
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && \
mv kubectl /usr/local/bin
echo $BASH_VERSION gives me 4.3.48(1)-release, and according to apt, the bash-completionpackage is installed.
I'm using iTerm2 as terminal.
Any idea why it doesn't work or how to get it to work?
Ok, I found it - I simply needed to do a source /etc/bash_completion before or after the source <(kubectl completion bash).
check .bashrc
enable programmable completion features (you don't need to enable
this, if it's already enabled in /etc/bash.bashrc and /etc/profile
sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
. /etc/bash_completion
fi
A Linux container executed on macOS creates a separate environment and
yes, it looks like a thread from macOS shell, but it is not. Shell history,
properties, functions are a different story.
Moreover, if the container has no persistent volume mounted all of those parameters will be transisten and won’t survive container’s restart.
The approach to have bash completion of both of them - macOS and Ubuntu
Linux are similar, but require different steps to take:
macOS side - permanent support for kubectl bash completion:
use homebrew to install support:
brew install bash-completion
kubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl
Ubuntu container’s approach to have kubectl and bash completion support build in:
You can adapt this set of commands and use it in Dockerfile during the image preparation:
apt-get update && apt-get install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubectl
echo 'source <(kubectl completion bash)' >> ~/.bashrc
If afterwards you or user executes /bin/bash in running container then you should get completion working.
docker exec -it docker_image_id /bin/bash
this will start bash shell with the bash completion.
I united two top comments for Ubuntu 22.04
edit ~/.bashrc and add
source /etc/bash_completion
before
source <(kubectl completion bash)
alias k=kubectl
complete -o default -F __start_kubectl k

Docker refusing to run bash

I have the following docker setup:
python27.Dockerfile
FROM python:2.7
COPY ./entrypoint.sh /entrypoint.sh
RUN mkdir /src
RUN apt-get update && apt-get install -y bash libmysqlclient-dev python-pip build-essential && pip install virtualenv
ENTRYPOINT ["/entrypoint.sh"]
EXPOSE 8000
WORKDIR /src
CMD source /src/env/bin/activate && python /src/manage.py runserver
entrypoint.sh
#!/bin/bash
# some code here...
# some code here...
# some code here...
exec "$#"
Whenever I try to run my docker container I get python27 | /bin/sh: 1: source: not found.
I understand that the error comes from the fact that the command is run with sh instead of bash, but I can't understand why is that happening, given the fact that I have the correct shebang at the top of my entrypoint.
Any ideas why is that happening and how can I fix it?
The problem is that for CMD you're using the shell form that uses /bin/sh, and the /src/env/bin/activate likely contains a "source" command, which isn't available on POSIX /bin/sh (the equivalent builtin would be just .).
You must use the exec form for CMD using brackets:
CMD ["/bin/bash", "-c", "source /src/env/bin/activate && python /src/manage.py runserver"]
More details in:
https://docs.docker.com/engine/reference/builder/#run
https://docs.docker.com/engine/reference/builder/#cmd
https://docs.docker.com/engine/reference/builder/#entrypoint

Why is sudo: bundle command not found?

Why is command "bundle" not found when using sudo:
[root#desktop gitlab]# sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production
sudo: bundle: command not found
[root#desktop gitlab]#
but does exist when not using sudo:
[root#desktop gitlab]# bundle exec rake gitlab:setup RAILS_ENV=production
Warning
You are running as user root, we hope you know what you are doing.
Things may work/fail for the wrong reasons.
For correct results you should run this as user git.
This will create the necessary database tables and seed the database.
You will lose any previous data stored in the database.
Do you want to continue (yes/no)? no
Quitting...
[root#desktop gitlab]#
The reason I ask is I am following https://github.com/gitlabhq/gitlab-recipes/tree/master/install/centos, and it states to use sudo.
I've tried adding a -i flag as described by Using $ sudo bundle exec ... raises 'bundle: command not found' error, but get "This account is currently not available.".
Check if the PATH has the same values both with and without sudo. Apparently it cannot find bundle just because it is not listed in PATH
You can compare the outputs of following two lines
$ echo 'echo $PATH' | sh
$ echo 'echo $PATH' | sudo sh
Ideally sudo is supposed to leave PATH untouched. But this might be a side issue of your hosting distribution.
Edit by original poster. Output is:
[root#desktop etc]# echo 'echo $PATH' | sh
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root#desktop etc]# echo 'echo $PATH' | sudo sh
/sbin:/bin:/usr/sbin:/usr/bin:/user/local/bin
[root#desktop etc]#
The user was created without a bash login shell. Change this in centos using system-config-users. Then su git into /home/git and move to gitlab directory. Execute the bundle commands without the sudo tag. The next error you will encounter is the missing database.yml in the config dir. fix this with the correct password (i.e. copy the mysql or postgres sample and edit).
I had this issue I thought that my gitlab installed from source and I got same error. but after try Omnibus method for backup my issue solved
with this command:
sudo gitlab-rake gitlab:backup:create
Try :
sudo -u git -H env PATH=$PATH && bundle exec rake gitlab:check RAILS_ENV=production
to use the same PATH than current user.

Resources