aws ssm proxycommand for interactive session - openssh

Goal: Log into ec2 instance via SSM to an ssh terminal using a proxycommand
I can log into my instance fine using 'aws ssm start-session --target intance-id'. However to make life simpler, I want to be able to have a proxycommand in my .ssh/config that will allow me to ssh into the instance with a shorter command such as 'ssh i-awsguid' and the proxycommand will fill in the rest.
However, when I do this, I get a session that hangs without ever seeing the prompt of the remote machine, and on control-c, I see the json output of the aws action.
$ ssh i-076efab920fd7a5e2 -v
OpenSSH_8.1p1, LibreSSL 2.7.3
debug1: Reading configuration data /Users/user/.ssh/config
debug1: /Users/user/.ssh/config line 49: Applying options for *
debug1: /Users/user/.ssh/config line 70: Applying options for *
debug1: /Users/user/.ssh/config line 86: Applying options for i-*
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 47: Applying options for *
debug1: Executing proxy command: exec aws ssm start-session --target i-076efab920fd7a5e2
debug1: identity file /Users/user/.ssh/id_rsa type 0
debug1: identity file /Users/user/.ssh/id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.1
debug1: kex_exchange_identification: banner line 0:
debug1: kex_exchange_identification: banner line 1: Starting session with SessionId: user#email.com-0a7ca620b0b70ba7c
debug1: kex_exchange_identification: banner line 2: This session is encrypted using AWS KMS.
debug1: kex_exchange_identification: banner line 3: echo Connected to $(hostname)
debug1: kex_exchange_identification: banner line 4: $ Connected to ip-10-147-34-181
^C
Command '['session-manager-plugin', '{"SessionId": "user#email.com-0a7ca620b0b70ba7c", "TokenValue": "scrubbed", "StreamUrl": "wss://ssmmessages.us-east-1.amazonaws.com/v1/data-channel/user#email.com-0a7ca620b0b70ba7c?role=publish_subscribe", "ResponseMetadata": {"RequestId": "cd75db36-3cc0-415f-b849-b5995e1701ec", "HTTPStatusCode": 200, "HTTPHeaders": {"server": "Server", "date": "Wed, 07 Apr 2021 17:49:22 GMT", "content-type": "application/x-amz-json-1.1", "content-length": "678", "connection": "keep-alive", "x-amzn-requestid": "cd75db36-3cc0-415f-b849-b5995e1701ec"}, "RetryAttempts": 0}}', 'us-east-1', 'StartSession', '', '{"Target": "i-076efab920fd7a5e2"}', 'https://ssm.us-east-1.amazonaws.com']' died with <Signals.SIGPIPE: 13>.
example calling same instance directly from shell.
$ aws ssm start-session --target i-076efab920fd7a5e2
Starting session with SessionId: user#email.com-0bcfc5bf3d8325cb1
This session is encrypted using AWS KMS.
echo Connected to $(hostname)
$ Connected to ip-10-147-34-181
$ whoami
ssm-user
$ exit
Exiting session with sessionId: user#email.com-0bcfc5bf3d8325cb1.
relevant part of .ssh/config
# SSH over Session Manager
host i-*
ProxyCommand aws ssm start-session --target %h

I think the problem is that your proxy command is the aws cli. The JSON output does say that the command succeeded. But you haven't asked for a shell, you just asked for executing an aws command.
According to the AWS docs on SSH sessions through SSM, you pass the aws command as an argument to a shell:
sh for MacOS and Linux (I presume sh exists in MacOS as in Linux)
Windows PowerShell for Windows (C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe), although I guess that it would work with the command line
Also, I only got it working by
having a SSH private key on my PC
having my SSH public key in my VM's ~/.ssh/config/authorized_keys
specifying my username in my VM in the host config
specifying an AWS document in the proxy command, AWS-StartSSHSession
passing the port number variable %p
Having to use my username and a SSH key may be because my IAM account does not have the right permissions, so it's not a perfect solution. (But I don't control those permissions in my workplace.)
Judging by the debug info, you're on a Mac, right? So your .ssh/config host should be:
# SSH over Session Manager
# you may need the region as a parameter to aws, e.g. --region us-east-1
host i-*
User YourUsername
ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"
In my case, in PowerShell, since I have to use many AWS accounts, I specify the right AWS profile in the proxy command:
# SSH over Session Manager on a specific hostname
# using PowerShell and a specific AWS profile
Host myVM
HostName i-xxxxxxxxxxx
User myusername
ProxyCommand C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "aws ssm start-session --profile myAwsProfile --target %h --document-name AWS-StartSSHSession --parameters portNumber=%p"

