DEV Community

Cover image for Secure Your Coding: Create an SSH Key on Mac or Windows
Lou Creemers
Lou Creemers

Posted on

Secure Your Coding: Create an SSH Key on Mac or Windows

Hi lovely readers,

We all know the feeling: you’re about to push some code, and you have to pick between HTTPS or SSH. Maybe SSH is even your only option. And you, as an amazing developer, want to use the most secure option. Enter SSH keys—a simple and secure way to authenticate without typing your password every time. Whether you’re working on a Mac or a Windows machine using PowerShell, generating an SSH key is easier than you think.

I help students every year with generating their first SSH key. I tend to forget how to do it every single year, even though it's quite simple. So let's go and generate a secure SSH key together.

Why SSH Keys?

SSH keys are a more secure and convenient alternative to using passwords. They are particularly useful when you need to connect to remote servers or repositories regularly. Here are some reasons why SSH keys might become your new best friend:

  • Security: SSH keys use public-key cryptography, which is much more secure than simple passwords.
  • Convenience: Once set up, you don’t need to enter your password every time you connect.
  • Automation: Perfect for scripts and automating tasks that require server access.

Let’s Generate Your SSH Key

Now that you’re excited about SSH keys, let’s dive into generating one using the ED25519 algorithm. The ED25519 algorithm is currently the most secure algorithm out there for SSH keys.

The steps for generating an SSH key are slightly different depending on whether you’re using a Mac or a Windows machine, so I’ll cover both.

For Mac Users:

  1. Open Terminal:

    You can find Terminal in the Applications > Utilities folder, or just use Spotlight to search for it.

  2. Generate the SSH Key:

    Type the following command and press Enter:

    ssh-keygen -t ed25519 -C "your_email@example.com"
    

    Replace "your_email@example.com" with your actual email address. This email is used to label your key. Try to match the email with the email you use for whatever service you want to use this SSH key for.

  3. Choose a location and passphrase:

    You’ll be prompted to save the key in a specific directory. The default is usually fine (/Users/yourusername/.ssh/id_ed25519), so just press Enter.

    Next, it will ask for a passphrase. You can either choose to enter one (for added security) or leave it blank for no passphrase. Press Enter after making your choice.

  4. View Your SSH Key:

    To display the newly created SSH key, use the cat command:

    cat ~/.ssh/id_ed25519.pub
    

    The output is your public SSH key. You’ll typically copy this to your clipboard and paste it where needed, such as in GitHub or your server’s SSH settings.

For Windows Users (Using PowerShell):

  1. Open PowerShell:

    You can do this by searching for "PowerShell" in the Start menu. You can also right-click on the Windows logo and then click on "Terminal".

  2. Generate the SSH Key:

    Enter the following command:

    ssh-keygen -t ed25519 -C "your_email@example.com"
    

    Replace "your_email@example.com" with your actual email address. Try to match the email with the email you use for whatever service you want to use this SSH key for.

  3. Choose a location and passphrase:

    PowerShell will prompt you to save the key to a specific location. The default is typically C:\Users\yourusername\.ssh\id_ed25519, so you can just press Enter.

    It will then ask for a passphrase. As with the Mac, you can choose to enter one or leave it blank.

  4. View Your SSH Key:

    To display your SSH key in PowerShell, use this command:

    cat ~/.ssh/id_ed25519.pub
    

    The key will be displayed in your terminal. Copy it and use it where needed, like adding it to your GitHub account or a remote server.

What's next?

Now that you have your SSH key set up, here are a few things you might want to explore next:

  • Add Your SSH Key to GitHub / GitLab / Azure DevOps: If you're using a developer platform that uses git like GitHub or GitLab, the next step is to add this SSH key to your account. This allows you to push and pull repositories without entering your password.
  • Set Up SSH Key Authentication for a Remote Server: If you regularly connect to remote servers, you can configure your server to accept your SSH key for login, making your workflow more secure and convenient.
  • Learn About SSH Agent: Consider setting up an SSH agent to manage your keys, especially if you have multiple SSH keys. This can save you from having to enter your passphrase every time.
  • Backup Your SSH Keys: It’s always a good idea to back up your SSH keys securely. This ensures that you won’t lose access if you switch machines or need to restore your system.

It's a wrap!

And that’s it! You’ve just generated an SSH key using the ED25519 algorithm on both Mac and Windows. By now, you should feel more confident about using SSH keys for secure, password-less logins. Whether you’re setting up a new repository or connecting to a remote server, your SSH key is ready to help you out.

Do you have any questions or comments? Be sure to leave them here or contact me at @lovelacecoding on most social media platforms. Thanks!

Top comments (0)