Skip to content

Instantly share code, notes, and snippets.

@411A
Created April 8, 2024 18:49
Show Gist options
  • Save 411A/a4de5631cd6f49da77b769971a459ab7 to your computer and use it in GitHub Desktop.
Save 411A/a4de5631cd6f49da77b769971a459ab7 to your computer and use it in GitHub Desktop.

Creates the related GitHub's SSH configuration on drive C.

  1. Create a new directory to store your credentials inside the C drive (/), then create a hidden .ssh directory inside it:
# Create directories
mkdir "/MyGitHub"
cd "/MyGitHub"
mkdir ".ssh"
# Create a new config file
New-Item "/MyGitHub\.ssh\config" -ItemType "file"
  1. Create SSH Keys using ssh-keygen:
ssh-keygen -t ed25519 -C YourGitHubEmail@Domain.com
  1. Enter the absolute path (full path) where you want to store SSH Keys inside the .ssh folder:
C:\MyGitHub\.ssh\id_ed25519
  1. Choose a passphrase, don't forget it! You may need to enter it on every pull/push/commit.
  2. Add SSH Keys to ssh-agent:
Set-Service ssh-agent -StartupType Automatic
Start-Service ssh-agent
ssh-add /MyGitHub\.ssh\id_ed25519
  1. Open Public Key & copy its content:
Get-Content /MyGitHub\.ssh\id_ed25519.pub
  1. Open https://github.com/settings/keys & then click on New SSH key, enter any title, Key Type: Authentication Key, then paste the content you copied from previous step (Public Key) inside Key field & click on Add SSH key.
  2. Make the .ssh folder more secure:
# Set hidden attribute for the .ssh folder
Set-ItemProperty -Path "/MyGitHub\.ssh" -Name "Attributes" -Value ([System.IO.FileAttributes]::Hidden)

# Remove existing permissions
$folderPath = "/MyGitHub\.ssh"
$acl = Get-Acl $folderPath
$acl.Access | ForEach-Object { $acl.RemoveAccessRule($_) }

# Add permissions for yourself
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$env:USERNAME", "FullControl", "Allow")
$acl.SetOwner([System.Security.Principal.NTAccount]"$env:USERNAME")
$acl.SetAccessRule($rule)

# Save the modified ACL
Set-Acl $folderPath $acl
  1. Change default SSH path to your preferred path:
@'
Host github.com
    IdentityFile /MyGitHub\.ssh\id_ed25519
'@ | Out-File -FilePath "/MyGitHub\.ssh\config" -Encoding utf8
  1. Test GitHub connection:
ssh -i C:\MyGitHub\.ssh\id_ed25519 -T git@github.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment