How to generate and copy a SSH key in one line on macOS? - shell

I use this command to generate a key:
ssh-keygen -t rsa -b 4096 -C "your_email#example.com"`
However I do not want to save it as a file, instead I would like to copy it to my clipboard so it is ready to be pasted.
How can I do that? How to combine this with some sort of copy to clipboard command?
I tried the following command but it didn't work:
pbcopy ssh-keygen -t rsa -b 4096 -C "your_email#example.com"ssh-keygen -t rsa -b 4096 -C "your_email#example.com"

You should create a script (or a function) to achieve this. Example with a script:
genkey.sh
#!/bin/bash
ssh-keygen -t rsa -b 4096 -C "your_email#example.com" -f $1 && pbcopy < $1.pub
The first command generates a key at the location given in the first argument of the script. The second one, pbcopy, copies the content of the newly-generated public key in your clipboard.
When running the script, feed it the path to the private key you want to generate:
sh genkey.sh ~/.ssh/id_rsa

Related

sshpass want to use parameter of sftp

Hi i created following script to initialize my storage box to use rsync without password later. Last year it works if i remember correct...
cat .ssh/id_rsa.pub >> .ssh/storagebox_authorized_keys
echo -e "mkdir .ssh \n chmod 700 .ssh \n put $.ssh/storagebox_authorized_keys" \
".ssh/authorized_keys \n chmod 600 .ssh/authorized_keys" | sshpass -p ${storage_password} \
sftp -P ${storage_port} -i .ssh/id_rsa ${storage_user}#${storage_address}
today I get following error:
sshpass: invalid option -- 'i'
but the parameter -i belongs to sftp and not sshpass - is there an possibility to parse the parameters in the correct way?
edit: i switched the position of
-i .ssh/id_rsa ${storage_user}#${storage_address}
and get this error
sshpass: Failed to run command: No such file or directory
edit: it seems like an sftp problem...
after discussion, updating answer to properly support automation
step 1:
create an sftp "batch file" e.g: ~/.ssh/storage-box_setup.sftp
mkdir .ssh
chmod 700 .ssh
put /path/to/authorized_keys_file ".ssh/authorized_keys
chmod 600 .ssh/authorized_keys
/path/to/authorized_keys_file is a file containing public keys of ONLY the keys that should have access to your storage box (.ssh/storagebox_authorized_keys)
step 2:
update automation script command to
sshpass -p <password> -- sftp -P <port> -b ~/.ssh/storage-box_setup.sftp user#host
the -b flag was the answer you needed.
refer: man sftp
-b batchfile
Batch mode reads a series of commands from an input batchfile instead of stdin. Since it lacks user interaction it should be used in conjunction with non-interactive authentication.
--
sshpass -p ${storage_password} -- \
sftp -P ${storage_port} -i .ssh/id_rsa \
${storage_user}#${storage_address}
the -- before sftp is a way to tell sshpass (and most other programs) to stop parsing arguments.
everything after -- is assumed as the last argument, which in the case of sshpass is the command to be executed ssh -i ~/.id_rsa ...
in case you're wondering switching the position of -i tells sshpass to execute -i as a program and hence fails with command not found

Why -f flag of ssh-keygen fails to follow symlink?

My ~/.ssh directory is a symlink to /somewhere/else/.ssh/
Now, the following works perfectly; and the demo key ends up getting created at /somewhere/else/.ssh/Official, as expected.
export NewUser="Sam"
ssh-keygen -N "" -t rsa -b 8192 -C "Login Key of ${NewUser}." -f ~/.ssh/Official/demo
However, when -f is supplied with the same path but via a variable, it fails with the following error:
Saving key "~/.ssh/Official/demo" failed: No such file or directory
export NewUser="Darsh"
export SSHKey_Path="~/.ssh/Official/demo"
ssh-keygen -N "" -t rsa -b 8192 -C "Login Key of ${NewUser}." -f ${SSHKey_Path}
I have tried several ways to supply this variable, but nothing worked. I'm not able to find anything about variables in the documentation either. I wish to know why does -f fail to follow the symlink path ONLY if passed via a variable? Is there a workaround? I'm not sure but, would it be recommended to bring this to notice here?
EDIT: Updating question after debugging. The original question has been preserved below:
I am aware that ssh-keygen has a flag -f to specify input_keyfile - With which, once can create a key with custom name at a custom location. However, this fails if the input_keyfile is a variable.
How do I provide the key path as a variable to ssh-keygen?
Following are oversimplified snippets from the bigger code:
This works perfectly:
export NewUser="Sam"
ssh-keygen -N "" -t rsa -b 8192 -C "Login Key of ${NewUser}." -f ~/.ssh/Official/demo
However, this fails with the following error Saving key "~/.ssh/Official/demo" failed: No such file or directory
export NewUser="Darsh"
export SSHKey_Path="~/.ssh/Official/demo"
ssh-keygen -N "" -t rsa -b 8192 -C "Login Key of ${NewUser}." -f ${SSHKey_Path}
I have tried wrapping ${SSHKey_Path} in several ways, but nothing worked:
", ', $(echo ${SSHKey_Path}), and many more.
The failure is not in the variable, but in the interpretation of ~.
Try
export SSHKey_Path=~/.ssh/Official/demo
or
export SSHKey_Path="$HOME/.ssh/Official/demo"

Automatically no-overwrite in ssh-keygen

If I run
ssh-keygen -t rsa -N "" -f id_rsa
and the file id_rsa already exists, it will prompt me whether I want to overwrite:
id_rsa already exists.
Overwrite (y/n)?
How do I automatically answer no in a bash script? I checked man ssh-keygen but couldn't find any such option.
you could simply do like this:
echo "n"|ssh-keygen -t rsa -N "" -f id_rsa
You can use here-string to automatically answer n to the Overwrite (y/n)? question.
ssh-keygen -t rsa -N "" -f id_rsa <<< n

Ansible does not ask user for prompts

I'm trying to get an ansible script to prompt the user. I have the following script:
- name: Generate a new SSH Key
shell: ssh-keygen -t rsa -b 4096 -C {{email_address}}
When I run from command line, it ask for a passphrase and file location. When I run from the ansible, it uses the default values. How do I get it to pause, so that it asks the user.
You have a few options:
Write an Expect script, copy it to the destination machine and execute it
or
Use Ansible's Expect module
- expect:
command: ssh-keygen -t rsa -b 4096 -C {{email_address}}
responses:
'Enter file in which .*': '/home/gibson/.ssh/id_rsa'
or
Specify non-interactive ssl-keygen
- shell: ssh-keygen -f myfile.rsa -t rsa -b 4096 -C {{email_address}} -N ''
or
Use Ansible's vars_prompt to prompt for the value and pass it to the command
vars_prompt:
- name: "key_file_name"
prompt: "Enter file in which to save the key: "
...
...
tasks:
- shell: ssh-keygen -f {{key_file_name}} -t rsa -b 4096 -C {{email_address}} -N ''

Automating "enter" keypresses for bash script generating ssh keys

I would like to create script, which simply runs ssh-keygen -t rsa. But how to pass to it 3 times enter?
Try:
ssh-keygen -t rsa -N "" -f my.key
-N "" tells it to use an empty passphrase (the same as two of the enters in an interactive script)
-f my.key tells it to store the key into my.key (change as you see fit).
The whole thing runs without you needing to supply any enter keys :)
To send enters to an interactive script:
echo -e "\n\n\n" | ssh-keygen -t rsa
a version with passphrase is:
$ ssh-keygen -t rsa -b 4096 -C "comment" -P "examplePassphrase" -f "desired pathAndName" -q
the -q is for silent
Source is http://linux.die.net/man/1/ssh-keygen
Agree with Michel Marro except that it needs some more:
If the file already exists, it will still be interactive asking if it has to overwrite it.
Use the answer of this question.
yes y | ssh-keygen -q -t rsa -N '' >/dev/null
The redirection to null is necessary to silence the overwrite message.
It is recommended to use ed25519 for security and performance.
yes "y" | ssh-keygen -o -a 100 -t ed25519 -C "Bla Bla" -f /mypath/bla -N ""
here
-o OpenSSH key format instead of older PEM (needs OpenSSH 6.5+)
-a Number of primality test while screening DH-GEX candidates
-t Type of key (ed25519, RSA, DSA etc.)
-f /mypath/bla The output file path and name
-N "" Use empty passphase
and yes "y" for no interaction.
It will generate two files
/mypath/bla
/mypath/bla.pub
where the bla file is private and bla.pub is public.
echo -e "\n"|ssh-keygen -t rsa -N ""

Resources