How to add ssh key to project in GCP - ssh

I am trying to add my public ssh-key to my project but can't seem to make it work.
According to documentation : https://cloud.google.com/compute/docs/instances/adding-removing-ssh-keys?hl=fr#project-wide ; after creating my ssh txt file I need to use the command :
gcloud compute project-info add-metadata --metadata-from-file ssh-keys=[LIST_PATH]
I named my ssh txt file "ssh.txt" and my full path was to file was : C:\Users\33768\Desktop\ssh.txt .
I tried the following commands :
gcloud compute project-info add-metadata --metadata-from-file ssh-keys=C:\Users\33768\Desktop\ssh.txt
gcloud compute project-info add-metadata --metadata-from-file ssh-keys="C:\Users\33768\Desktop\ssh.txt"
gcloud compute project-info add-metadata --metadata-from-file ssh-keys=C:/Users/33768/Desktop/ssh.txt
gcloud compute project-info add-metadata --metadata-from-file ssh-keys="C:/Users/33768/Desktop/ssh.txt"
Yet, none of them worked, error being :
ERROR: (gcloud.compute.project-info.add-metadata) Unable to read file [LIST_PATH]: [Errno 2] No such file or directory: [LIST_PATH]
where I replaced the actual list path that command line tool showed by [LIST_PATH].
Please someone help, I am getting crazy.
Thx.

Thanks everyone.
I actually found why it was not working. I used ubuntu for windows and that messed up the path of my folders. Inside the ubuntu terminal, I do not have access to folder in my local machine that are not on my ubuntu folder.
Just ran my commands outside of ubuntu terminal and it worked !

From your post I see you use Windows. I don't know how you generate your keys so I will write down everything starting from this procedure.
Download Putty (it also includes PuttyGen). Run PuttyGen, change "comment" field to your username#somemachine and click "Generate" button. After some "mouse moving" you will get your personal SSH-key which looks something like this:
ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEAtJqgJA1MLB7ZqIL+xF0cnZaXyGW9LYxlyj/JrK/eOkgvRN36zI7xJc1ML5uO2Hn+EPiTwKO5+0xmwomZKnu2nrCsuZzQZakGWHiyKBYSQ1x+l+PqISOniiHOGTHc0p//lwbCLKO7bUUYuS2+7Uw3lNhKytnNA7WbcfMmm+NTH2C8ZdWptWaGmX/Yt1kdUKFCyTLAlXqdoNyr4QssdaMo4BY07JUrYHGN8Uzt7/Knd6zqqsK4Hzf0lTzxYdiuP3Y6qYBcAMtLs7iaEibu8r/i1Js7DpSHQTUYbQ6lWBk7p1yI8XJ809FTXLy20doF3ElQjBrqk/dkDk1p3AV2RlplYQ== username#somemachine
Click "save public key" and save it on some directory. After that "save private key" - ideally to the same directory.
You have your keys generated and saved.
Now - add them to your GCP project. Easiest ways are:
console - go to Compute Engine > Metadata > SSH Keys - click "Edit" button and then "Add item". Copy your entire SSH key string into this box and click "save".
gcloud SDK - gcloud compute project-info add-metadata --metadata-from-file ssh-keys=your_file_name/name/and/path - when adding you might get a warning The following key(s) are missing the <username> at the front - don't worry - they will still work.
If in doubt just read the documentation how to add private SSH-keys to your GCP project. It's a bit long but everythin's explained in detail.
Furthermore - I generated keys on Linux, added them the same way to my project and it also works.

#John Hanley: [LIST_PATH], or ssh.txt in this case, is the concatenation in a single file of all the public keys (.pub) to upload as metadata.

Related

Copying ssh key from windows machine to windows server 2019

