How to get started with Linux commands on Windows: Cygwin

I know about virtual machines and that Microsoft has implemented WSL (Windows Subsystem for Linux). The virtual machine requires resources (not suitable for very weak computers). Other options are also not very simple.

Cygwin installs like a normal portable application. After installation, a shortcut is added to the desktop when clicked on which opens the Linux console. Moreover, this is not just an emulator – there are real Linux programs here – that is, how they will work in the same way in a real Linux OS.

Related:

This approach has limitations – first of all, there is no access to hardware (to Wi-Fi cards, network adapters, video card). There is also no package manager as such – you can select packages to install in the Cygwin installer. That is, installing something that is not included in the standard Linux distribution (for example, sqlmap, WPScan, etc.) will be problematic.

That is, on the one hand, it will not work as a full-fledged Linux. But on the other hand, there are plenty of programs to familiarize yourself with the command line. For example, cURL, grep, wget, sed, vim and many more.

For example, you can practice parsing (almost all examples from “Web site parsing in command line” will work in this environment).

An example of running a command in Cygwin:

curl -s  https://hackware.ru/  | grep -E -o '<h3 class=ftitle>.*</h3>' | sed 's/<h3 class=ftitle>//' | sed 's/<\/h3>//'

Cygwin mimics the Linux directory structure, so you can also get started with the hardware of this operating system.

See also: Linux directory structure. Important Linux files

How to install Cygwin

Go to https://www.cygwin.com/, find the setup-x86_64.exe file there, download and run it.

During the installation process, the program will prompt you to select a download mirror, a folder to save and programs to install – all this can be left unchanged.

After installation, a shortcut will appear on the desktop.

SSH, Python, Vim, compilation tools

Don't take Cygwin as a mere toy. It is a highly advanced environment that contains many Linux tools, including those for advanced users.

Cygwin has a full-fledged SSH client that allows you to connect to remote systems and even generate SSH keys. That is, you can use Cygwin to connect to your remote servers or other Linux systems – Cygwin can replace third-party SSH clients that are not always convenient

Cygwin already includes Python – you can start learning this language without installing it on Windows.

A console text editor such as Vim is a must-have tool when editing configurations on remote systems – here you can get started with it in a relaxed atmosphere.

Git is available – update your Git repositories, or you can simply use it to download (clone) other developers' repositories.

Very advanced users can even use compilation tools.

You can see PHP, web server and more in packages.

So

Cygwin is a great way to get started with Linux. It works even on weak computers and supports many features of the Linux OS.

In this topic, we will look at simple and complex examples of using Cygwin, including compiling and installing tools.

How to access disks in Cygwin

If you left the default path when installing Cygwin, then Cygwin is stored in the C:\cygwin64 folder. It is this folder that is the root of the file system for Cygwin, above which it is impossible to rise.

In Linux, other disks (if any) are mounted to the selected mount points below the file system tree. That is, in Cygwin, you can view the list of files, create new files, edit and delete existing files on all disks of the Windows operating system (if write permissions allow). But you need to know where they are mounted.

To view a list of physical disks on a system:

ls -l /dev/s*

Output example:

brw-rw-rw- 2 MiAl Отсутствует 11, 0 окт 14 10:23 /dev/scd0
brw-rw-rw- 1 MiAl Отсутствует 8, 0 окт 14 10:23 /dev/sda
brw-rw-rw- 1 MiAl Отсутствует 8, 1 окт 14 10:23 /dev/sda1
brw-rw-rw- 1 MiAl Отсутствует 8, 2 окт 14 10:23 /dev/sda2
brw-rw-rw- 1 MiAl Отсутствует 8, 3 окт 14 10:23 /dev/sda3
brw-rw-rw- 1 MiAl Отсутствует 8, 4 окт 14 10:23 /dev/sda4
brw-rw-rw- 2 MiAl Отсутствует 11, 0 окт 14 10:23 /dev/sr0
lrwxrwxrwx 1 MiAl Отсутствует 15 июл 12 23:41 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx 1 MiAl Отсутствует 15 июл 12 23:41 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx 1 MiAl Отсутствует 15 июл 12 23:41 /dev/stdout -> /proc/self/fd/1

