At the moment, Windows 10’s implementation of the OpenSSH client does not have the ssh-copy-id
command available. However, a PowerShell one-line command can mimic the ssh-copy-id
command and allow you to copy an SSH public key generated by the ssh-keygen command to a remote Linux device for passwordless login.
Generate an SSH Key
Note: If you have already generated an SSH keypair that you would like to use, skip this section and proceed to the Copy SSH Key to Remote Linux Device section.
First, open a new PowerShell window (not a Command Prompt window!) and generate a new SSH keypair with the ssh-keygen
command. By default, the public and private keys will be placed in the %USERPROFILE%/.ssh/
directory. The public key file we are interested in is named id_rsa.pub
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
PS C:\Users\Christopher> ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (C:\Users\Christopher/.ssh/id_rsa): Created directory 'C:\Users\Christopher/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in C:\Users\Christopher/.ssh/id_rsa. Your public key has been saved in C:\Users\Christopher/.ssh/id_rsa.pub. The key fingerprint is: SHA256:/mjkrJOQbRzCAwlSPYVBNcuxntm/Ms5/MMC15dCRrMc christopher@Christopher-Win10-VM-01 The key's randomart image is: +---[RSA 2048]----+ |oo.+o== o.o | |. o +. = o = | | o .+. . B | | +..+o o E | | *+.S. . | | o +...o | | o =. .o | | o.*o .. | | .=+++. | +----[SHA256]-----+ PS C:\Users\Christopher> |
Copy SSH Key to Remote Linux Device
Next, we use the below PowerShell one-line command to copy the contents of the id_rsa.pub
public key to a remote Linux device. Replace the {IP-ADDRESS-OR-FQDN}
with the IP address or FQDN (Fully Qualified Domain Name) of the remote Linux device you would like to copy the public key to.
1 |
type $env:USERPROFILE\.ssh\id_rsa.pub | ssh {IP-ADDRESS-OR-FQDN} "cat >> .ssh/authorized_keys" |
An example of this command is shown below. In this example, I am copying the contents of the id_rsa.pub
public key to a remote Linux device at IP address 192.168.30.31.
1 2 3 4 5 6 7 |
PS C:\Users\Christopher> type $env:USERPROFILE\.ssh\id_rsa.pub | ssh 192.168.30.31 "cat >> .ssh/authorized_keys" The authenticity of host '192.168.30.31 (192.168.30.31)' can't be established. ECDSA key fingerprint is SHA256:mTD0/WNCVZ/p/PFSkNDmLJtzIGb5eD7qj6erOQkomjM. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.30.31' (ECDSA) to the list of known hosts. [email protected]'s password: PS C:\Users\Christopher> |
Test Passwordless SSH Connectivity to Remote Linux Device
Finally, verify that you can SSH to the remote Linux device with the ssh
command. An example to a remote Linux device at IP address 192.168.30.31 is shown below. Note how a password did not need to be entered in order for us to establish SSH connectivity to the remote Linux device.
1 2 3 4 |
PS C:\Users\Christopher> ssh 192.168.30.31 Last login: Sat May 23 12:44:51 2020 from 192.168.10.139 [christopher@linux ~]$ who christopher pts/0 2020-05-24 19:35 (192.168.10.113) |
References
The instructions for this blog post were heavily inspired by Scott Hanselman’s blog post on the subject.
This post is licensed under CC BY-NC-SA by the author.
The `ssh-copy-id` command is used to install your SSH public key on a remote server, allowing for passwordless authentication when connecting via SSH.
ssh-copy-id user@remote_host
Understanding SSH Key Authentication
What are SSH Keys?
SSH keys are a pair of cryptographic keys used to authenticate a secure connection between a client and a server. Each pair consists of a public key and a private key. The public key is stored on the server in a special file, while the private key remains with the client. This method enhances security by allowing users to connect to remote servers without needing to transmit passwords over the network.
Benefits of Key-Based Authentication
Using SSH keys provides numerous advantages:
- Enhanced Security: The encryption provided by SSH keys makes unauthorized access considerably more difficult compared to traditional password authentication.
- Convenience: Once set up, key-based authentication allows for seamless logins without entering passwords. This is particularly beneficial for automated scripts and routines.
- Prevention of Brute Force Attacks: Since SSH keys are virtually impossible to guess or crack, they effectively fend off brute force attacks aimed at password-protected logins.
Mastering PowerShell SSH Commands: A Quick Guide
Setting Up PowerShell for SSH
Installing OpenSSH
To utilize the `powershell ssh-copy-id` functionality, you need to ensure that OpenSSH is installed on your Windows system. You can do this through the Settings app:
- Open Settings > Apps > Optional features.
- Look for OpenSSH Client. If it’s not listed, click on Add a feature, search for OpenSSH Client, and install it.
After installation, verify it by executing the following command in PowerShell:
ssh -V
This command will display the version of SSH installed, confirming a successful installation.
Generating SSH Keys
The first step in setting up key-based authentication is to generate SSH keys. You can do this by using the `ssh-keygen` command in PowerShell. Follow these steps:
- Open PowerShell.
- Enter the following command to generate a new SSH key pair:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
This command will generate two files in the default location (`%USERPROFILE%\.ssh\`):
- `id_rsa`: Your private key (keep this secure and do not share it).
- `id_rsa.pub`: Your public key (this is what you’ll send to servers for authentication).
Where to Find SSH Keys
You can locate your SSH keys in the `.ssh` directory within your user profile:
C:\Users\YourUsername\.ssh\
Effortless File Transfers: PowerShell Copy-Item -Exclude
Introduction to `ssh-copy-id`
What is `ssh-copy-id`?
The `ssh-copy-id` command is a convenient utility to install your public key onto a remote server’s authorized keys. By doing so, you can establish a password-less SSH connection to that server.
Limitations of `ssh-copy-id` in PowerShell
While `ssh-copy-id` is a native command in Unix-like systems, PowerShell does not support it directly. However, you can simulate its functionality through a manual approach in PowerShell, which we’ll explore next.
Mastering PowerShell SSH -i for Seamless Connections
Using `ssh-copy-id` in PowerShell
Simulating `ssh-copy-id` Functionality
To implement the functionality of `ssh-copy-id` in PowerShell, you need to manually copy the public key to the remote server. The command below demonstrates this process:
cat ~/.ssh/id_rsa.pub | ssh user@remote_host "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
Explanation of Each Command in the Snippet:
- `cat ~/.ssh/id_rsa.pub`: Reads your public key file.
- `|`: Pipes the output of the previous command into the next one.
- `ssh user@remote_host`: Initiates an SSH session to the specified user and host.
- `»mkdir -p ~/.ssh»`: Creates the `.ssh` directory on the remote server (if it does not exist).
- `cat >> ~/.ssh/authorized_keys`: Appends the public key to the `authorized_keys` file, allowing for key-based authentication.
Verifying Key-Based Authentication
After copying the SSH key, test the connection to ensure that it works without a password prompt. You can do this by executing:
ssh user@remote_host
If everything is set up correctly, you should be logged in without requiring a password. If you encounter issues, check for common mistakes such as incorrect permissions or misconfiguration.
PowerShell Copy-Item: Create Folder Made Easy
Practical Examples
Example Scenario: Copying Keys to a Remote Server
Assuming you have a remote server (`remote_host`) and a user (`user`), use the provided command to transfer your key:
cat ~/.ssh/id_rsa.pub | ssh user@remote_host "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
Once executed, you’ll be prompted for your password for the last time. Upon successful connection, verifying key-based authentication should grant you password-less access.
Example Scenario: Automating Key Copy with PowerShell Scripts
To further streamline the process, you can automate the key copying with a PowerShell script. Below is a sample script:
# Save as Copy-SSHKeys.ps1
param (
[string]$User,
[string]$Host
)
$keyPath = "$HOME\.ssh\id_rsa.pub"
if (Test-Path $keyPath) {
$key = Get-Content $keyPath
ssh $User@$Host "mkdir -p ~/.ssh && echo $key >> ~/.ssh/authorized_keys"
} else {
Write-Error "SSH public key not found!"
}
Explanation of the Script:
- `param`: Accepts parameters for the username and host to which the key is being copied.
- `Test-Path`: Checks if the public key file exists.
- `Get-Content`: Reads the public key from the specified file.
- `ssh $User@$Host`: Executes the SSH command to append the key.
Mastering PowerShell Split: A Quick Guide to Strings
Troubleshooting Common Issues
Common Errors with SSH Key Authentication
While setting up SSH key authentication, you may encounter various errors, such as:
- Permission denied errors: Often occurs due to incorrect permissions on the `.ssh` directory or the `authorized_keys` file.
- SSH key not recognized: This can happen if the public key isn’t properly added or has incorrect formatting.
Fixing Permissions of `.ssh` Directory
To ensure proper permissions, you can set the correct permissions directly on the remote server using:
ssh user@remote_host "chmod 700 ~/.ssh; chmod 600 ~/.ssh/authorized_keys"
This command makes the `.ssh` directory accessible only to the user and secures the `authorized_keys` file.
Mastering PowerShell Select-Object in a Nutshell
Tips and Best Practices
Protecting Your SSH Keys
Managing your SSH keys securely is crucial. Consider implementing the following practices:
- Use a Passphrase: Protect your private key with a strong passphrase to add an extra security layer.
- Backup Your Keys: Always keep a secure backup of your SSH keys in a safe location.
Rotating SSH Keys Regularly
Regularly updating and rotating your SSH keys is essential to maintaining a robust security posture. Implement this practice as part of your routine system maintenance.
Using SSH Config File for Convenience
You can simplify your SSH connection methods by using an SSH config file (`~/.ssh/config`). Here’s an example entry:
Host myserver
HostName remote_host
User user
IdentityFile ~/.ssh/id_rsa
This configuration allows you to connect using a shorthand command (`ssh myserver`), improving efficiency.
Mastering PowerShell Get-Credential: A Quick Guide
Conclusion
In this article, we explored how to implement `powershell ssh-copy-id` functionality. By leveraging the PowerShell commands and understanding SSH key authentication mechanisms, you can enhance your network operations and secure your connections effectively. Start applying these principles today for a more secure SSH experience!
PowerShell Shutdown: Quick Commands for Instant Reboot
Resources
For further reading, consider checking the official PowerShell and OpenSSH documentation. Engaging with community forums can also provide additional support and insights as you deepen your understanding and skills in using PowerShell and SSH.
<#
.SYNOPSIS
Copy ssh public key to remote account
.DESCRIPTION
Copies public key to target machine
Default is .ssh\id_rsa.pub
See copyright notice at end of script
If this script won’t run, then run this command once
Set-ExecutionPolicy RemoteSigned CurrentUser
refer to https://go.microsoft.com/fwlink/?LinkID=135170
#>
[CmdletBinding()]
param(
[Parameter(Position = 0, Mandatory = $True)]
[string]$Target <# SSH compatible Name of Target machine #>,
[Parameter( Position = 1, Mandatory = $False)]
[string]$IdentityFile = «id_rsa« <#Identity File, must be in .ssh directory #>,
[switch]$Save = $False <# Save script used #>
)
$IdentityPath = Join-Path (Join-Path $env:USERPROFILE .ssh) $IdentityFile
if (-not (Test-Path —Path $IdentityPath —PathType Leaf)) {
Write-Host Key file $IdentityFile does not exist, use ssh—keygen
exit(1)
}
$PUBLIC_KEY = get-content «${IdentityPath}.pub«
$SCRIPT = @»
#!/bin/bash
#
PUBLIC_KEY=»$PUBLIC_KEY«
umask 0077
[ -d ~/.ssh ]||mkdir -p ~/.ssh
[ -f ~/.ssh/authorized_keys ]|| touch ~/.ssh/authorized_keys
grep —quiet —fixed-strings «`$PUBLIC_KEY» ~/.ssh/authorized_keys
if [ `$? -ne 0 ]
then
echo «`$PUBLIC_KEY» >> ~/.ssh/authorized_keys
else
echo Key already exists, no update performed.
fi
«@
if ($Save) {
Set-Content .\ssh—copy-id.sh —Value $SCRIPT
}
# The sed step is to convert crlf to lf for bash — it’s fussy
$SCRIPT | ssh $Target —o StrictHostKeyChecking=no «sed ‘s/\r//’ | bash«
if ($?) {
Write-Output «You should be able to login to $Target without a password using $IdentityFile key«
}
else {
Write-Host «Could not connect to $Target — Terminating« —ForegroundColor Red
}
<#
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED «AS IS», WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>
#>
21 Feb 2023 |
Create ssh-copy-id script for Windows |
Those of us who use linux with keyless logins and ssh , usually find the utility ssh-copy-id as a convenient way to copy the ssh ke into the authorized keys files of the destination server. This works great on Linux systems, but windows (and also Mac) don’t have a native version fo ssh-copy-id, of course you could find scripts online that do this.. but many are overkill basically re-creating the entire ssh client in code just for the purpose of copying over a single key. Also since this is a securtity realted operation , you want to be confident those scripts are only doing what they say they’re doing.
With that said I hope to address these issues in this post and show you a quick, short and most importantly easy to modify powershell script that you can use in any modern version of Windows.
Security and Concerns
Finally there’s security concerns about using these scripts, unless you go over everly line of of code you’re never really sure what they’re doing with your key. Because of this the code velow is short, in native powershell and very easy to use, you litterally can read the code in this post and res-assured it’s only doing what it’s supposed to.
SSH-COPY-ID in Powershell
Windows Powershell is the defacto Widnows command line programming language replacing the traditional DOS batch files, and has been in available since around Windows 7 (circa 2007) . You can find a useful Powershell cheat sheet here:
param ( [Parameter(Mandatory=$true)] [string]$connectionString, [Parameter(Mandatory=$true)] [string]$publicKeyFile ) $connectionParts = $connectionString.Split('@') if ($connectionParts.Length -ne 2) { throw "Invalid connection string format. Must be in the format 'user@hostname:port'" } $username = $connectionParts[0] $hostParts = $connectionParts[1].Split(':') $hostname = $hostParts[0] if ($hostParts.Length -gt 1) { $port = $hostParts[1] } else { $port = 22 } $sshDir = "$env:USERPROFILE\.ssh" if (!(Test-Path -Path $sshDir)) { New-Item -ItemType Directory -Path $sshDir | Out-Null } $sshPubKey = Get-Content $publicKeyFile $sshPubKeyFileName = Split-Path $publicKeyFile -Leaf $sshAuthorizedKeysFile = "$sshDir\authorized_keys" $sshConnection = New-SSHSession -ComputerName $hostname -Credential $username -Port $port $sshAuthorizedKeys = Invoke-SSHCommand -SessionId $sshConnection.SessionId -Command "cat $sshAuthorizedKeysFile" | Select-Object -ExpandProperty Output if ($sshAuthorizedKeys -notcontains $sshPubKey) { Invoke-SSHCommand -SessionId $sshConnection.SessionId -Command "echo `"$sshPubKey`" >> $sshAuthorizedKeysFile" } Write-Output "Public key added to $hostname authorized_keys file" ?
This script requires the New-SSHSession
and Invoke-SSHCommand
cmdlets, which are available in the PowershellSSH
module. You can install this module by running Install-Module -Name PowershellSSH
.
How it works
The script takes three parameters: the username on the remote host, the hostname or IP address of the remote host, and the path to the public key file. The script then creates the ~/.ssh
directory on the remote host if it doesn’t exist, reads the public key from the specified file, and adds it to the authorized_keys
file on the remote host if it’s not already there. Finally, the script outputs a message indicating that the public key was added to the authorized_keys
file.
To use this script, save it to a file with a .ps1
extension, and then run it with the required parameters. For example:
.\ssh-copy-id.ps1 user@remotehost:port -publicKeyFile C:\Users\myuser\.ssh\id_rsa.pub
- PowerShell
- Ssh-copy-id for copying SSH keys to servers
- Ssh-copy-id on windows doesn’t work: No such file or directory
- Is there an equivalent to ssh-copy-id for Windows?
- Windows powershell ssh-copy-id code example
- Is there an equivalent to ssh-copy-id for Windows?
- Copy SSH Key In Windows 10
PowerShell
PowerShell is a task automation and configuration management framework from
Microsoft, consisting of a command-line shell and associated scripting
language. Initially a Windows component only, known as Windows PowerShell, it
was made open-source and cross-platform on 18 August 2…
function name ([Type]$Param1, [Type]$Param2)
{
# Instructions
}
function Verb-Noun
{
param (
# Definition of static parameters
)
dynamicparam {
# Definition of dynamic parameters
}
begin {
# Set of instruction to run at the start of the pipeline
}
process {
# Main instruction sets, ran for each item in the pipeline
}
end {
# Set of instruction to run at the end of the pipeline
}
}
name value1 value2
Verb-Noun -Param1 value1 -Param2 value2
[Console]::WriteLine("PowerShell")
Ssh-copy-id for copying SSH keys to servers
Setting up public key authentication. Key based authentication in SSH is
called public key authentication.The purpose of ssh-copy-id is to make setting
up public key authentication easier. The process is as follows. Generate an
SSH Key. With OpenSSH, an SSH key is created using ssh-keygen.In the simplest
form, just run ssh-keygen and answer the questions. . The …
# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/ylo/.ssh/id_rsa): mykey Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in mykey. Your public key has been saved in mykey.pub. The key fingerprint is: SHA256:GKW7yzA1J1qkr1Cr9MhUwAbHbF2NrIPEgZXeOUOz3Us [email protected] The key's randomart image is: +---[RSA 2048]----+ |.*++ o.o. | |.+B + oo. | | +++ *+. | | .o.Oo.+E | | ++B.S. | | o * =. | | + = o | | + = = . | | + o o | +----[SHA256]-----+ #
ssh-copy-id -i ~/.ssh/mykey [email protected]
ssh -i ~/.ssh/mykey [email protected]
ssh-copy-id [-f] [-n] [-i identity file] [-p port] [-o ssh_option] [[email protected]]hostname
brew install ssh-copy-id
sudo port install openssh +ssh-copy-id
curl -L https://raw.githubusercontent.com/beautifulcode/ssh-copy-id-for-OSX/master/install.sh | sh
Ssh-copy-id on windows doesn’t work: No such file or directory
Git’s ssh is a version of OpenSSH.You can confirm it by running ssh -V under
path\to\git\usr\bin.Here is what it evals on my machine: OpenSSH_7.7p1,
OpenSSL 1.0.2o 27 Mar 2018 ssh-copy-id script internally executes some *nix
shell command (like exec, cat, etc.You can find more by opening the one under
path\to\git\usr\bin in text mode), so it works only against …
ssh-copy-id -i C:/Users/username/.ssh/mykey.pub [email protected]
ssh-copy-id -i ~/.ssh/mykey.pub [email protected]
OpenSSH_7.7p1, OpenSSL 1.0.2o 27 Mar 2018
Get-Content $env:USERPROFILE\.ssh\id_rsa.pub | ssh <user>@<hostname> "cat >> .ssh/authorized_keys"
Is there an equivalent to ssh-copy-id for Windows?
Login to the server with an SSH client, like PuTTY. On the server type: ssh-
copy-id -i mykey.pub [email protected] On Windows ssh-copy-id script comes
with Git for Windows. So you may use that locally, if you have Git for
Windows. If you do not want to …
GET_ID="cat ${ID_FILE}"
{ eval "$GET_ID" ; } | ssh ${1%:} "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys" || exit 1
type public_id | plink.exe [email protected] "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys"
c:\>ssh-keygen
c:\>ssh [email protected] "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys || exit 1" < \\path_to_where_the_file_was_generated_from_ssh_key_gen\id_rsa.pub
# this should work but it didn't work for me
type file | ssh [email protected] "cat >> /tmp/t.txt"
vi ~/.ssh/authorized_keys
---- BEGIN SSH2 PUBLIC KEY ----
Comment: "2048-bit RSA, [email protected]"
AAAAB3NzaC1yc2EAAAABIwAAAQEAnvYlVooXGoj3+7huZBUqf4wj57r25SHCKiiShyla33
5flX7Rsmb4meExpdh2NzfzffG15xl1wo0xBZ3HdZdqF2GUniEcNbtVjS1FKzQwPfsYPHMC
Y58qT0U2ZgK1zsXj2o0D2RWrCv3DFFfwUgNyZRYN2HK32umY6OmGSOVuJvIKhT+X6YaCVy
ax3CHv2ByB2OTBl9mh4nrwYAVXToT+X2psBE+MKB5R85lrUGkl3GtymTk10Dvf5O80exdT
LFRMvkCA5RAIZgvxMk/bbNaH/0UHQoctX9oaDeKGWUPfVaknFBQdU9009+lK/ocAlKVNHE
Qkw+1wuV6dFoT1/hngSw==
---- END SSH2 PUBLIC KEY ----
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAnvYlVooXGoj3+7huZBUqf4wj57r25SHCKiiShyla335flX7Rsmb4meExpdh2NzfzffG15xl1wo0xBZ3HdZdqF2GUniEcNbtVjS1FKzQwPfsYPHMCY58qT0U2ZgK1zsXj2o0D2RWrCv3DFFfwUgNyZRYN2HK32umY6OmGSOVuJvIKhT+X6YaCVyax3CHv2ByB2OTBl9mh4nrwYAVXToT+X2psBE+MKB5R85lrUGkl3GtymTk10Dvf5O80exdTLFRMvkCA5RAIZgvxMk/bbNaH/0UHQoctX9oaDeKGWUPfVaknFBQdU9009+lK/ocAlKVNHEQkw+1wuV6dFoT1/hngSw== [email protected]
c:\>ssh [email protected] "ls -al /tmp/"
ssh-copy-id -i mykey.pub [email protected]
1. added c:\cygwin\bin to the environment's Path variable
2. starting cmd.exe (windows commandline)
3. > ssh-keygen -t rsa (just pressing enter till done)
4. > cat ~/.ssh/id_rsa.pub | ssh [email protected] "umask 077; test -d ~/.ssh || mkdir ~/.ssh ; cat >> ~/.ssh/authorized_keys"
5. ..enter server password
6. > ssh [email protected] (testing, not beeing asked for password)
C:\Users\user>ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/user//.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/user//.ssh/id_rsa.
Your public key has been saved in /c/Users/user//.ssh/id_rsa.pub.
The key fingerprint is:
20:16:9b:0d:00:92:c4:34:99:51:20:b7:ef:50:c4:0f [email protected]
C:\Users\user>ssh [email protected] "umask 077; test -d .ssh || mkdir .ssh ; cat >> .s
sh/authorized_keys || exit 1" < "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys
|| exit 1" < C:\Users\user\.ssh\id_rsa.pub
The authenticity of host 'remote (xx.xx.xxx.xx)' can't be established.
RSA key fingerprint is 99:99:73:74:fe:14:bc:91:c8:3b:ac:f4:95:99:4d:06.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'remote,xx.xx.xxx.xx' (RSA) to the list of known hosts.
*************************************************************************
This computer system is the property of Sample Corporation and is to
be used for business purposes. All information, messages, software and
hardware created, stored, accessed, received, or used by you through
this system is considered to be the sole property of Sample Corporation
and can and may be monitored, reviewed, and retained at any time. You
should have no expectation that any such information, messages or
material will be private. By accessing and using this computer, you
acknowledge and consent to such monitoring and information retrieval.
By accessing and using this computer, you also agree to comply with
all of Sample Company's policies and standards.
*************************************************************************
[email protected]'s password:[Enter Password for first time]
C:\Users\user>ssh [email protected]
*************************************************************************
This computer system is the property of Sample Corporation and is to
be used for business purposes. All information, messages, software and
hardware created, stored, accessed, received, or used by you through
this system is considered to be the sole property of Sample Corporation
and can and may be monitored, reviewed, and retained at any time. You
should have no expectation that any such information, messages or
material will be private. By accessing and using this computer, you
acknowledge and consent to such monitoring and information retrieval.
By accessing and using this computer, you also agree to comply with
all of Sample Company's policies and standards.
*************************************************************************
Last login: Wed Oct 14 14:37:13 2015 from localhost
function ssh-copy-id([string]$userAtMachine){
$publicKey = "$ENV:USERPROFILE" + "/.ssh/id_rsa.pub"
if (!(Test-Path "$publicKey")){
Write-Error "ERROR: failed to open ID file '$publicKey': No such file"
}
else {
& cat "$publicKey" | ssh $userAtMachine "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys || exit 1"
}
}
ssh-copy-id [email protected]
Windows powershell ssh-copy-id code example
Pandas how to find column contains a certain value Recommended way to install
multiple Python versions on Ubuntu 20.04 Build super fast web scraper with
Python x100 than BeautifulSoup How to convert a SQL query result to a Pandas
DataFrame in Python How to write a Pandas DataFrame to a .csv file in Python
type $env:USERPROFILE\.ssh\id_rsa.pub | ssh {IP-ADDRESS-OR-FQDN} "cat >> .ssh/authorized_keys"
Is there an equivalent to ssh-copy-id for Windows?
Solution 3: ssh-copy-id does a couple of things (read the man page for
details), but the most important thing it does is append the contents of your
local public key file to a remote file called authorized_keys.. You could do
this yourself by opening the key file with a text editor and pasting the
contents in the Kitty terminal.
GET_ID="cat ${ID_FILE}"
{ eval "$GET_ID" ; } | ssh ${1%:} "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys" || exit 1
type public_id | plink.exe [email protected] "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys"
c:\>ssh-keygen
c:\>ssh [email protected] "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys || exit 1" < \\path_to_where_the_file_was_generated_from_ssh_key_gen\id_rsa.pub
# this should work but it didn't work for me
type file | ssh [email protected] "cat >> /tmp/t.txt"
vi ~/.ssh/authorized_keys
---- BEGIN SSH2 PUBLIC KEY ----
Comment: "2048-bit RSA, [email protected]"
AAAAB3NzaC1yc2EAAAABIwAAAQEAnvYlVooXGoj3+7huZBUqf4wj57r25SHCKiiShyla33
5flX7Rsmb4meExpdh2NzfzffG15xl1wo0xBZ3HdZdqF2GUniEcNbtVjS1FKzQwPfsYPHMC
Y58qT0U2ZgK1zsXj2o0D2RWrCv3DFFfwUgNyZRYN2HK32umY6OmGSOVuJvIKhT+X6YaCVy
ax3CHv2ByB2OTBl9mh4nrwYAVXToT+X2psBE+MKB5R85lrUGkl3GtymTk10Dvf5O80exdT
LFRMvkCA5RAIZgvxMk/bbNaH/0UHQoctX9oaDeKGWUPfVaknFBQdU9009+lK/ocAlKVNHE
Qkw+1wuV6dFoT1/hngSw==
---- END SSH2 PUBLIC KEY ----
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAnvYlVooXGoj3+7huZBUqf4wj57r25SHCKiiShyla335flX7Rsmb4meExpdh2NzfzffG15xl1wo0xBZ3HdZdqF2GUniEcNbtVjS1FKzQwPfsYPHMCY58qT0U2ZgK1zsXj2o0D2RWrCv3DFFfwUgNyZRYN2HK32umY6OmGSOVuJvIKhT+X6YaCVyax3CHv2ByB2OTBl9mh4nrwYAVXToT+X2psBE+MKB5R85lrUGkl3GtymTk10Dvf5O80exdTLFRMvkCA5RAIZgvxMk/bbNaH/0UHQoctX9oaDeKGWUPfVaknFBQdU9009+lK/ocAlKVNHEQkw+1wuV6dFoT1/hngSw== [email protected]
c:\>ssh [email protected] "ls -al /tmp/"
Copy SSH Key In Windows 10
Did you know that Windows 10 has a proper SSH client, and has had one for a
while? You can generate your keys using ssh-keygen. The problem, as you will
soon discover, is there is no ssh-copy-id command. The good news is that you
can fix this problem in your Powershell profile. Open your profile using the
following command.
notepad++ $profile
function ssh-copy-id([string]$userAtMachine, [string]$port = 22)
{
# Get the generated public key
$key = "$ENV:USERPROFILE" + "/.ssh/id_rsa.pub"
# Verify that it exists
if (!(Test-Path "$key")) {
# Alert user
Write-Error "ERROR: '$key' does not exist!"
}
else {
# Copy the public key across
& cat "$key" | ssh $userAtMachine -p $port "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys || exit 1"
}
}
ssh-copy-id [email protected]
ssh-copy-id [email protected] 222