I've been trying to get access to Windows Server 2019 without password through OpenSSH protocol.
So I've created new key which I need it to be copied to the Windows Server, I've tried this:
ssh-copy-id -i ~/.ssh/id_rsa user#server
But I get this after entering correct password:
'exec' is not recognized as an internal or external command,
operable program or batch file.
The system cannot find the path specified.
The system cannot find the path specified.
My issue is how to transfer key from one windows machine(using gitbash, WSL, powershell or whatever)
to Windows Server 2019 location of authorized keys if I am not mistaken.
I am desperate enough to do it manually but location of those keys is mystery to me, do I need to set something on Windows Server first so that it can accept keys for authentication ?
What is the alternative on ssh-copy-id from Windows machine to Windows Server 2019 ?
Found solution:
Followed this helpful youtube guide, props to the
https://www.youtube.com/watch?v=Cs3wBl_mMH0&ab_channel=IT%2FOpsTalk-Deprecated-SeeChannelDescription
Also, installing OpenSSHUtils worked with:
Install-Module -Name OpenSSHUtils -RequiredVersion 0.0.2.0 -Scope AllUsers
Also this guide helped:
https://www.cloudsma.com/2018/03/installing-powershell-modules-on/
My server didn't have access so I manually copied file from:
C:\Program Files\WindowsPowerShell\Modules to the server's:
Server:\Program Files\WindowsPowerShell\Modules
First, this error message is followed by microsoft/vscode-remote-release issue 25
Current workaround (the context is VSCode, but should apply also for regular SSH connection):
Also, for anyone else here that loves their bash on windows but still wants to be able to use VSCode remote, the workaround I have currently setup is to use an autorun.cmd deployed on the servers that detects when an SSH connection is coming in and has a terminal allocated:
#echo off
if defined SSH_CLIENT (
:: check if we've got a terminal hooked up; if not, don't run bash.exe
C:\cygwin\bin\bash.exe -c "if [ -t 1 ]; then exit 1; fi"
if errorlevel 1 (
C:\cygwin\bin\bash.exe --login
exit
)
)
This is known to work with Cygwin bash, unsure about bash that ships with windows; I imagine it's very sensitive to how the TTY code works internally.
This way, launching cmd.exe works normally, using VSCode (because it does not allocate a PTY) works normally, but SSH'ing into the machine launches bash.exe.
I suspect it would also work using the bash.exe which comes with Git for Windows, should it be installed on the target server.
The destination file should be on the server:
%USERPROFILE%\.ssh\authorized_keys
If you can do it manually, simply try and scp it instead of using ssh-copy-id
scp user#server:C:/Users/<user>/.ssh/authorized_key authorized_key
# manual and local edit to add the public key
scp authorized_key user#server:C:/Users/<user>/.ssh/authorized_key
(again, I would use the scp.exe coming with Git For Windows, installed this time locally)
Found solution:
Followed this helpful youtube guide, props to the
https://www.youtube.com/watch?v=Cs3wBl_mMH0&ab_channel=IT%2FOpsTalk-Deprecated-SeeChannelDescription
Also, installing OpenSSHUtils worked with:
Install-Module -Name OpenSSHUtils -RequiredVersion 0.0.2.0 -Scope AllUsers
Also this guide helped:
https://www.cloudsma.com/2018/03/installing-powershell-modules-on/
My server didn't have access so I manually copied file from:
C:\Program Files\WindowsPowerShell\Modules to the server's:
Server:\Program Files\WindowsPowerShell\Modules

ls doesn't show any output in ssh connected to an Debian 9 VM instance in Google Cloud Platform

this might be a dumb question, but I checked everywhere and there's no direct answer to it.
I set up both SSH keys successfully and I can connect to my instance via terminal, but when I do "ls", it doesn't show me any output. I am using iTerm2 with zsh on my Mac but I don't think this is an issue.
Can anybody give me a hint? Thanks!
When you access a VM through SSH, your working directory is the home directory of the user specified with the SSH command, i.e. /home/username. In case you access as root, the working directory will be /root.
You can check it through the command pwd
If it is a brand new machine, it is normal that the output of 'ls' is empty since in your home directory no file matches the filters of 'ls' with no parameters. The reason is that 'ls' doesn't show filenames starting with a dot ('.') because in the Linux convention they are hidden unless you run ls -al.
You can try again with $ ls -al and you will be able to see hidden files and directories as well.
On the other hand you can create as well first an empty file and then running again 'ls':
$ touch file
$ ls

Using Gitlab deploy keys with write access

