Skip to content

Instantly share code, notes, and snippets.

@kcubeterm
Last active November 19, 2024 06:59
Show Gist options
  • Save kcubeterm/7e2792366bd6e8c14064f7e2ac109cc7 to your computer and use it in GitHub Desktop.
Save kcubeterm/7e2792366bd6e8c14064f7e2ac109cc7 to your computer and use it in GitHub Desktop.
How to install google cloud sdk in termux and run cloud shell interactively.

Now Google cloud sdk support python3.9 as well. Lets start.

Without any surprise,use your copy-paste skills to fire following script into your innocent terminal.

pkg install python3 openssh
curl -o sdk.sh sdk.cloud.google.com
chmod +x sdk.sh
./sdk.sh --install-dir=$PREFIX
Previous version with python2

python 2.7 requires because gcloud does't support python 3.9

pkg install python2 openssh

export CLOUDSDK_PYTHON='python2.7'
echo "export CLOUDSDK_PYTHON='python2.7'" >> ~/.bashrc
echo "export PATH=$PATH:$PREFIX/google-cloud-sdk/bin"
curl -o sdk.sh sdk.cloud.google.com
chmod +x sdk.sh
./sdk.sh --install-dir=$PREFIX
Now just attend few questions and authenticate yourself. reload shell so that all Commmand reloads in your PATH

gcloud auth login

copy link and paste in your default browser, Choose account and copy code then fill that in terminal. now done. Let's do ssh in cloud shell.

gcloud cloud-shell ssh

@kcubeterm
Copy link
Author

kcubeterm commented Jul 5, 2021

Prevent from getting kills the vm after 20 minutes of inactivity by simple ssh config/trick

We are aware of limitations of Google cloudshell.

  • VM gets kills after 20 minutes of inactivity.(Hard)
  • 5 GB persistent $HOME storage (Hard)
  • 12 Hours maximum session period. (Hard)
  • 50 hours per week of VM time.(soft)

There are also hidden limitations. Probably more !!!

* You have a hidden network quota. After reaching it, your VM will be force terminated. If you restart it multiple times but still continue to use a lot network, e.g. uploading 2-4 GB somewhere like transfer.sh, your ability to use cloud-shell will be disabled for some time.

Among them. Let's deal with first one. First of all understand what does google cs mean by inactivity. In case of cloudshell inactivity means there's no data transfer between client and server. If it persist till 20 minutes, Your VM gets kills. So if you don't want to get kill. keep working on it or do something which transfer some bytes of data to/from server.

Simply put these two lines in ssh config and done. Either in global config ($PREFIX/etc/ssh/ssh_config) or user config {~/.ssh/config)

ServerAliveInterval 120
ServerAliveCountMax 40

The first lines enable ssh to send some null bytes to server(cloudshell) once per 2 minutes and second lines count how many times have to do. Read about it more in manpages or duck it. Adjust number as per your needs.

Note: These setup may keep your VM alive until you don't kill it manually or gets disconnect as a result your 50 hours quota may waste. Also it don't gonna prevent you from 12 hours limits. VM still gets killed after 12 hours but you can connect again.

Disclaimer: Google collects and keep eyes on your cloudshell activities so don't think of abusing it better use for development purpose. It exist especially for those who use it for development purposes.

Copy link

ghost commented Jul 9, 2021

You don't need to use "alpha" components for connecting to cloud shell.

gcloud cloud-shell ssh

Works as well, if you have up-to-date sdk.

Also it perfectly works with python3.9.

We are aware of limitations of Google cloudshell.

There also hidden limitations. You have a hidden network quota. After reaching it, your VM will be force terminated. If you restart it multiple times but still continue to use a lot network, e.g. uploading 2-4 GB somewhere like transfer.sh, your ability to use cloud-shell will be disabled for some time.

@kcubeterm
Copy link
Author

Updated. Thanks @xeffyr

Copy link

ghost commented Jul 24, 2021

This may be useful for everyone who wants to run GUI apps with vnc hopefully this helps

To run GUI applications on gcloud, we should port forward port 5901 through openssh from guest to host

  1. pass openssh flag -L to locally forward 5901 for vnc:
gcloud cloud-shell ssh --ssh-flag="-L 5901:localhost:5901"
  1. on debian shell, we can install vnc server and a desktop environment of our choice. we'll use KDE for example:
sudo apt install task-kde-desktop dbus-x11 tigervnc-standalone-server
  1. run vnc server:
vncserver -xstartup /usr/bin/startkde :1
  1. now in vnc viewer app, connect to localhost:5901, then enter a password if necessary
  2. enjoy!

Copy link

ghost commented Jul 24, 2021

gcloud cloud-shell ssh --ssh-flag="-L 5901:localhost:5901"

Copy link

ghost commented Jul 25, 2021

@xeffyr done

Copy link

ghost commented Aug 1, 2021

I guess it would be worth to note that Google Cloud Shell is not a virtual machine. That's a Docker container which imply certain limits.

