Difference between the commands gcloud compute ssh and ssh
gcloud compute ssh installs the key pair as ~/.ssh/google_compute_engine[.pub]. ssh uses ~/.ssh/identity[.pub] by default.
You can either copy the google key pair to the default name:
$ cp ~/.ssh/google_compute_engine ~/.ssh/identity
$ cp ~/.ssh/google_compute_engine.pub ~/.ssh/identity.pub
or you can just specify the private key to use when you invoke ssh:
$ ssh -i ~/.ssh/google_compute_engine instance_name
gcloud compute ssh and gcloud compute copy-files return code propagation
It appears as though copy-files
does ignore the return code of scp
. I've filed an internal feature request to propagate it correctly (gcloud compute ssh
does pass through the return code).
In the meantime, you can use the --dry-run
flag with gcloud compute ssh
and gcloud compute copy-files
to see the equivalent scp
/ssh
command lines, and call those directly.
How to use autossh with google compute (`gcloud compute ssh`)
A replacement for the command line
Here is the two commands that allow you to use autossh:
gcloud compute config-ssh
autossh -M9042 mymachine.myzone.myproject
In my opinion, this is the easiest way to do it. The first line export the ssh key and configuration of your machines, and the second just use this alias.
More on ssh configuration
This also allow you to use the shortcut mymachine.myzone.myproject
for scp, ssh, and all ssh related tools. Indeed, the list of your machines are exported in your ~/.ssh/config
.
This is a list of aliases you can also configure by hand.
For more information on gcloud's config-ssh
, you can refer to the official documentation.
Notice that some shell environment will provide you with ssh command autocompletion, which is really helpful when it comme to remembering the name of your machines. Here is a reference for zsh.
Is there a way to execute commands remotely using gcloud compute ssh utility
As long as you can ssh onto the instance, you should be able to:
gcloud compute ssh .... --command="bash -s" <<EOF
echo "Hello Freddie"
ls -l
EOF
You can test this (thanks to gcloud
consistency using Cloud Shell):
gcloud alpha cloud-shell ssh --command="bash -s" <<EOF
echo "Hello Freddie"
ls
EOF
NOTE you may not need the
-s
but I think it's preferred
Yields:
Automatic authentication with GCP CLI tools in Cloud Shell is disabled. To enable, please rerun command with `--authorize-session` flag.
Hello Freddie
Projects
README-cloudshell.txt -> /google/devshell/README-cloudshell.txt
stackoverflow
gcloud compute execute command remotely
Try:
$ gcloud compute ssh --zone ZONE INSTANCE -- 'cd /tmp && python some.py'
From gcloud compute ssh --help
:
[-- IMPLEMENTATION-ARGS ...]
Flags and positionals passed to the underlying ssh implementation.
The '--' argument must be specified between gcloud specific args on the
left and IMPLEMENTATION-ARGS on the right. Example:
$ gcloud compute ssh example-instance --zone us-central1-a -- -vvv \
-L 80:%INSTANCE%:80
Google Compute Engine VM - set up SSH tunnel through Python using google cloud API?
As clarified by John in the comments; the API is not meant for this task. Instead, I was able to use the sshtunnel
library to simply replicate the behavior of the gcloud command:
from sshtunnel import SSHTunnelForwarder
server = SSHTunnelForwarder(
"<server_ip>",
ssh_username="<username>",
local_bind_address=("0.0.0.0", 5000),
remote_bind_address=("0.0.0.0", 5000),
)
server.start()
print(server.local_bind_port) # show assigned local port -> 5000
print(server.is_active) # -> True
# Communicate with the server...
server.stop()
Related Topics
Extract/See Content of a Specific File Inside a .War File
Linux Select() and Fifo Ordering of Multiple Sockets
How to Clone a Git Repository Which Is Present on a Remote Linux Server into Windows
How to Do In-Memory Binary Search in Bash
How to Create Files with Special Characters in Linux
Save The Result of Ls Command in The Remote Sftp Server on Local Machine
Required Alignment of .Text Versus .Data
Killing Stanford Core Nlp Process
How to Continously Run a Unix Script in Background Without Using Crontab.
Command and Script to Re-Read a File in Gnuplot
Mongodb Service Fails After Reboot
Remove The Lines Starting with a Character in Shell
Youcompleteme Can't Find Local Header Files
Syscall Hooking on Kali Linux (Kernel Version 5)
"The Launch Timed Out and Was Terminated" Error with Bumblebee on Linux