The git path is not passing when rooting.

Asked 1 years ago, Updated 1 years ago, 44 views

For $, you can use the git command, but
The git command is unavailable when sudo-s becomes the root user.
I think the reason is that the path is not passed when I was a root user, but I don't know how to set it up.
How do I set the path for the root user just like the normal user?

$whichgit
/usr/local/bin/git

# which git
/usr/bin/which:no git in (/sbin:/bin:/usr/sbin:/usr/bin)

git centos

2022-09-30 17:23

2 Answers

The default setting for CentOS (which I confirm with 7) is /sbin:/bin:/usr/sbin/:/usr/bin due to the secure_path setting of sudoers, the sudo configuration file.

In order to enable /usr/local/bin/git to be used in the same operational manner as the question, one of the following methods should be used:In addition, if it is not sudo-s, sudo can take over the current environment variable PATH with the -E option.

  • Use sudo su- instead of sudo-s
  • Use su or su- instead of
  • sudo-s (root password becomes root)
  • Enter
  • sudo visudo to edit sudoers and set the Defaults secure_path line


2022-09-30 17:23

For bash, set the environment variable PATH to pass the path.
The bash documentation reads:

PATH
Search path for the command. A colonized list of directories where the shell searches for commands (see command execution below). A directory name with a length of 0 during PATH indicates the current directory. An empty directory name can be either two colons side by side or represented by a leading or trailing colon. The default path is system dependent and is configured by the system administrator who installed bash. The typical value is `/usr/gnu/bin:/usr/local/bin:/usr/ucb:/bin:/usr/bin'.

Use the export command to set the environment variable. Add /usr/local/bin separated by /usr/local/bin

#exportPATH=$PATH:/usr/local/bin
# which git
/usr/local/bin/git

The value set by the export command is discarded when logged off, so it is written in through /.bashrc.

#vim to /.bash_profile
(omitted)
export PATH=$PATH:/usr/local/bin

sudo-s or manually load to reflect.

#.~/.bashrc←Read manually
# which git
/usr/local/bin/git
$sudo-s
# which git
/usr/local/bin/git


2022-09-30 17:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.