Cygwin's potholes

Source: Internet
Author: User
Tags time zones posix set background

Background introduction to problems encountered in use

There are many problems with Cygwin use, such as permissions, time zones, and so on.

Purpose of this article

Figure out what operations or features Cygwin will conflict with Windows itself, so that you can better use Cygwin to manage your Windows Server system.

Environment

操作系统:WindowsServer2012 x64

Cygwin版本: CYGWIN_NT-6.2 TEST 2.0.2(0.287/5/3)2015-05-0817:00 x86_64 Cygwin

Cygwin Core Concepts Windows security overview

The Windows NT system contains a complex ACL-based security model, which, by default, maps the owner and permissions of the Win32 file to ACLs on the file system (usually NTFS) that supports them cygwin. Users with administrator permissions can modify the owner of the file.

In the Windows security model, almost all object is safe. The object here refers to files, processes, threads, semaphores, and so on. Each object has a data structure called SD (Security descriptor), and the SD contains all the rights management information.

The SD contains the following elements:

    • Flags which control several aspects of this SD. This isn't discussed here.

    • Sid of Object Owner

    • SID of the object group

    • An ACE (Access control Entries) list, called a DACL (discretionary access control list)

    • Another ace list, called a SACL (Security Access Control list)

Each ACE consists of a sid (Security IDentifier). A SID is a unique identifier for all users, groups, computers, and AD domains. Unlike Uid/gid in POSIX, SIDS are globally unique across computers and domains.

For example, the SID of a computer "foo" is:

s-1-5-21-165875785-1005667432-441284377

The SID for a user "JohnDoe" in this computer "foo" is:

s-1-5-21-165875785-1005667432-441284377-1023

Format:

    • The 1th paragraph is constant s; The 2nd paragraph is the version number of the SID structure, currently 1;

    • 3rd, 4 represents the type or category of SID, and computers and domains always start with s-1-5-21;

    • The next 3 paragraphs are a 32-bit value that represents a 96-bit unique identifier for a computer system;

    • The user's SID is appended with a number to the computer SID, which is called the RID (relative identifier), and the RID is created when the user creates it.

It is a similar structure for domains.
SID of a domain "bar.local":

s-1-5-21-186985262-1144665072-740312968

SID of a user "JohnDoe" in the Domain "bar.local":

s-1-5-21-186985262-1144665072-740312968-1207

Note : Although the above JohnDoe user exists in the computer and exists in the domain, he has a different SID, so the two SIDs are not the same account. A different SID is a different account.

From the Cygwin 1.7.34 release, Cygwin automatically translates the SID in Windows into a POSIX uid, GID. RIDs in Windows systems are converted to/etc/passwd, UID in/etc/group, GID.

The ACE consists of 3 parts:

    • ACE Type: Allow Ace or deny ACE

    • 32-bit permission bit

    • These permissions allow or deny SIDs

File permissions

Cygwin implements the file permissions of the Windows system to POSIX file permissions, while POSIX files manage file permissions with ACLs. POSIX file and directory permissions are controlled by the Mount option, which is set to ACL by default.
This means that by default, the contents of the/etc/fstab file are:

None/cygdrive cygdrive binary,posix=0,user 00

This means that Cygwin simulates POSIX file permissions, which can cause confusion in the file permissions of applications in Windows systems, and in order to solve this problem, add the Noacl parameter and change/etc/fatab to:

None/cygdrive cygdrive binary,user,noacl,posix=000

Under D:\ (that is,/cygwin/d), create 3 files to test the permissions, respectively, as shown in the following:
650) this.width=650; "title=" 150605170455 "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0 px; "border=" 0 "alt=" 150605170455 "src=" Http://s3.51cto.com/wyfs02/M01/6E/10/wKioL1Vyygjz072BAALuIcCUWTU948.jpg " height= "406"/>

On the left, the permissions for files created in Windows system, the file permissions created by default for Cygwin to impersonate POSIX file permissions, and the permissions for files created in Cygwin after adding the Noacl parameter to Cygwin are added in the image. As you can see, after adding the Noacl parameter, Cygwin will not impersonate the POSIX file permissions, and the permissions for the files created in Cygwin are consistent with the file permissions of the Windows system.