I am currently running CE version 8.17.4 and am attempting to setup a deploy key with write access (as of 8.16) so that my runner instance may commit build artifacts back to the repository. I took the following steps to set this up:
On the runner instance, I generated the ssh keypair with the command: 
sudo ssh-keygen -t rsa -C "label" -b 4096
The generated keypair was saved to /home/gitlab-runner/.ssh/id_rsa and password protected.
Within Gitlab, I created a public deploy key from the admin console and pasted the contents of id_rsa.pub into the appropriate field and verified that the key fingerprints matched. I checked the "Write access allowed" box. 
In the private project that I wished to enable repository access from the runner, I enabled the newly created public deploy key.
This is a LaTeX document respository, so in the .gitlab-ci.yml file, I issue the following script after building the pdf:
after_script:
  - "git commit -am 'autobuild PDF'"
  - "git push origin master"
When the changes were committed, the build ran successfully on the runner up until the git push origin master command, and this error was thrown:
fatal: Authentication failed for 'http://gitlab-ci-token:xxxxxxxxxxxxxxxx#host/project.git/'
Ok. A couple questions:
If the deploy key is just an SSH key, shouldn't it be connecting on the secure port or does this matter? I haven't found much documentation on using this new write-permission deploy key feature, so am I missing something in the steps I took above?
Do I need to include [ci skip] in the commit message to avoid looping CI builds? I saw this concern come up in the original issue tickets for this feature, but did not see whether this step was required or not. 
Thanks for any help!
Jawad's comment worked for me: you need to force SSH. for example
git remote add ssh_remote git#host:user/project.git
git push ssh-remote HEAD:dev
thanks jawad

How to find Private Key Location

I'm trying to access a server using Filezilla and was told I needed to use authentication with public/private keys. I created the keys using the Terminal, but cannot find them on my computer.
This is where the key is located:
(/Users/ed/.ssh/id_rsa)
I checked in my home directory, but the folder .ssh is nowhere to be found. Is there a secret place .ssh folder is stored and how can I access it?
My Mac runs on OS X ElCaptain. I would really appreciate any help.
Files and folders starting with a period (.ssh) are hidden by default. To find private/public key, run this commands:
ls -a
In your case, run this commands to find the ssh keys:
cd ~/.ssh
then:
ls -a
Now you should see the keys like this:
. .. id_rsa id_rsa.pub
If the keys are not there then definitely you need to create the key by ssh-keygen command.
(MacOS) I my case, .ssh folder was hidden so I went to folder route (example /Users/syed.dastagir)and pressed Command + Shift + . (full stop/period) and it showed me .ssh folder.
When you no longer want to see the hidden folders just press Command + Shift + . again.
If using Finder, I used defaults
Terminal
write com.apple.Finder AppleShowAllFiles true [Press Return]
killall Finder
Then I was able to see .ssh directory in Finder at
/Users/MYUSERNAME/.ssh
when referencing the private key name, when setting up the config file for GitHub for example ( https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#adding-your-ssh-key-to-the-ssh-agent ), I thought I needed an id with number, i.e 'id_43248390', however I just needed to reference 'id_rsa'.
This is incase any gets blocked similarly.

How assure that gnome-terminal is displaying the correct hostname on window title?

I am looking for a solution that would update the window title to the current host.
I am usually doing ssh to different boxes and I observed that the window title in Gnome Terminal (3.0.1 from Ubuntu 11.00) is not correctly updated. Currently it displays "user#localcompure: path" - and I want to be updated after I do a ssh.
I should note that I am looking for a solution that will not require me to change settings on any machine I'm connecting to.
I'm looking to do the same here, the functionality works fine in konsole(kde's terminal app) but not from within gnome-terminal. The best solution I have found thus far is to invoke this by using a separate app with the following:
#!/bin/bash
#!/bin/bash
SETTP='MY_PROMPT="$HOSTNAME:$PWD\$ "'
SETTP="$SETTP;"'MY_TITLE="\[\e]0;$HOSTNAME:$PWD\a\]"'
SETTP="$SETTP;"'PS1="$MY_TITLE$MY_PROMPT"'
ssh -t $1#$2 "export PROMPT_COMMAND='eval '\\''$SETTP'\\'; bash --login"
found and copied from:
https://unix.stackexchange.com/a/40337?sgp=2