Cygwin (Bash for Windows)
General
Cygwin is a POSIX-compatible environment that runs natively on Microsoft Windows. Its goal is to allow programs of Unix-like systems to be recompiled and run natively on Windows with minimal source code modifications by providing them with the same underlying POSIX API they would expect in those systems.
The Cygwin installation directory behaves like the root and follows a similar directory layout to that found in Unix-like systems, with familiar directories like /bin, /home, /etc, /usr, /var available within it, and includes by default hundreds of programs and command-line tools commonly found in the Unix world, plus the terminal emulator Mintty which is the default command-line interface tool provided to interact with the environment.
Cygwin provides native integration of Windows-based applications, data, and other system resources with applications, software tools, and data of the Unix-like environment. Thus, it is possible to launch Windows applications from the Cygwin environment, as well as to use Cygwin tools and applications within the Windows operating context.
Installing from Repo Script
Easy and fastest install or update using a PowerShell script from a repository.
The only question asked is to update of install.
GitLab Repository
powershell -Command "Invoke-Expression(Invoke-WebRequest -Uri 'https://git.scanframe.com/shared/bin-bash/-/raw/master/install-cygwin.ps1' -UseBasicParsing).Content"
GitHub Repository
Clone from the GitLab repository.
powershell -Command "Invoke-Expression(Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/Scanframe/sf-cygwin-bin/master/install-cygwin.ps1' -UseBasicParsing).Content"
Installing from Scratch
WinGet
From the command-line.
In case of a hash error.
Run in an elevated shell the next command.
Then rerun the command but now with option --ignore-security-hash added.
Downloading
Goto the Cygwin website and download the appropriate 32 or 64 bit version of Cygwin.
Run the Installer and install it in the default location for "all users". Use a nearby proxy for downloading the packages.
Select the following needed packages: * wget * openssh * git
Missing a package for a command look it up here.
Configuration
Set the cygwin users home directory to the Windows users directory
Edit the file /etc/nsswitch.conf or from Windows using C:\<cygwin-dir>\etc\nsswitch.conf and set the line with db_home as shown.
# passwd: files db
# group: files db
# db_enum: cache builtin
db_home: /cygdrive/c/Users/%u/cygwin
# db_shell: /bin/bash
# db_gecos: <empty>
Clone the Git bin shared scripts repository
Clone the bin dirtectory using this on the cygwin command-line.
Incase of an error you might have to git config --global http.sslVerify false.
Copy the files .bash_profile and .bashrc from cloned repo in ~/bin.
This to automatically runs ssh-agent killing an existing one and also enables bash completion.
The ssh-agent will load the ssh-keys from your ~/.ssh directory.
Install additional packages from the command-line
After a cygwin restart all script from the script repository are ineffective and packages are now easily install using the apt-cyg script.
To find a certain command/application for cygwin use the package finder here
So installing the killall command you need to install the psmisc package like this:
Now install the 'needed' multiple packages to easier use :
Type apt-cyg for additional options when needed.
VcXsrv (X-server for Windows)
Installing
Download
Goto the sourceforge website and download the installer.
Run the installer with all defaults.
Start the X-server using the command xserver-start.sh from the cygwin shell.
In the Windows system tray an X-icon mst be visible.
Additional Terminal Window
Then start it using the script called terminal.
Personal SSH Key
Create a key
If you do not have a personal rsa ssh-key create one using the ssh-keygen application from openssh.
The option -C specifies the comment which is added to the key and is readable from a server which then can use it to act on it in shell scripts.
Always use add a sensible pass phrase to encrypt your key since this key allows others to access your privates and nobody else touches your privates! :)
The key is saved in the ~/.ssh subdirectory using 2 files:
* id_rsa (private with pass frase encrypted key)
* id_rsa.pub (public key for on remote systems)
Now the key is generated call ssh-add to add it to the already runing ssh-agent which was started in the .bashrc of cygwin.
Also when cygwin is restarted you're asked to enter your pass phrase because the ssh-agent automatically loads the created ssh-key.
Check if the key is loaded using ssh-add -L.
Access Remote
Drop your public key on a remote system to access itr without passing a password.
When having access to a system with username and password then you can add the key to the file ~/.ssh/authorized_keys like this.
# Open ssh session.
ssh <username>@<remote-name-or-ip>
# Add the line with the public key to the list of keys.
ssh-add -L >> .ssh/authorized_keys
Make sure the file have mode 0600 by calling chmod 0600 ~/*.
Much shorter is:
This is only possible when you can login to the system using username and password. Some systems have ssh access using usernames blocked and only allow ssh-keys for authentication.
Notes
Forwarding
When ssh-agent forwarding is enabled accessing another remote system from a remote system with the same key is possible.
This feature is really helpful when accessing containers with no public IP.
Add the following content to the file ~/.ssh/config.
Port Tunneling
Ssh is also used for tunneling tcp-ip ports.
For example a mysql at port 3306 is only accessible from localhost on a remote system for security reasons. To access it you can tunnel is locally using the command:
# Tunnel local port localhost:3307 to remote port localhost:3306 and run in the background.
ssh -f -N -L 0.0.0.0:3307:localhost:3306 <username>@<remote-host>
Now a mysql client can open a connection on the remote using localhost:3307.
Using sshfs
Installing
In order to beable yo use sshfs the Cygwin package sshfs needs to be installed.
The cygwin package is dependent on a WinFPS package and is downloaded from this site.
Mounting
Mounting the a drive letter using Windows assigned uid and gid.
sshfs <user>@<host>:<path> <drive-letter>: \
-o reconnect,follow_symlinks,transform_symlinks,idmap=user,uid=$(id -u),gid=$(id -g)
Unmounting
To unmount use the cygpath for the drive.
Usage
Opening Bash
To open bash in current directory set the CHERE_INVOKING environment before calling or set it in the users environment of Windows.
Opening bash like this from the Windows Command-Prompt, when a mintty is running, it will get the environment from the mintty instance so also the ssh-agent.