You can't use systemd.

leonid_pliushch@cloudshell:~$ sudo systemctl status
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

Your file system is overlay. Besides 5GB in $HOME, you have some free space in rootfs. But you won't be able to reclaim it since you don't have access to the root layer of overlay.

overlay on / type overlay (rw,relatime,lowerdir=/var/lib/docker/overlay2/l/JXR4BHW6RRI3BWJRVLBCU4VBNN:/var/lib/docker/overlay2/l/GZJMALD4PSXNAOGNWPXRRIHIFD:/var/lib/docker/overlay2/l/SF6PKKCLPHMPMGSFDUPLLCKL7C:/var/lib/docker/overlay2/l/KGNCHUNEYO3UNJPVFRKVEXOJCM:/var/lib/docker/overlay2/l/4H54T42NHPV5GY665B7BLN3HN6:/var/lib/docker/overlay2/l/MOSY5SI7HN6R7LNMO3Z7FWL3VL:/var/lib/docker/overlay2/l/W3MRKRAUMVE3A2AOFNEVHJ33FS:/var/lib/docker/overlay2/l/6FPCAE75DMC5U3DWR2R73H2VYZ:/var/lib/docker/overlay2/l/UTQP7G7AMGFMAGFVRMVITF4HAP:/var/lib/docker/overlay2/l/NEES3LWI4DEKJI53X7PN5LGVDK:/var/lib/docker/overlay2/l/Y3GE46LGPMNIOOMGCRAVSYKMI4:/var/lib/docker/overlay2/l/D5EWHJYZD45GBQFMMFIKGH7ROF:/var/lib/docker/overlay2/l/GMH2PLB2TCTFC7CKW7JFU2NDSV:/var/lib/docker/overlay2/l/RO6V77F7RKJJDQ7TNJXLMIJEAS:/var/lib/docker/overlay2/l/M5VU6YV3B4DRJHI55JVBY65RHE:/var/lib/docker/overlay2/l/XQHQEFTZXFP5DPKDYX2WJU5OVM:/var/lib/docker/overlay2/l/B7MMG7ZQCVLWHHUDX75QQCKQKZ:/var/lib/docker/overlay2/l/EZCJECPR6XTDVTUTE4SBYD4KG6:/var/lib/docker/overlay2/l/CK62YCBRSBBYUNF76HSV5MSEAF:/var/lib/docker/overlay2/l/YT3CTVJUWLELK7N4PTXIAPKEYV:/var/lib/docker/overlay2/l/RS2YPN36U3QRQL4SWEF25V7XX3:/var/lib/docker/overlay2/l/XQ7J3QKHV52PMSV7CIBVQBC4OG:/var/lib/docker/overlay2/l/AVLK3R6MMACN6RFLJASGRSYLKP:/var/lib/docker/overlay2/l/XKBNDBVP7R4UGRGD3TTT3QDCWL:/var/lib/docker/overlay2/l/LGK6F4ZZJEQEUOZSR6YRDTKNDN:/var/lib/docker/overlay2/l/KXIMC2OFMU6K4CZNFS5FYVC4UR:/var/lib/docker/overlay2/l/PP4RTGBMBUD7AQPD5IQ7Z7KTE3:/var/lib/docker/overlay2/l/UR3G67ZT4RCBMD4RZONJ7HNGXK:/var/lib/docker/overlay2/l/MOI6HYBMR44LF5NK3KUQG7A5WG:/var/lib/docker/overlay2/l/2K3UNTCL3EJLS4AWFAK4V3GRZK:/var/lib/docker/overlay2/l/EZXFAAR7PFLSBYKEWDT3O7FDQA:/var/lib/docker/overlay2/l/N55AS6NYYRZWLTDSNNZ7ELXZAE:/var/lib/docker/overlay2/l/MKS2VHR6764BV32HF3ARHJA6YK:/var/lib/docker/overlay2/l/G2GQY6FRJMVBSKVHA5IWYF5Y33:/var/lib/docker/overlay2/l/XAKOSUFAM6GZOECAGZFDMV6ENJ:/var/lib/docker/overlay2/l/VHCTGH3NUPQ3GJ5Z7YFGTAWL6W:/var/lib/docker/overlay2/l/2US2KIVSJLR2RWUBEJTYVSYNRX:/var/lib/docker/overlay2/l/6LXKAGTMMTUA7W63WIN7NT22WV:/var/lib/docker/overlay2/l/WU7LPZFHCZLMT4I3M6TZTOFIMG:/var/lib/docker/overlay2/l/4U6VAGUJ5BOFZYHD4OGMKHQBJR:/var/lib/docker/overlay2/l/356QSRGSDW6XEKVLA6JA6ULX2K:/var/lib/docker/overlay2/l/TN6D54FURUKMXRYLRKYRRLBZIQ:/var/lib/docker/overlay2/l/G7ICORLIUCES7VHJH2H2WIO4TA:/var/lib/docker/overlay2/l/VA442ZVKCHEER35KNZHO6MWJY2:/var/lib/docker/overlay2/l/AFKORDN6VQGTZ53FR25BVIVMFN:/var/lib/docker/overlay2/l/4F3JHKBMGWIFGCLAJKOAL45YSD:/var/lib/docker/overlay2/l/CDMS37OOYPZL2LLBUPLCYFTR4K:/var/lib/docker/overlay2/l/HWOKY7QGO7ROXMYITKUSHUITAZ:/var/lib/docker/overlay2/l/BFGXTD5H2GOLGBI2C4NLT2DCQ7:/var/lib/docker/overlay2/l/2GGYROMJRLNBA6X25FLQFOJOVH:/var/lib/docker/overlay2/l/7TI3VX53O4JDDXLGBQICNW3ASS:/var/lib/docker/overlay2/l/WJI2SQSPMLOOVUKYZWHFKOTLYK:/var/lib/docker/overlay2/l/4ZDUDNBMHRZNSYEZ2I6OMVSYDP:/var/lib/docker/overlay2/l/LH2YCBWBI37X2LXP7Z5CMORUH4:/var/lib/docker/overlay2/l/L7XUQMGD22VA3RT7NUYADT5DKT:/var/lib/docker/overlay2/l/UUNLJKRGKQGZCGEFARIIXLZM66:/var/lib/docker/overlay2/l/VQBY2YM27PO2KT23M7XFS7TGNZ:/var/lib/docker/overlay2/l/UVFI3H7YFQINMW4ISZ5JWCRKNN:/var/lib/docker/overlay2/l/AT5KBWRA6W27BWGOPUPO6YGI7W:/var/lib/docker/overlay2/l/LBAJP5WOUYAYHAHA6FCH7C4NSA:/var/lib/docker/overlay2/l/Q2PMCY7C6NSGNG2BND7YOZQV7I:/var/lib/docker/overlay2/l/F2VBAYEXR52SJGVBRHNC2P5QGX:/var/lib/docker/overlay2/l/ROSXNJPZNYVBJYRY34QOVGJLNJ:/var/lib/docker/overlay2/l/VAJ6O6TIH2XVOGMUIX22ZAEFE7:/var/lib/docker/overlay2/l/YCWGRL6H2S2P4ODYMDE7NWQ3HL:/var/lib/docker/overlay2/l/E2PLTF56L7OZN6HUI4CPUXT5WA,upperdir=/var/lib/docker/overlay2/6fb2807639dfb060222c4bf10a1630bbb6fd9b4e4413f647448d337ac72dea8d/diff,workdir=/var/lib/docker/overlay2/6fb2807639dfb060222c4bf10a1630bbb6fd9b4e4413f647448d337ac72dea8d/work)

