Enable seamless connectivity: Using SSH on Windows 11 with Putty, Git Bash and WSL2 — Part 2
Part one of this series covered the utilization of Putty in conjunction with Putty Key Generator for the creation and application of private and public keys in an SSH connection. In the upcoming segment, I will introduce Git Bash. Integrating SSH with Git, particularly on platforms like GitHub, GitLab, Bitbucket, and others, is a common practice. While alternatives such as GitKraken offer a graphical user interface (GUI), Git Bash is a commendable command-line tool. Beyond its capabilities with Git, it also provides essential shell commands like ssh or scp. For tasks involving Git, data exchange with an external server, and connections via SSH, Git Bash stands out as a recommended tool for those who prefer the command line.
Installation
Begin by obtaining the software from gitforwindows and proceed to install it using the standard Windows installation procedure. Throughout the installation, you will be presented with various configuration options.
Following the selection of an installation location, the setup will prompt you for additional components. I strongly recommend including “Git LFS” for robust support of large files. Keep in mind the best practice of storing code in Git rather than data.
Furthermore, during the installation process, consider associating Git-files (.git) and Bash-files (.sh), and enable the option for daily updates. If you find it convenient, integrating Git Bash within the Windows terminal is also a valuable choice.
Proceed to the next step where you decide whether to create a Start Menu folder.
When working with Git, having a text editor is essential for tasks such as composing commit messages or reviewing logs. While the default choice is Vim, known for its power but potentially less intuitive for some users, Git Bash offers several alternatives. In this instance, I opted for Nano.
Following that, you must choose the default branch when initiating a new Git project. Presently, the default is still “master,” but if you prefer terms like “main,” “prd,” “dev,” or any other, this is the opportune moment to make that decision.
Moving on, Git Bash will prompt for a PATH configuration. It’s important to note that this isn’t referring to a specific directory like “C:\Users”; rather, PATH is an environment variable specifying a set of directories where executable programs are stored. When a command is executed in the Command Prompt or PowerShell, the operating system searches for the corresponding executable file in the directories listed in the PATH variable. This enables you to run programs from any location in the command line without specifying the full path to the executable. I opted for the recommended option and also endorse it, particularly if you plan to utilize additional tools later on.
In the subsequent step, you are presented with the choice between the bundled OpenSSH that accompanies Git Bash or opting for an external OpenSSH. Unless you specifically require the latest features, the bundled version is perfectly adequate. I personally selected this option. However, should you decide otherwise, ensure that you independently install OpenSSH.
Now, you are faced with a decision regarding the utilization of the OpenSSL library for HTTPS connections. Unless your administrator mandates the use of the Windows Secure Channel library, opting for the OpenSSL library is entirely appropriate.
The subsequent query pertains to line endings. If you’re not familiar regarding this crucial point have a look here. Given that we are installing Git Bash on Windows, it is recommended to select the option “Checkout Windows-style, commit Unix-style line endings.”
Git Bash also requires a terminal emulator, and you are presented with the choice between “MinTTY” and the limited default console in Windows. Opting for “MinTTY” is a straightforward decision due to its enhanced functionality.
Moving forward, you’ll encounter a decision frequently seen on various Git platforms, such as Bitbucket. In my perspective, the default behavior should consistently be “Fast-forward or merge”. This preference is rooted in the fact that “Rebase” tends to heighten the probability of merge conflicts and requires the use of forced pushes. Conversely, opting for “Only ever fast-forward” is straightforward and easy to understand initially, but as more people contribute to a project, it can become progressively cumbersome.
The Git Credential Manager is a viable option, aiding in the secure storage of Git credentials. To be candid, I am not acquainted with its functionality, but based on my understanding, it may not be necessary, especially considering our utilization of private and public keys later on. Feel free to leave a comment if you hold a differing viewpoint.
In the next option, file system caching is recommended, providing an enhancement in performance. Conversely, utilizing symbolic links in Git repositories is not advised. Taking precautionary measures to prevent their use from the outset could be a prudent decision.
Finally (yes we reached the end of the installation process), there are two experimental options to consider. Integrating Python into Git Bash may not be essential. If you hold a different perspective, exploring Windows Subsystem for Linux (WSL) could be an alternative worth considering. Additionally, the built-in file system monitor is, in my view, unnecessary.
Creating Private and Public Keys
Upon launching Git Bash, the interface presented will resemble the image below. It’s important to note that, in my case, Git Bash is installed in the user’s folder. Mintty serves as the interface to Cygwin, providing a Unix-like environment for Windows. Consequently, commands such as “pwd” to display the current directory and “ls” to show folders and files are functional.
Now, you can copy or, preferably, move the private and public keys you created in the first part into a folder named “.ssh,” which you’ll need to create. However, for the purpose of creating new keys using Git Bash, execute the following command:
ssh-keygen -t rsa -o -b 4096 -C "git key"
Allow me to provide a brief explanation of the command:
ssh-keygen
: Initiates the process of creating a private and corresponding public key.-t rsa
: Specifies the encryption algorithm, with RSA being a widely used and proven choice.-o
: Directs the tool to save the private key in the newer OpenSSH format, as opposed to the older PEM format.-b 4096
: Sets the key length to 4096 bits. The security increases with higher values, but performance may be impacted. A minimum of 2048 is recommended, and 4096 strikes a good balance between security and performance.-C "git key"
: Adds the comment "git key" to help identify this specific key. You can customize the comment to your preference.
Throughout the creation process, the system will prompt you for the directory to store the keys and their names. Opting for the default values is acceptable unless you already have keys with the same names. In such a case, choose different names to prevent overwriting. It’s worth noting that while leaving the passphrase blank is an option, setting one significantly enhances security. However, keep in mind that using a passphrase necessitates its input each time the key is utilized. The recommendation is to set a passphrase for added security.
You can print the public key into your terminal using the command cat ~/.ssh/id_rsa.pub
Copy the displayed key and add it to the “authorized_keys” file on your server, following the steps outlined in part one. This ensures that your public key is authorized for secure connections to the server.
Connect via SSH
Establishing an SSH connection is a straightforward process.
ssh joe@3.71.202.104
To initiate the SSH connection, use the “ssh” command, followed by the username (in this case, “joe”), the “@” symbol, and the IP address or URL. The default port is 22. If an alternative port is required, such as port 6666, the command would be:
ssh -p 6666 joe@3.71.202.104
Conclusion
As demonstrated, the process of installing Git Bash, generating private and public keys, and utilizing them to establish a connection is straightforward. Conclude the setup by adding your public key to a platform such as “GitHub.” If your requirements extend beyond this, involving a full bash environment, the ability to install Linux-specific software, or utilizing Docker, stay tuned for part three, where WSL2 will be presented.