However, the above changes only modify the/cygdrive directory, that is, all the disks in the Windows system (C:, D:, E:, F: etc.), and the Cygwin itself/not modified, the same will exist the permissions problem, so need to be modified, the final complete/etc/ The contents of the Fatab file are as follows:

None/cygdrive cygdrive Binary,user,noacl,posix=000d:\cygwin64/ntfs Binary,noacl,override00d:\cygwin64\bin/usr/bin NTFS binary,noacl,override00d:\cygwin64\lib/usr/lib NTFS binary,noacl,override00
Cygserver

Cygserver is designed for Cygwin to run as a background service and does not start when the default installation cygwin. We need to open it and install it as a standard service for Windows.

Enter the command in Cygwin:

Cygserver-config

Follow the interface prompts to install the Cygserver and agree to install it as a service.

Run the service:

Cygrunsrv--start Cygserver

Cygrunsrv is actually the startup of the Windows standard service cygserver, this command can also be done under cmd: net start Cygserver, or you can press the key windows+r, type services.msc Enter, Find the Cygwi cygwerver service and start it.

To view the status of the Cygserver service:

Cygrunsrv--qurey Cygserver

Configuration file for Cygserver:

/etc/cygserver.conf
Cygwin command-line tools

All Cygwin command-line tools support help (-h)/–version parameters for Help and query versions.

Cygcheck: Lists system Information, checks the installed packages, and queries the database for the package. Cygpath: Convert Windows and UNIX file paths. For example, for the file "~/.BASHRC"-w conversion to the Windows file path "D:\CYGWIN64\HOME\YHN1792\.BASHRC", the-U translates to the Unix file path "/HOME/YHN1792/.BASHRC". Dumper: Core dump of Windows process for GDB debugging. Getconf: Writes the system configuration variable value to standard output. Getfacl: Gets the ACL for the file or directory. SETFACL: Sets the ACL for the file or directory. Kill: Sends a signal to the Cygwin process. LDD: Displays a shared lib dependency relationship. Locale: Get locale information. Mkgroup: Displays/etc/group-style groups. MKPASSWD: Displays/etc/passwd-style users. Mount: Displays the mount point, or mounts the file system. Umount: unmount the file system. passwd: Modifies the user's password or password properties. PLDD: Displays a dynamically shared object loaded by a process. PS: Displays the status of the Cygwin process. The-w parameter can list the processes of the Windows system. Regtool: View or modify the registry of your Windows system. Strace: Tracks system calls and signals. Tzset: Prints the POSIX-compatible time zone ID from the current Windows system time zone settings. Export tz=$ (Tzset)
Home Home Directory

If some other software, such as Msysgit, has set the HOME environment variable for Windows, it will affect the settings of the owner directory in Cygwin, even if different commands in Cygwin have different user home directories. For example: Cygwin the user home directory under git is set to/cygdrive/c/documents and Settings/jiangxin, and the SSH client software's home directory is set to/home/jiangxin, which can cause confusion for users.

This occurs because Cygwin determines that the home directory has several different bases to determine the main directory in order:

    • First look at the system's HOME environment variables;

    • Next, look at the home directory set for the user in/etc/passwd.

Some software follows this principle, while some Cygwin applications such as SSH do not use the HOME environment variable but instead use the settings in/etc/passwd directly. To avoid having two different user home directory settings in the same Cygwin environment, you can use the following two methods.

Method 1: Modify the Cygwin-initiated batch file (for example: C:\cygwin\Cygwin.bat) and add the following line of code at the beginning of the batch to prevent other software from being brought into the Cygwin with the HOME environment variable introduced by Windows.

Set Home=

Method 2: If you want to use the home environment variable to point to the home directory, you can manually edit the/etc/passwd file, the user home directory is modified to the home environment variable point to the directory.

command line completion ignoring file name capitalization

The file system of Windows ignores the case of file names, and it is better to use the command-line completion settings in Cygwin to ignore the case, which is more convenient.
Edit the file ~/.inputrc and add the settings:

Set Completion-ignore-case on

Or, uncomment the Well (#) number in front of the existing related settings.

After the modification, and then re-enter the Cygwin, so that the command line can be completed to the file name of the case of ignoring.

Time

To view the time zone command:

Tzset

To view the time zone environment variables:

Echo $TZ

To view the system current time (UTC time):

Cygcheck-s | Grep-i time

View current time (GMT time):

Date-r

Here's a sample to show how to set the time zone:

# # # View current UTC time $ cygcheck-s | Grep-i timecurrentsystemtime:frijun0507:50:252015### View current GMT time, utc+8 hours $ date-rfri,05jun201515:50:44+0800### Set to Los Angeles time for testing (note: At this time only the current shell environment is in effect, write to the configuration file if the global effect is required) $ export tz=america/los_angeles### UTC time Unchanged $ cygcheck-s | Grep-i timecurrentsystemtime:frijun0508:50:502015### current time into Los Angeles local time, utc-7$ date-rfri,05jun201500:50:56-0700
File system

Cygwin supports POSIX and Win32-type file path formats, using/or \ to split file paths. The Cygwin DLL library converts the POSIX format to the native NT format as needed.

$ df-h File system capacity used available% mount point D:/cygwin64/bin 16g661m15g5%/usr/bind:/cygwin64/lib 16g661m15g5%/usr/libd:/cygwin64 16G 661M 15G 5%/c:45g18g27g41%/cygdrive/cd:16g661m15g5%/cygdrive/de:3.6g3.6g0100%/cygdrive/e

From the cygwin1.7.0 version, the POSIX style layout of the Windows file system will be saved in the/etc/fatab file, which is a global file that each user's files can save in/etc/fstab.d/${user}.

$ cat/etc/fatabnone/cygdrive cygdrive binary,posix=0,user 00
Symbolic Links

The Cygwin creates symbolic links in several ways:

    • The default is created in plain text format, indicating the path of the target file, marked as DOS system property, using UTF-16 to encode the file name of the target;

    • Shortcut format for Windows. LNK, there is a special header and DOS ReadOnly property that will create this type of symbolic link when the Cygwin environment variable is set to Winsymlinks or Winsymlinks:lnk;

    • Native Windows symlinks:windows vista/2008 or later, when the Cygwin environment variable is set to Winsymlinks:native or winsymlinks: This type of symbolic link will be created when nativestrict.

    • On NFS file systems, Cygwin creates a true NFS symbolic link;

Note : The various symbolic links above can be identified and used in all environments, but if the plain text symbolic link loses the DOS system attribute, the shortcut format of the symbolic link loses the dos readonly attribute, then they will not be considered a symbolic link.

Hard Links

Hard links is fully supported on NTFS and NFS file systems. On FAT and other file systems which "T" Support Hardlinks, the "call returns" with a error, just like on other POSIX Syst Ems.

Inode

On file systems which don ' t support unique persistent file IDs (FAT, older Samba shares) the inode number for a file is CA Lculated by hashing it full Win32 path. The inode number generated by the matches, the one returned in D_ino of the dirent structure. It is worth noting, the number produced by this method was not guaranteed to be unique. However, we have not found the is a significant problem because of the low probability of generating a duplicate inode Number.

$ ls-lai/tmp/Total dosage 23281474976710864 drwxrwxrwt+1administratornone06 month 114:30.281474976710851 drwxr-xr-x+ 1administratornone06 Month 119:07..844424930152905-rw-r--r--1administratornone776 Month 114:29 awk.1040844424930152887-rw-r--r--1administratornone776 Month 114:29 awk.1668844424930150273-rw-r--r--1administratornone776 month 114:22 awk.1716
Extended Properties

Cygwin 1.7 and later supports Extended Attributes (EAs) via the Linux-specific function calls Getxattr, setxattr, list Xat TR, and Removexattr. All EAS on Samba or NTFS be treated as user EAs, so, if the name of a EA is "foo" from the Windows perspective, it ' s TRA Nsformed into "User.foo" within Cygwin. This allows linux-compatible EA operations and keeps tools like attr, or setfattr happy.

Chroot

Chroot is supported from cygwin1.1.3, but chroot is not a concept on Windows.
There are many restrictions on chroot on Cygwin:
A) chroot is not an elevated command and can be called by all users;
b) The chroot environment is not secure for local Windows processes.
Therefore, the use of chroot is not recommended in Cygwin environments.

Text mode and binary mode

Files created by the Windows on-premises app can work with the Cygwin app. For example, a file created by a Windows local app should be readable by the Cygwin app and vice versa.

However, in a text file, Win32 and Unix have different line terminators. The line terminator in a Unix text file is a newline (lines feed,lf), while the Win32 text file is two characters: a carriage return (carriage RETURN,CR) and a newline (line FEED,LF).

Unix/binary: If you select this item, Cygwin does not convert the line terminator in the text file. This is the default option and applies to most users. Selecting this option means that files created with standard Windows tools, such as Notepad, appear to have an extra line terminator when viewed with a program compiled in a Cygwin environment, such as Cat.

Dos/text: In this mode, Cygwin attempts to convert the line terminator of the file, so that files and programs created with the standard Windows Editor are like programs that run under Cygwin, because each line terminator has only one LF character. Cygwin does not perform this conversion for files opened in binary mode. (Recall that when you open a file, you need to pass a flag indicating whether the file is text or binary data.) You should not select this option unless you need to use some of the "stubborn" programs on your development host in your environment.

Creation of processes

The Cygwin fork system call does not map well with the Win32 API, which makes it difficult to implement the fork call correctly. Currently, Cygwin Fork is not a copy-on-write mechanism.
In Cygwin, the process by which the parent process creates the child process:

    • The parent process Initializes a space on the Cygwin process table for the child process;

    • Create a pending subprocess using Win32 's CreateProcess call;

    • The parent process calls setjmp to save his own context and sets a pointer to that context in Cygwin shared memory;

    • The parent process copies the data of its own address space into the address space of the pending child process to populate the child process's. Data and. BSS;

    • When the child process's address space initialization is complete, the child process begins to run, and the parent process waits for the mutex;

    • The child process discovers that it has been forked out and then jumps using the saved switch buffer. The child process then sets the amount of mutex that the parent process waits for and is blocked by another mutex;

    • The parent process copies the stack data to the child process, after which the parent process releases the mutex that the child process waits for and returns from the fork call;

    • Finally, the child process is activated from the last mutex, recreating the memory-mapped area from the shared area, and returning from the fork;

In short, the current implementation mechanism of Windows makes it impossible for Cygwin to achieve a perfect, reliable fork call, and occasional fork failures are unavoidable.

Internationalization

According to the POSIX standard, Cygwin can change the language by setting the Lc_all, lc_ctype, and Lang environment variables, which are three variables in order. The content of the variable is compliant with the POSIX standard.
The format is:

language[[_territory][.charset][@modifier]]
    • "Language" is a two-bit, lowercase language code string defined by ISO 639-1, which can be represented using ISO 639-3 's three-bit string if no language is found in ISO 639-1.

    • "TERRITORY" is a two-bit uppercase country code string defined in ISO 3166.

    • "CharSet" is the character encoding.

For example:

"Fr_fr. UTF-8 "language = French, Territory =france, CharSet = UTF-8

Note: The default locale is "C.utf-8".

The Windows system uses UTF-16 to encode the file name, so Cygwin will determine how to convert the file name in UTF-16 format to the format specified in Cygwin according to Lc_all, Lc_ctype, and Lang variables.

Use the LOCALE.EXE-A command to see a list of all locales.

Potential Problems
1. When a Cygwin process starts, the environment variable of Windows is converted from UTF-16 to UTF-8, and if the environment variable contains only ASCII encoding, then there is no problem. However, if you include a local character set, such as GBK, it is possible that garbled characters will occur.
2. Symbolic Link: The symbolic link contains the path name of the target file, if the pathname contains non-ASCII code, then change the locale, it may be garbled. This issue does not exist for cygwin1.7 and later versions.