As you can see from the previous output, /dev/sda is divided into four partitions: /dev/sda1, /dev/sda2, /dev/sda3, and /dev/sda4.

But disks are placed here as devices – that is, these devices in Linux can be accessed when formatting, creating and deleting volumes (perhaps Cygwin cannot interact with disks at this level). To access the disk files, they must be mounted, that is, connected to the file system tree (for details, see the article “Linux directory structure. Important Linux files”).

To view information about mounted disks and mount points, run the command:

mount

Output example:

C:/cygwin64/bin on /usr/bin type ntfs (binary,auto)
C:/cygwin64/lib on /usr/lib type ntfs (binary,auto)
C:/cygwin64 on / type ntfs (binary,auto)
C: on /cygdrive/c type ntfs (binary,posix=0,user,noumount,auto)
Z: on /cygdrive/z type vboxsharedfolderfs (binary,posix=0,user,noumount,auto)

From this information, it follows that the C drive is mounted in the /cygdrive/c. directory. Drive Z: (this is a network drive) is mounted in the /cygdrive/z directory.

The entry C:/cygwin64 on / means that the C:/cygwin64 folder is mounted as the root filesystem (/).

To see all the drives on the system, including network drives and including flash drives and other external drives, run the command:

ls -l /cygdrive/

So, any drive in Windows from Cygwin can be accessed through the /cygdrive/ folder. For example, to view files and folders on the C drive:

ls -l /cygdrive/c

How to install packages (programs) in Cygwin

Let’s consider the task, how to install additional packages in Cygwin

Let's take an example: we need to install a number of dependencies, namely the following packages (they are needed for compilation):

  • libssl-devel
  • ibbz2-devel
  • libgmp-devel
  • zlib-devel
  • libOpenCL-devel
  • libcrypt-devel

This can be done in two different ways.

The first way to install programs in Cygwin

First, you can run the installer again, and at the stage of selecting packages, switch to the “Full” list, enter the package name and double-click on the word “Skip”:

So that the version number appears instead:

Immediately after that, enter the name of the second package (the first package seems to disappear – no need to worry about it), click on the word “Skip”.

Repeat this for all packages.

When done, clear the search field and switch to the Pending list to see a list of packages to install:

Click next and complete the installation of the selected packages.

The second way to install programs in Cygwin

Perhaps you will like another way better: move the setup-x86_64.exe installation file to the C:\cygwin64\ folder and then open a Windows command prompt (PowerShell) and just run a command like this:

C:\cygwin64\setup-x86_64.exe -q -P PACKAGE1 -P PACKAGE2 -P PACKAGE3

For example, for my case of installing six packages, the command looks like this:

C:\cygwin64\setup-x86_64.exe -q -P libssl-devel -P libbz2-devel -P libgmp-devel -P zlib-devel -P libOpenCL-devel -P libcrypt-devel

After starting the command, downloading and installing the required CygWin packages will begin.

Choose any of these methods.

apt-cyg – apt for Cygwin

How to install apt-cyg

In addition to the method shown above for installing packages in Cygwin using the Cygwin installer itself, there is an analogue of the apt (apt-get) program for Cygwin, it is called apt-cyg.

To install apt-cyg run the following commands:

wget  https://raw.githubusercontent.com/transcode-open/apt-cyg/master/apt-cyg 
install apt-cyg /bin

If an error appears

-bash: wget: command not found

then do it like this:

curl https://raw.githubusercontent.com/transcode-open/apt-cyg/master/apt-cyg > apt-cyg
install apt-cyg /bin

apt-cyg needs the lynx and wget packages to work properly. If you do not have them, then you may need to install them in the usual way, using the Cygwin installer.

Now, to install new packages, you can use a command like this:

apt-cyg install PACKAGE

For example:

apt-cyg install libxml2

apt-cyg command reference

NAME

apt-cyg - package manager utility

SYNOPSIS

apt-cyg [operation] [options] [targets]

DESCRIPTION

apt-cyg is a package management utility that tracks installed packages on a Cygwin system. Invoking apt-cyg involves specifying an operation with any potential options and targets to operate on. A target is usually a package name, file name, URL, or a search string. Targets can be provided as command line arguments.

OPERATIONS

install

Install package(s).

remove

Remove package(s) from the system.

update