Related

Shh connection, in a Jenkins pipeline, disconnects after login

Scenario: I'm developing a jenkins step that needs to transfer a file to a machine (install a jboss module). I'm trying to do it via ssh interactions. I need to connect via ssh, switch to an authorized user in order to access jboss folders/files, and then use rsync to transfer the jar file inside jboss modules folder. I cannot use the same user to ssh and jboss.
Problem: I can successfully connect via ssh, but when I send the first command (to switch user), it disconnects and then nothing works anymore. Appearently is disconnecting before the 'su' command is executed. The next command would be to check if module folder exists (and create it if doesn't).
The sequence of commands is executed inside a function:
def installModule(HOST, USER, PASSWORD) {
sh set -x && sshpass -p [PASSWORD] ssh -v -tt -o StrictHostKeyChecking=no [USER]#[HOST] echo [PASSWORD] | sudo -S su - jboss && cd [MODULE_FOLDER] && if [[ ! -e [MODULE_VERSION] ]]; then mkdir [MODULE_VERSION]; fi
}
The console output:
debug1: Authentication succeeded (keyboard-interactive).
Authenticated to [MACHINE_NAME_HERE] ([IP_HERE]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions#openssh.com
debug1: Entering interactive session.
debug1: pledge: network
debug1: client_input_global_request: rtype hostkeys-00#openssh.com want_reply 0
debug1: tty_make_modes: no fd or tio
debug1: Sending environment.
debug1: Sending env LANG = en_GB.UTF-8
debug1: Sending command: echo [PASSWORD_HERE]
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
debug1: fd 2 clearing O_NONBLOCK
Connection to [MACHINE_NAME_HERE] closed.
Transferred: sent 2180, received 3356 bytes, in 0.3 seconds
Bytes per second: sent 7006.2, received 10785.6
debug1: Exit status 0
Sorry, try again.
[sudo] password for jenkins: Sorry, try again.
[sudo] password for jenkins:
sudo: no password was provided
sudo: 2 incorrect password attempts
Any help would be appreciated =)
I had to do something similar, in my case, I opt for having a shell script file in my environment containing all the commands I needed to be executed on the remote machine.
I did it like this:
withCredentials([
usernamePassword(credentialsId: "$VM_CREDENTIALS", usernameVariable: 'USER_VM', passwordVariable: 'PWD_VM')
]) {
script {
sh 'sshpass -p $PWD_VM ssh -o StrictHostKeyChecking=no $USER_VM#$IP_VM "bash -s ' + "$VARIABLE_A $VARIABLE_B" + '" < path/to/shell/script.sh'
}
}
I used $VARIABLE_A and $VARIABLE_B to pass some arguments to the script. The $path/to/shell_script.sh represents the path to the script placed in your Jenkins environment to be executed on the remote machine.
I also had to switch users in the shell script, I did it like so:
# Switch to root user
echo $PWD | sudo -S sleep 1 && sudo -E su
Remember, don't define the $PWD variable hardcoded somewhere, you need to take security measures.

SSH shows the wrong IP address when SSH with port forward

My use case is I have to access AWS ec2 instances through a jumpbox.
Here is my SSH config.
Host awsjumpbox
User sshuser
HostName jumpboxhostname
IdentityFile /Users/myusername/.ssh/id_rsa
LocalForward 8022 10.0.168.43:22
It works when I do SCP command to copy files to the EC2 instance.
myusername % scp -r -i ~/aws/aws-keypair.pem -P 8022 * ec2-user#localhost:testdir
The authenticity of host '[localhost]:8022 ([::1]:8022)' can't be established.
ECDSA key fingerprint is SHA256:rrwr62yjP2cgUTT9SowdlrIwGi4jMMwt5x4Aj6E4Y3Y.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[localhost]:8022' (ECDSA) to the list of known hosts.
/etc/profile.d/lang.sh: line 19: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
README.md 100% 1064 24.3KB/s 00:00
However, when I executed SSH command. It returns a strange IP address.
myusername % ssh -i ~/aws/aws-keypair.pem -P 8022 ec2-user#localhost
ssh: connect to host 0.0.31.86 port 22: No route to host
What is the cause of this issue? How do I fix it?
Thank you.
Don't use LocalForward and reverse the flow.
Use ProxyCommand or ProxyJump. This will allow SSH to open a session to your bastion server transparently.
E.g. your configuration should be something in the line of
Host 10.0.168.43
User root
ProxyCommand ssh -W %h:%p sshuser#awsjumpbox
...
or
Host 10.0.168.43
User root
ProxyJump sshuser#awsjumpbox
...

SSH and SCP failed with "CreateProcessW failed error:2 posix_spawn: No such file or directory"

I am trying to do SSH and SCP to AWS managed instance. The public key of my machine is stored at that managed instance. There is no .pem file required. I am using the following commands.
scp -r username#instance-id:/pathtoremotefile localpath
scp localfilepath username#instance-id:~
ssh username#instance-id
But none of these are working in windows.The error says,"CreateProcessW failed error:2 posix_spawn: No such file or directory lost connection". I would mention that I am able to start ssm session to that instance using this command
aws ssm start-session --target instance-id.
Managed instance has linux operating system and my pc has windows.
Also, in config file of .ssh directory, I've added following lines.
host i-* mi-*
ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"
The strange thing is I am able to do both ssh and SCP in 'windows subsystem from Linux' from the same machine by using the same commands. But couldn't do that in windows command prompt.
What am I doing wrong?
This seems to be a problem of the Windows openSSH package.
In ProxyCommands it requests full path qualifiers, but better use forward slashes there!
ProxyCommand C:/somepath/sh.exe -c "aws ssm ...
I don't use the windows openSSH anymore, instead I'm using the ssh command from git-bash to avoid all the windows problems.
The key agent works perfect, proxyCommand/proxyJumps works, and in contrast to windows, it's reliable.

Github permission denied: ssh add agent has no identities

This is my first time accessing GitHub and I'm not experienced using a console. I am on a MacBook using Bash. When I try to access GitHub, I get this:
git clone git#github.com:dhulihan/league-of-legends-data-scraper.git
Cloning into 'league-of-legends-data-scraper'...
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I've tried following the instructions on Github page about permission being denied.
When I use ssh -vT git#github.com, I get the following:
OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: Connecting to github.com [192.30.252.129] port 22.
debug1: Connection established.
debug1: identity file /Users/XXXX/.ssh/id_rsa type -1
debug1: identity file /Users/XXXX/.ssh/id_rsa-cert type -1
debug1: identity file /Users/XXXX/.ssh/id_dsa type -1
debug1: identity file /Users/XXXX/.ssh/id_dsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.2
debug1: Remote protocol version 2.0, remote software version libssh-0.6.0
debug1: no match: libssh-0.6.0
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-sha1 none
debug1: kex: client->server aes128-ctr hmac-sha1 none
debug1: sending SSH2_MSG_KEXDH_INIT
debug1: expecting SSH2_MSG_KEXDH_REPLY
debug1: Server host key: RSA 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /Users/XXXX/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /Users/XXXX/.ssh/id_rsa
debug1: Trying private key: /Users/XXXX/.ssh/id_dsa
debug1: No more authentication methods to try.
Permission denied (publickey).
Next, eval "$(ssh-agent -s)" returns "Agent pid 2314",
however ssh-add -l returns "The agent has no identities."
And that is where I am stuck.
Full details in this answer.
In summary, when ssh-add -l returns “The agent has no identities”, it means that keys used by ssh (stored in files such as ~/.ssh/id_rsa, ~/.ssh/id_dsa, etc.) are either missing, they are not known to ssh-agent, which is the authentication agent, or that their permissions are set incorrectly (for example, world writable).
If your keys are missing or if you have not generated any, use ssh-keygen -t rsa, then ssh-add to add them.
If keys exist but are not known to ssh-agent (like if they are in a non-standard folder), use ssh-add /path/to/my-non-standard-ssh-folder/id_rsa to add them.
See this answer if you are having trouble with ssh-add or ssh-agent.
try this:
ssh-add ~/.ssh/id_rsa
worked for me
THE 2019 ANSWER for macOS Sierra & High Sierra & Catalina:
PS: most of the other answers will have you to create a new ssh key ... but you don't need to do that :)
As described in detail on https://openradar.appspot.com/27348363, macOS/OS X till Yosemite used to remember SSH keys added by command ssh-add -K <key>
So here are the 4 steps i had to take in order for it to work:
1: ssh-add ~/.ssh/PATH_TO_YOUR_SSH_PRIVATE_KEY (e.g. ~/.ssh/id_rsa)
2: Add the following in ~/.ssh/config
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile PATH_TO_YOUR_SSH_PRIVATE_KEY (e.g. ~/.ssh/id_rsa)
3: make sure to remove any gitconfig entry that use osxkeychain helper:
https://github.com/gregory/dotfiles/commit/e38000527fb1a82b577f2dcf685aeefd3b78a609#diff-6cb0f77b38346e0fed47293bdc6430c6L48
4: restart your terminal for it to take effect.
I have been stucked a while on the same problem, which I eventually resolved.
My problem: I could not execute any push. I could check & see my remote (using git remote -v), but when I executed git push origin master, it returned : Permission denied (publickey). fatal: Could not read from remote repository. and so.
How I solved it :
I generated a key using ssh-keygen -t rsa. Entering a name for the key file (when asked) was useless.
I could then add the key (to git): ssh-add /Users/federico/.ssh/id_rsa , which successfully returned Identity added: /Users/myname/.ssh/id_rsa (/Users/myname/.ssh/id_rsa)
I added the SSH key to github using this help page.
Having tried all the commands in Github's 'Permission denied publickey' help page, only the ssh-add -l command worked / seemed useful (after having ran the previous steps), it successfully returned my key. The last step shows you where to check your public key on your GitHub page. And this command will help you check all your keys : ls -al ~/.ssh.
Then the push command eventually worked !
I hope this will help !
Best luck to all.
Run the following commands:
ssh-keygen -t rsa
ssh-add /Users/*yourUserNameHere*/.ssh/id_rsa**
pbcopy < ~/.ssh/id_rsa.pub**
Go to your Github account : https://github.com/settings/profile
1) Click : SSH and GPG keys
2) New SSH Key and Past it there
3) Add SSH Key
Done!
tl;dr
ssh-add # no parameter
# Identity added: /home/<user>/.ssh/id_rsa (user#host)
Further readings
two files necessary in the ssh - user - folder:
ls ~/.ssh
id_rsa id_rsa.pub
If the files are not there, enter command ssh-keygen
Now start the ssh-agent:
eval `ssh-agent -s`
Verify
ssh-add -l
# voila:
2048 SHA256:<key one> user#host (RSA)
Bonus
check the local SHA256 from above with the Github SHA256 from your user settings -> SSH Keys. If they are equal you should be able to push/pull to/from Github using your keys.
first of all you need to go in your ssh directory
for this type following command in your terminal in mac or whatever you use in window
cd ~/.ssh
now it is in the ssh
here you can find all you ssh key/files related to your all projects. now, type the following command to show you if any ssh key available
ls
this will show you all available ssh, in my case there were two
now, you will need to start an agent to add a ssh in it. For this type following command
eval "$(ssh-agent -s)"
now last but not least you will add a ssh in this agent type following command
ssh-add ~/.ssh/your-ssh
replace
replace your-ssh with your ssh file name which you got a list form second step ls command
This could cause for any new terminal, the agent id is different.
You need to add the Private key for the agent
$ ssh-add <path to your private key>
This worked for me:
chmod 700 .ssh
chmod 600 .ssh/id_rsa
chmod 644 .ssh/id_rsa.pub
Then, type this:
ssh-add ~/.ssh/id_rsa
For my mac Big Sur, with gist from answers above, following steps work for me.
$ ssh-keygen -q -t rsa -N 'password' -f ~/.ssh/id_rsa
$ ssh-add ~/.ssh/id_rsa
And added ssh public key to git hub by following instruction;
https://docs.github.com/en/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account
If all gone well,
you should be able to get the following result;
$ ssh -T git#github.com
Hi user_name! You've successfully authenticated,...
One additional element that I realized is that typically .ssh folder is created in your root folder in Mac OS X /Users/. If you try to use ssh -vT git#github.com from another folder it will give you an error even if you had added the correct key.
You need to add the key again (ssh-add 'correct path to id_rsa') from the current folder to authenticate successfully (assuming that you have already uploaded the key to your profile in Git)
I had this issue after restoring a hard drive from a backup.
My problem:
I could check & see my remote (using git remote -v), but when I executed git push origin master, it returned : Permission denied (publickey). fatal: Could not read from remote repository.
I already had an SSH folder and SSH keys, and adding them via Terminal (ssh-add /path/to/my-ssh-folder/id_rsa) successfully added my identity, but I still couldn't push and still got the same error. Generating a new key was a bad idea for me, because it was tied to other very secure permissions on AWS.
It turned out the link between the key and my Github profile had broken.
Solution:
Re-adding the key to Github in Profile > Settings > SSH and GPG keys resolved the issue.
Also:
My account had 2-factor authentication set up. When this is the case, if Terminal requests credentials, use your username - but NOT your Github password. For 2-factor authentication, you need to use your authentication code (for me, this was generated by Authy on my phone, and I had to copy it into Terminal for the pw).
If you are using Linux or Windows open terminal or cmd in directory you want your keys in. Create a pair of private-public key
$ ssh-keygen -t ed25519 -C "your_email#example.com"
If you are using a legacy system that doesn't support the Ed25519 algorithm, use:
$ ssh-keygen -t rsa -b 4096 -C "your_email#example.com"
Generating public/private ALGORITHM key pair.
Enter a file in which to save the key (/c/Users/YOU/.ssh/id_ALGORITHM):[Press enter any name you like your private public keys file to be]
Enter passphrase (empty for no passphrase): [Type a passphrase or enter for without it]
Enter same passphrase again: [Type a passphrase or enter for without it]
Now, in Linux
$ sudo cp <filename without .pub ending> ~/.ssh
In Windows simply copy the <filename without .pub ending> file in this C:\Users\your_username\.ssh directory
And then
$ ssh-add ~/.ssh/<filename without .pub ending>
It may work now!
After struggling for long I was finally able to resolve this issue on Windows, For me the User env variable GIT_SSH was set to point to
"C:\Program Files(x86)\WinScp\PuTTY\plink.exe"
which was installed along with WinScp. I changed the pointing to use default ssh.exe which comes with git-scm "C:\Program Files\Git\usr\bin\ssh.exe"
Steps for BitBucket:
if you dont want to generate new key, SKIP ssh-keygen
ssh-keygen -t rsa
Copy the public key to clipboard:
clip < ~/.ssh/id_rsa.pub
Login to Bit Bucket:
Go to View Profile -> Settings -> SSH Keys (In Security tab)
Click Add Key,
Paste the key in the box, add a descriptive title
Go back to Git Bash :
ssh-add -l
You should get :
2048 SHA256:5zabdekjjjaalajafjLIa3Gl/k832A /c/Users/username/.ssh/id_rsa (RSA)
Now: git pull should work
This worked for me also:
chmod 700 .ssh
chmod 600 .ssh/id_rsa
chmod 644 .ssh/id_rsa.pub
Then, type this: ssh-add ~/.ssh/id_rsa
Sometimes it could be useful to check ~/.ssh/config
it should look like this
Host github.com
IdentityFile ~/.ssh/id_rsa
id_rsa can be different if you linked a different key.

scp from Amazon ec2 is hanging, but SSH works fine

I'm trying to copy a file from an ec2 instance to my local machine. Here's the command:
scp -v -i commoncrawl_practice.pem ec2-user#ec2-54-86-8-91.compute-1.amazonaws.com:/home/ec2-user/testfile .
And here's where it hangs:
debug1: Trying private key: commoncrawl_practice.pem
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
Authenticated to ec2-54-86-8-91.compute-1.amazonaws.com ([54.86.8.91]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions#openssh.com
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending command: scp -v -f /home/ec2-user/testfile
Thanks in advance.
Check if you have a ~/.bashrc file on the destination which prints stuff to your terminal. If yes, disable it for non-interactive shells by inserting
[[ $- == *i* ]] || return
before printing anything.
(solution found on https://unix.stackexchange.com/questions/18231/scp-fails-without-error)
Try SFTP as an alternative to SCP.

Resources