Using the native Win32 path

It is possible, but it is not recommended.

The following path is considered to be the native Win32 path:
A) The path starting with the drive letter:

C:\foo C:/foo

b) At least one backslash is included in the full path:

c:/foo/bar\baz/...

c) The UNC path uses a backslash:

\\server\share\ ...
File permissions

Fat or FAT32

A) Read permission: In the FAT or FAT32 file system, the file is always readable; b) Write permission: chmod can only change the file's write permission; c) Executable permissions: Whether the file has executable permissions on a UNIX system depends on whether the file has an X permission bit, The Windows system is judged by whether the file name extension is a. bat/.com/.exe/file or not, starting with #!.

Ntfs

File permissions are determined by the ACL of the file

Nfs

File permissions are standard POSIX permissions that are transferred from the server side via the NFSV3 protocol.

/etc The following special files need to be ensured with Publicread permissions:

/etc/fstab/etc/fstab.d/$USER/etc/passwd/etc/group
Illegal file name

The WIN32 application will consider the file name of the Aux, COM1, LPT1 or PRN to be illegal, for example: Prn.txt or Foo.aux, etc. However, this restriction is not valid for Cygwin applications, and Cygwin applications do not consider them to be illegal.

Illegal characters in the file name

In the Windows file system, the ASCII code number 1-31 character cannot be used in the file name, and the following characters have special meanings in the Win32 API:

"*:<>?| \

Cygwin will convert the characters above except \ into Unicode, and the range is: 0xf000-0xf0ff. Because \ can be used as a delimiter in the file path, it is not converted.

The file name contains infrequently used (foreign) characters

The Windows file system uses UTF-16 Unicode encoding to store file name information. If you do not use UTF-8 encoding, then there is a good chance that one or more characters will not correspond to your character set.

It is recommended to use the UTF-8 character set.

Case-sensitive file names

In Win32 systems, filenames are case insensitive.

. exe extension

On Windows systems, the Win32 executable can be executed without an. exe when executing the command, if it is an. exe extension. However, a file with an extension of. bat/.com must be carried with its extension.

For example: if filename.exe exists and filename does not exist, then LS filename will list information for the Filename.exe file.

If a shell script Myprog and a program myprog.exe exist at the same time, then the shell script without the. exe extension MYPROG will be executed first. That is:./myprog will execute the shell script instead of the Myprog.exe, and if the Myprog script does not exist, it will be to execute the. exe file.

@pathname

Using @pathname in bash and cmd can have different effects, as in the following examples:

[Email protected] bash$/bin/echo ' Thisis "a Long" line ' > mylistbash$/bin/echo @[email protected]bash $ cmdc:\> C:\cygwin\bin\echo @mylistThisis a long line
Important points to be aware of

Cygwin is not a complete operating system, unlike the native features of Windows, here are a few things to keep in mind:

    1. Cygwin is simply a POSIX mapping of the Windows system's file system and does not provide the driver of the filesystem itself;

    2. Some Windows-brought command-line tools (for example, Find.exe,link.exe,sort.exe, etc.) may conflict with Cygwin to ensure that these commands use absolute paths (/usr/bin/find). or in the PATH environment variable to put Cygwin bin directory in front;

    3. The Windows system does not recognize the Cygwin file path format, so it needs to be converted with the Cygpath command, for example: Notepad.exe "$ (cygpath-aw" Desktop/phone Numbers.txt ")";

    4. Some cygpath tools, such as Ncftp/lynx/wget, require a network connection, and Cygpath is dependent on the Windows network connection, so if there are problems with these tools, you need to use Windows tools (ping.exe/telnet /FTP) for commissioning;

    5. The symbolic links of Cygpath and windows are not the same;

    6. encoding format;

    7. User, group, file permissions, ACLs and other security characteristics of the difference;

Problem rollup Cygwin installing sshd

Cygwin after successful installation, the connection to SSH localhost has been error:

Cygwin Connection closed By::1

Took a lot of time to find, finally solved, the original text is as follows:

See an official mail question and answer from Cygwin on Google. Refer to installing the Cygwin SSH daemon. The problem is a special permission problem, the SSHD service running under the system account needs to have the privilege of changing the user ID (on the issue of permissions, in Cygwin's/usr/share/doc/cygwin/openssh. The README has been made clear: 2003Server has a funny new feature. When starting services under System account, these services has nearly all user rights which SYSTEM holds...exceptfor th E "Create a token object" right, which are needed to allow public key authentication:-(). So the event log will have seteuid errors. As for the solution, let Cygwin help you create two special users sshd and Cyg_server, where the sshd service runs under Cyg_server user, Cyg_server has the appropriate permissions (in/usr/share/doc/cygwin/ OpenSSH. Also described in the README).

The key is that all two users are created.

Solve the problem of Cygwin under VI and VIM key and BACKSPACE

The default VI (VIM) does not have a profile, thus causing the arrow keys to appear ABCD, and the backspace will only move the cursor, the character does not disappear. Cygwin The example of the VI (VIM) configuration file, copy it directly, and refer to the following command:

$ Cp/usr/share/vim/vim74/vimrc_example.vim ~/.VIMRC
The problem of "Command not found" in Cygwin

When compiling some Unix software on Cygwin, such as FFmpeg, Gvim, etc., due to the difference of unix/dos system, there are some strange and strange problems, which is often encountered recently:

Line 9: $ ' \ R ': Command not Found

Such errors, preliminary judgment may be due to the difference between \ r \ n, encountered this situation, you can use Dos2unix to repair.
Execution: Dos2unix Configure configure, then run:./configure, there will be no more errors.

Problems caused by line breaks

The line break in Windows is \ r \ n, and the newline character under Linux/unix is \ n.
which

Carriage return: \r=0x0d return, #回车 (carriage return) newline character: \n=0x0a (Ten) newline. #换行 (newline) The newline character used in DOS is ^m$, which we call two symbols of CR and LF. In Linux, however, only LF ($) is the line break. You can complete the format conversion with the following command: $dos 2unix, $unix 2dos.
Cgywin command output is garbled in Chinese

Execute some commands (ipconfig, etc.) if there is a Chinese output, there will be a Chinese garbled problem.
You need to change the local language to ZH_CN in Options->text and the character set to GBK.

Color issues

LS color: Cygwin the output of the default LS is no color, and some mainstream releases such as Ubuntu are configured with a color output by default for the terminal, because the Cygwin uses Mintty.exe virtual terminal is color-supported, so you can edit ~/.BASHRC file, append:

[Plain] View plaincopy[-x/usr/bin/dircolors]&&eval "$ (dircolors-b)" Alias ls= ' ls--color=auto '

grep color: Similarly, grep does not have a color by default. Edit the ~/.BASHRC file, append:

[Plain] View Plaincopyalias grep= ' grep--color=auto '

Vim comment dark blue problem: In the default syntax coloring of vim, the comment is dark blue, which is not clearly visible under the black background. You can change it to a light blue color, edit ~/.VIMRC (not created), add:

[Plain] View Plaincopysyntax onhi Comment Ctermfg=blue

or set the background color to dark, edit the ~/.VIMRC file, and add:

[Plain] View plaincopysyntax on set Background=dark
Cywin input, display Chinese is not normal

Workaround: Modify the. Inputrc to dismiss the following lines of comment:

#set Meta-flag on #set convert-meta off#set Input-meta on#set Output-meta on
Invalid home/end key under command line

Add the following lines in the ~/.INPUTRC:

Vim ~/.inputrc "\e[3~":d Elete-char "\e[1~": Beginning-of-line "\e[4~": End-of-line "\e[h": Beginning-of-line "\e[F": End-of-line "\e[7~": Beginning-of-line "\e[8~": End-of-line "\eoh": Beginning-of-line "\eof": End-of-line
Resources

Cygwin User manual: Https://cygwin.com/cygwin-ug-net.html
Cygwin Installation Guide: https://cygwin.com/install.html
Cygwin faq:https://cygwin.com/faq.html

This article from "Fireworks Easy Cold" blog, reproduced please contact the author!

Cygwin's potholes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.