Download a fresh copy of the master package list (setup.ini) from the server defined in setup.rc.

download

Retrieve package(s) from the server, but do not install/upgrade anything.

show

Display information on given package(s).

depends

Produce a dependency tree for a package.

rdepends

Produce a tree of packages that depend on the named package.

list

Search each locally-installed package for names that match regexp. If no package names are provided in the command line, all installed packages will be queried.

listall

This will search each package in the master package list (setup.ini) for names that match regexp.

category

Display all packages that are members of a named category.

listfiles

List all files owned by a given package. Multiple packages can be specified on the command line.

search

Search for downloaded packages that own the specified file(s). The path can be relative or absolute, and one or more files can be specified.

searchall

Search cygwin.com to retrieve file information about packages. The provided target is considered to be a filename and searchall will return the package(s) which contain this file.

mirror

Set the mirror; a full URL to a location where the database, packages, and signatures for this repository can be found. If no URL is provided, display current mirror.

cache

Set the package cache directory. If a file is not found in cache directory, it will be downloaded. Unix and Windows forms are accepted, as well as absolute or regular paths. If no directory is provided, display current cache.

OPTIONS

--nodeps

Specify this option to skip all dependency checks.

--version

Display version and exit.

Error “apt-cyg: command not found”

If at startup

apt-cyg

You got an error

-bash: apt-cyg: command not found

then this means that you need to install the apt-cyg script as shown earlier in this post.

How to update Cygwin

Updating Cygwin packages is the same as installing.

To update all Cygwin components and installed packages, go to https://www.cygwin.com/, find the setup-x86_64.exe file there, download and run it.

Then follow all the steps of the installer, when it finishes, Cygwin will be updated.

How to run a program from Cygwin on Windows

With the cygstart utility, you can run a program on Windows directly from Cygwin:

cygstart notepad

If the program being launched is not included in the PATH, then you need to specify the full path to it.

Optionally, you can pass arguments to the program being run, for example:

cygstart notepad C:\file.txt

How to convert Windows and UNIX paths

With the cygpath utility you can convert Windows and UNIX paths between each other.

For example:

Windows-formatted path with relative path to file:

cygpath --windows ~/.bashrc
C:\cygwin64\home\MiAl\.bashrc

Show Linux path for mixed notation file:

cygpath --unix C:/cygwin/bin/ls.exe
/cygdrive/c/cygwin/bin/ls.exe

The path to the file in the Cygwin directory in Linux format with the absolute path to the file:

cygpath --unix C:\\cygwin\\bin\\ls.exe
/cygdrive/c/cygwin/bin/ls.exe

Note that bash interprets the backslash "\" as an escape character (escaping), so you must type it twice in the bash shell if you want it to be recognized as a literal backslash.

Mixed notation output:

cygpath --mixed ~/.bashrc
C:/cygwin64/home/MiAl/.bashrc

To show the path to the Desktop directory and exit:

cygpath -D
/cygdrive/c/Users/MiAl/Desktop

To show the path to the Profiles directory and exit:

cygpath -H
/cygdrive/c/Users

To show the path to the My Documents directory and exit:

cygpath -O
/cygdrive/c/Users/MiAl/Documents

To show the path to the Programs directory in the Start Menu and exit:

cygpath -P
/cygdrive/c/Users/MiAl/AppData/Roaming/Microsoft/Windows/Start Menu/Programs

To show the path to the system directory and exit:

cygpath -S
/cygdrive/c/Windows/System32

To show the path to the Windows directory and exit:

cygpath -W
/cygdrive/c/Windows

Displaying system information about Cygwin (cygcheck)

The cygcheck utility displays system information, checks installed packages, or queries the package database.

Usage:

cygcheck [-v] [-h] PROGRAM
cygcheck -c [-d] [PACKAGE]
cygcheck -s [-r] [-v] [-h]
cygcheck -k
cygcheck -f FILE [FILE]...
cygcheck -l [PACKAGE]...
cygcheck -p REGEXP
cygcheck --delete-orphaned-installation-keys
cygcheck -h

List system information, check installed packages, or query package database.

At least one command option or a PROGRAM is required, as shown above.

  PROGRAM              list library (DLL) dependencies of PROGRAM
  -c, --check-setup    show installed version of PACKAGE and verify integrity
                       (or for all installed packages if none specified)
  -d, --dump-only      just list packages, do not verify (with -c)
  -s, --sysinfo        produce diagnostic system information (implies -c)
  -r, --registry       also scan registry for Cygwin settings (with -s)
  -k, --keycheck       perform a keyboard check session (must be run from a
                       plain console only, not from a pty/rxvt/xterm)
  -f, --find-package   find the package to which FILE belongs
  -l, --list-package   list contents of PACKAGE (or all packages if none given)
  -p, --package-query  search for REGEXP in the entire cygwin.com package
                       repository (requires internet connectivity)
  --delete-orphaned-installation-keys
                       Delete installation keys of old, now unused
                       installations from the registry.  Requires the right
                       to change the registry.
  -v, --verbose        produce more verbose output
  -h, --help           annotate output with explanatory comments when given
                       with another command, otherwise print this help
  -V, --version        print the version of cygcheck and exit

Note: -c, -f, and -l only report on packages that are currently installed. To search all official Cygwin packages use -p instead.  The -p REGEXP matches package names, descriptions, and names of files/paths within all packages.

Cygwin repository mirrors

Cygwin has a number of FTP mirrors for its repositories. A complete list of current mirrors can be found at https://cygwin.com/mirrors.html. The list is automatically updated and maintained.

To display the current mirror, run the command:

apt-cyg mirror

To set a new mirror, run a command like:

apt-cyg mirror URL

For example:

apt-cyg mirror https://cygwin.cu.be/

32 bit Cygwin

At first glance, there is only one installer on the Cygwin home page, setup-x86_64.exe – the installer for the 64-bit version. In fact, the 32-bit Cygwin installer is still present at the bottom of the page, but the authors strongly advise against using it, here's what the official website says about it:

Address space is a very limiting factor for Cygwin. These days, a full 32 bit Cygwin distro is not feasible anymore, and will in all likelihood fail in random places due to an issue with the fork(2) system call.

Therefore we recommend using 32 bit Cygwin only in limited scenarios, with only a minimum of necessary packages installed, and only if there's no way to run 64 bit Cygwin instead.

You have been warned. If you're still sure you really need a 32 bit Cygwin, and there's absolutely no way around it, you may run the setup-x86.exe installer.

Running GUI Programs in Cygwin

Cygwin can run GUI programs as well as X window managers and even X desktop environments.

Cygwin/X packages are located in the X11 category.

  • xorg-server (required, the Cygwin/X X Server)
  • xinit (required, scripts for starting the X server: xinit, startx, startwin (and a shortcut on the Start Menu to run it), startxdmcp.bat )
  • xorg-docs (optional, man pages)
  • xlaunch (optional, a wizard for starting X sessions)

An example of running a GUI program in Cygwin.

Install apt-cyg if you haven't already:

curl https://raw.githubusercontent.com/transcode-open/apt-cyg/master/apt-cyg > apt-cyg
install apt-cyg /bin

Install the X server and the required scripts:

apt-cyg install xorg-server xinit

Start the X server:

startxwin &

Press Enter to see the command line prompt.

Set the value of the DISPLAY variable:

export DISPLAY=:0.0

Run the desired GUI program:

~/johnny/johnny

See also: Chapter 3. Using Cygwin/X

Error “Could not connect to any X display” in Cygwin

If you get an error like the following when starting a GUI program:

QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-MiAl'
qt.qpa.screen: QXcbConnection: Could not connect to display
Could not connect to any X display.

It means that you have not started the X server and/or have not set the DISPLAY variable. To fix the error, follow the steps shown above.

How to reboot or shutdown Cygwin

You may notice that even after closing the console and then opening it, some processes running in Cygwin continue to run. You can check them with the commands

ps -e

Or

top

In Windows Task Manager, you can see the conhost.exe process owned by Cygwin even after closing all terminals.

Programs running with the X server are also not disabled when closing Cygwin terminals.

Since Cygwin is not a complete Linux kernel, some small things like shutting down the machine will not work the way they do on Linux.

The only ways that these processes can be closed is by restarting Windows or manually removing tasks.

Recommended for you:

Leave a Reply

Your email address will not be published. Required fields are marked *