简体   繁体   中英

command not found when adding sudo before my command

I'am using gitlab-runner binary and I cannot launch commands needed privilege. I'am logged with my user sysadmin I have put the binary in /usr/local/bin and i did'nt need to add the path in my PATH variable. Here my bash_profile

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

echo $PATH

/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/sysadmin/bin

Command without sudo work's perfectly

gitlab-runner -help
NAME:
   gitlab-runner - a GitLab Runner

USAGE:
   gitlab-runner [global options] command [command options] [arguments...]

But all command with sudo return

sudo gitlab-runner status

return

command not found

NOW FOR ROOT

This is my bash_profile for root.

  # User specific environment and startup programs
    GITLAB_RUNNER=/usr/local/bin
    PATH=$PATH:$HOME/bin:$GITLAB_RUNNER

    export PATH
    export GITLAB_RUNNER

echo $PATH

/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/bin

gitlab-runner status
gitlab-runner: Service is running!

The only way to use any command it's to use the path where the binary is located.

[sysadmin@XXXXXXXX ~]$ sudo /usr/local/bin/gitlab-runner status
Runtime platform                                    arch=amd64 os=linux pid=12666 revision=8bb608ff version=11.7.0
gitlab-runner: Service is running!

Please can you explain me what's going wrong. Thanks.

When you use sudo, you are running the command as root user/super user. The root user doesn't have /usr/local/bin/ in PATH variable in your case. That is why you are able to run the command using full path. If you want to verify this, you can try the following

sudo -E env "PATH=$PATH" gitlab-runner status

This will use the PATH variable of the current user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM