Cygwin

From miki
Jump to navigation Jump to search

References

  • Local Reference Cygwin documentation can be found at /usr/share/doc/cygwin-doc-1.4.

Packages

Creating a local setup.ini

Packages in Cygwin can be installed from repositories. The content of these repositories is described in a file setup.ini. Each package in the repository is made of one or more .bz2 files (the actual package) and a file setup.hint (spec) describing the content of the package. The setup.ini file of the repository is actually a compilation of all setup.hint files in the repository along with additional information. Cygwin apparently does not want to install a package from a single .hint file (or at least have not found how to do so). In that case you need to generate the setup.ini file. Here's an example how this can be done.

Example is based on package recode v3.6.1.

  • Download the package above, and create following directory structure within Cygwin Local Package Directory (where setup.ini location is already shown).
.
|-- http%3a%2f%2fftp....
|   ...
`-- local
    |-- release
    |   `-- recode
    |       |-- recode-3.6-1-src.tar.bz2
    |       |-- recode-3.6-1.tar.bz2
    |       |-- recode-devel
    |       |   |-- recode-devel-3.6-1.tar.bz2
    |       |   `-- setup.hint
    |       `-- setup.hint
    `-- setup.ini
  • Create the file ./local/setup.ini with the following content:
# File manually generated on 20081105.
# setup-timestamp and setup-version are completely fake...
setup-timestamp: 1214338445
setup-version: 2.573.2.2

@ recode
sdesc: "GNU recode utility"
ldesc: "The program recognizes or produces approx. 150 character sets and can convert almost any character set to almost  any other."
category: text
requires: cygwin libintl3
version: 3.6-1
install: release/recode/recode-3.6-1.tar.bz2 838836 c185a17d8e1eb964611d26cbfe9b0e05
source: release/recode/recode-3.6-1-src.tar.bz2 1731299 83b0f5940aa5d2a82355586b6d977bab
  • (in gray) First the header
  • (in black) Then entry giving the package name
  • (in red) Then a copy-paste from setup.hint file.
  • (in green) Then the version (derived from package name)
  • (in blue) And finally two entries for binary and source package, giving for each: location of .bz2 file, the file size in bytes, the md5 checksum (as given by md5sum).

When done, simply install the package by running Cygwin setup.exe, asking to Install from Local Directory. Cygwin will see the new file setup.ini, and proposes for install the packages listed inside.

Troubleshooting

Cygwin

  • Look for multiple copies of the cygwin DLL cygwin1.dll. Removes all copies that are not in the cygwin install directory (usually c:\cygwin).
  • List of interfering software:
    • NoMachine NX Client - installs a custom version of the Cygwin DLL which can cause multiple interferences:
      • Cygwin crashes when multiple instances are launched.
      • Running cygwin commands (like ls) in a NT command shell when another instance of cygwin is running creates application error (Memory cannot be "read").
      • setup.exe fails because it runs several instances of bash.
    • SysInternals' Process Monitor
      • All back-quoted commands or $() commands in scripts fail until Windows is restarted

Missing DLL using strace

strace can be used to check whether an application needs a .DLL but cannot find it. This error is not reported by default on cygwin, so an application may just leave without any message, giving the impression that it simply does nothing. strace on the other hand will report about the missing .DLL. To solve the problem, install the .DLL somewhere in your PATH, or edit the PATH to include the .DLL location.

Example ([1]):

$ strace /usr/local/sbin/authtest                   # This will open a dialog box to complain about DLL.
$ export PATH=$PATH:/usr/local/lib/bin
$ /usr/local/sbin/authtest ausername

Administration

Adding Users

Create the user in Windows User Management first, then use command mkpasswd to add it to Cygwin environment.

$ net user <username> <password> /add /yes
$ net localgroup <an_local_group> <username> /add
$ mkpasswd -l -u <username> >> /etc/passwd

Installing a Service

Reference: Cygwin inetutils-1.5 OLD-README

Use cygrunsrv to install a Cygwin process as a service in Windows. Some examples:

$ cygrunsrv -I inetd -d "CYGWIN inetd" -p /usr/sbin/inetd -a -D
$ cygrunsrv --install imapd --desc "Courier IMAP daemon" --disp "CYGWIN imapd" \
  --path /usr/local/sbin/imapd-service.sh --termsig QUIT --type auto --shutdown

By default this will install the service to run under account SYSTEM (LocalSystem), who is the user that has all the required permission up to Windows XP. For Windows 2003 Server or Windows Vista, see below.

Start/Stop the service

Manually starting and stopping is possible via

$ net start <service>
$ net stop <service>

Creating a Privileged User in Windows 2003, Windows Vista

Beginning with Windows 2003 Server and Windows Vista, the SYSTEM user is lacking the "Create a token object" right, which is required to change the user without providing the password. On these systems you have to create an admin user account with special additional user rights. You can use the Cygwin tool editrights or the local/domain security policy mmc snap in to set these rights:

Create a token object
Act as part of the operating system
Replace process level token
Increase quotas
Logon as a service

Get a Bash shell owned by another user

For instance, to open a Bash window as user localuser (you'll need to type in the user's password)

$ runas /user:localuser c:\cygwin\Cygwin.bat

Another method is to apply the same trick used to log as user SYSTEM (see below). This trick is even better since it doesn't require the user password, and hence would enable privilege escalation. However Windows security system prevents a process running as a user without providing the user password to connect to network shares.

Get a Bash shell owned by SYSTEM

This is a trick I found to easily open a Bash windows as user SYSTEM. First create a dummy service bashsystem:

$ cygrunsrv --install bashsystem --desc "Bash SYSTEM" --disp "CYGWIN bashsystem" --path /Cygwin.bat --type manual --interactive

Next, to open a Bash shell as user SYSTEM:

$ net start bashsystem

File permissions

  • Use getfacl and setfacl to set NT file permissions and access conditions.
  • To copy ACL from one file to another
% getfacl srcfile | setfacl -f - dstfile

Install SSHD

Reference: [2]

  • Install the packages:
cygrunsrv
openssh
  • Launch ssh host config:
ssh-host-config -y       # Yes to all question
  • When queried for CYGWIN variable, answer tty ntsec
  • Launch the service:
cygrunsrv -S sshd        # or 'net run sshd'
  • Optionally, open the SSH port (22) on the firewall

Miscellaneous Tips

  • Interesting post about the 'su' command, explaining why there is no 'su' in Cygwin, and how 'ssh' overcomes this.
    • Other relevant post.
  • Use rxvt as terminal emulator instead of standard console window.
  • The + that appears next to file permission in ls output means that there are other permissions for the file (use getfacl on the file to see them)
-rwxr-x---+ 1 beq06659 Users 2555 Oct 10 23:32 inetd.conf*       #More permissions set (getfacl inetd.conf to see them)
-rwxr-x---  1 beq06659 Users  706 Nov  8 01:27 initscript*
  • Clipboard integration
    • To enable clipboard usage in Cygwin X Server, use option --clipboard with command xwin.
    • Device /dev/clipboard can be used for command-line manipulation of the clipboard (note that /dev/clipboard is not listed in /dev directory). The commands getclip and putclip can be used for the same purpose (package cygutils):
$ cat myfile >/dev/clipboard                         # To copy some text to the clipboard
$ cat myfile | putclip                               # ... same effect
$ cat /dev/clipboard                                 # To print the content of the clipboard
$ getclip                                            # ... same effect
  • Change the prefix used by cygwin to mount windows partitions (default is /cygdrive):
mount --change-cygdrive-prefix /win

Cygwin Useful commands

id

Use command id to print user identity

getclip

Read from the Windows clipboard and write it to standard output (same as cat /dev/clipboard)

getfacl

Display file and directory access control lists (ACLs).

putclip

Read standard input and write into windows clipboard (same as cat >/dev/clipboard)

regtool

  • regtool is the command-line equivalent of Regedit. It can be used to manipulate the content of the Windows registry. Some examples:
% regtool set -b '\prefix\KEY\VALUE' [data ...]   # REG_BINARY
% regtool set -e '\prefix\KEY\VALUE' [data ...]   # REG_EXPAND_SZ
% regtool set -i '\prefix\KEY\VALUE' [data ...]   # REG_DWORD
% regtool set -d '\prefix\KEY\VALUE' [data ...]   # REG_DWORD
% regtool set -m '\prefix\KEY\VALUE' [data ...]   # REG_MULTI_SZ
% regtool set -s '\prefix\KEY\VALUE' [data ...]   # REG_SZ
% regtool unset -q '\prefix\KEY\VALUE'            # -q quiet (no error output)
with prefix being HKCU, HKEY_CURRENT_USER, ... (see man regtool or regtool --help for more)
  • Check whether a KEY exists:
# in Bash...
% if ( regtool check -q '\HKEY_LOCAL_MACHINE\KEY' ); then echo Found; else echo Not Found; fi
# in CMD.EXE...
> regtool check -q '\HKEY_CURRENT_USER\KEY'
> if errorlevel 1 echo Not Found!
  • Check whether a VALUE exists:
# in Bash...
# ... we use list -l to limit output to KEYs!
% if ( regtool list -l -q '\prefix\KEY' | grep -q "^VALUE$" ); then echo Found; else echo Not Found; fi
% if ( regtool get -q '\prefix\KEY\VALUE' >/dev/null ); then echo Found; else echo Not Found; fi
# in CMD.EXE...
# ... we use list -l to limit output to KEYs!
> regtool list -l -q '\prefix\KEY' | find /i "VALUE" >\nul
> if errorlevel 1 echo Not Found!
> regtool get -q '\prefix\KEY\VALUE' >\nul
> if errorlevel 1 echo Not Found!
  • Beware of the use of back-slashes:
% regtool set -b '\HKEY_CURRENT_USER\Key\Value' [data ...]   # OK! works both for Cygwin and CMD.EXE
% regtool set -b '\\HKEY_CURRENT_USER\Key\Value' [data ...]  # NOK! Only works for CMD.EXE
% regtool set -b '\\HKEY_CURRENT_USER\\Key\Value' [data ...] # NOK! Only works for CMD.EXE

Cygwin Applications

rxvt

rxvt is a colour vt102 terminal emulator intended as an xterm replacement. It can be launched without starting any X Window System.

To install, simply add package rxvt.

To configure the default shell used by rxvt, define the following user variable as windows environment variable:

USER=/bin/bash

Appearance is controlled in file ~/.Xdefaults

rxvt*geometry:          160x50
rxvt*loginShell:        true
rxvt*font:              Lucida Console-12
rxvt*saveLines:         3000
rxvt*scrollBar_right:   true

Cygwin/X

Install:

  • Run setup.exe
  • Install the packages: xorg-server, xinit, xorg-docs, X-start-menu-icons, some font packages

Setup keyboard layout:

  • From the command-line, run the command:
startxwin.exe -- -xkblayout be
  • From Start Menu shortcut, edit the shortcut target (need to add quotes!):
C:\cygwin\bin\run.exe /usr/bin/bash.exe -l -c "/usr/bin/startxwin.exe -- -xkblayout be"