Running Ubuntu.
Installed Google Cloud SDK via:
$ sudo curl https://sdk.cloud.google.com | sudo bash
$ exec -l $SHELL
Running "gcloud" works just fine.
Running "sudo gcloud" results in the following error:
sudo: gcloud: command not found
Oddly, when I installed Google Cloud SDK via apt-get, "sudo gcloud" works just fine. Unfortunately I cannot use Google Cloud SDK from apt-get as kubectl does not come with it and cannot be installed with the apt-get version.
Why would "gcloud" work and not "sudo gcloud"?
EDIT 5/21/2017: The following works if I manually set the path with the sudo command. I'd rather not do this every time though.
sudo env "PATH=$PATH" gcloud
The first thing to do after successful installation is open your command line and type “gcloud” to check whether Cloud SDK has installed perfectly. Run “gcloud init”, it opens up a new browser window and asks to login into your google cloud account.
Running gcloud CLI commands You can run gcloud CLI commands from the command line and from scripts and other automations—for example, when using Jenkins to automate Google Cloud tasks. Note: gcloud CLI reference documentation and examples use backslashes, \ , to denote long commands.
First, here's a gcloud article where I found an answer for this, but I'm also going to try to summarize it here as the answer isn't totally straight forward.
If you install google-cloud-sdk using curl, by default the installation directory will be the user's home directory. Before completion it also asks "Modify profile to update your $PATH and enable shell command completion?" This will actually update the logged in user profile not the sudoers file or root user profile.
If you install gcloud manually (e.g. via a tar/gz file) as your individual user and not root, the process only adds gcloud to your users bashrc and subsequently your PATH variable. This means it is never added to your root PATH variable and isn't available to the sudo command.
When you run something such as:sudo env "PATH=$PATH" gcloud
you are effectively grabbing your path (which includes the full path to gcloud) and temporarily assigning it to the root path. This then allows gcloud to work with sudo as it exists in the root PATH variable.
A few ways of getting around this include:
JUST PICK ONE OF THESE SOLUTIONS! (There is no need to do all of them.)
1) Try re-installing gcloud via aptitude (recommended, but not applicable in your case):
sudo apt-get install google-cloud-sdk
This should add gcloud to your sudoers path in the process. Check here for the full process if the command does not work.
2) Manually add the gcloud path to the sudoers secure_path variable:
sudo vi /etc/sudoers
Verify the installation directory for google-cloud-sdk and append your path to the end of the secure_path variable as below:
secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:{YOUR_PATH_TO_GCLOUD}"
In your case, since you installed with sudo curl, this would be:
secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/home/<user-name>/google-cloud-sdk/bin"
3) Add the Path of gcloud to your /etc/environment file:
Here instead of your sudoers file you're editing your /etc/environment
file. (Honestly, I wouldn't recommend this as it seems overkill).
4) Symlink to existing path directories:
You might also look to symlinking gcloud into one of the existing directories in the path, but I've gotten far enough to figure out which would be best. I'll update my answer with any feedback I get here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With