@Yonle
Copy link

Yonle commented Nov 1, 2021

By the way, During installation, You can skip this part to save space:

Welcome to the Google Cloud SDK!

Do CTRL + C to skip that part. Then link gcloud to $PREFIX/bin

ln -s $PREFIX/google-cloud-sdk/bin/gcloud $PREFIX/bin/gcloud

And execute gcloud. Your gcloud SDK is ready to use.

@BenVella
Copy link

BenVella commented Feb 1, 2024

╭─ ~/projects/gcloud ─────────── ✔  3s  21:57:02 ─╮
╰─ . ./sdk.sh --install-dir=$PREFIX                  ─╯
Downloading Google Cloud SDK install script: https://dl.google.com/dl/cloudsdk/channels/rapid/install_google_cloud_sdk.bash
################################################# 100.0%
Running install script from: /data/data/com.termux/files/usr/tmp/tmp.t1AK0OyurM/install_google_cloud_sdk.bash
which curl
/data/data/com.termux/files/usr/tmp/tmp.t1AK0OyurM/install_google_cloud_sdk.bash: line 135: which: command not found
which wget
/data/data/com.termux/files/usr/tmp/tmp.t1AK0OyurM/install_google_cloud_sdk.bash: line 135: which: command not found
Either curl or wget must be installed to download files.
╭─ ~/projects/gcloud ────────────── 1 ✘  21:57:05 ─╮
╰─ which curl                                        ─╯
/data/data/com.termux/files/usr/bin/curl

I'm seeing this which not found error even though it works just fine in the following request. It seems like whichever environment the Gcloud sdk install is happening in is outside the usual prompt. Tried prefixing a . with no success as well. Any thoughts?

Edit: Later realised how silly this issue is, installing which resolves it 😅

@Yonle
Copy link

Yonle commented Feb 2, 2024

pkg install -y which

@mobagenie
Copy link

mobagenie commented Aug 28, 2024

amazing! finally i g rid from absolutelly nightmare cloud browser based cli and also app based cli. now i can control my cloud shell from termux CLI!!!

Screenshot_2024-08-28-22-01-01-387_com termux

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment