Cygwin使用
Update: 2013-01-04
常用软件安装
- 安装Cygwin时,应安装上:wget, tar, gawk, bzip2, nc, xz, openssh
安装apt-cyg
-
修改x86/x86_64源、xz压缩包支持、sha512sum的apt-cyg:apt-cyg
#wget http://apt-cyg.googlecode.com/svn/trunk/apt-cyg wget http://xstarcd.github.io/wiki/files/shell/apt-cyg -O /usr/local/bin/apt-cyg chmod +x /usr/local/bin/apt-cyg apt-cyg --help cygcheck --help # 修改软件源 #apt-cyg --mirror http://mirrors.163.com/cygwin #apt-cyg --mirror http://mirrors.aliyun.com/cygwin apt-cyg --mirror https://mirrors.kernel.org/sourceware/cygwin/ # ftp://mirror.mcs.anl.gov/pub/cygwin # http://mirrors.163.com/cygwin # ftp://ftp.ges.redhat.com/private/releng/cygwin-1.8
-
201308后Cygwin版本变更,需修改apt-cyg
-
软件源路径变更
- 2013-08-08后Cygwin分为x86、x86_64两个安装包,源路径也改不同路径(x86、x86_64),因此apt-cyg下载setup.bz2和setup.ini时发生404错误。
- 在apt-cyg增加MPath来支持x86、x86_64版本。
- 添加xz压缩包支持:修改apt-cyg根据包文件扩展名选择binzip2或xz来进行解压。
-
软件源路径变更
-
cygwin 2.0.0中改用sha512sum替代md5校验,执行报
cygwin64 MD5 sum did not match, exiting
错误- 修改apt-cyg,将md5sum 换成 sha512sum
安装常用工具
# apt-cyg install man cygwin-doc apt-cyg install vim screen tmux wget subversion openssh pwgen gzip bzip3 curl rsync bash-completion \ lftp nc tree p7zip connect-proxy util-linux bind-utils inetutils
安装python环境
apt-cyg install python python-crypto
-
安装easy_install
-
参考:http://atbrox.com/2009/09/21/how-to-get-pipvirtualenvfabric-working-on-cygwin/
#wget http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c11-py2.6.egg #sh setuptools-0.6c11-py2.6.egg apt-cyg install python-setuptools easy_install pip pip install pssh # ansible apt-cyg python-crypto pip install ansible #pip install moin
-
参考:http://atbrox.com/2009/09/21/how-to-get-pipvirtualenvfabric-working-on-cygwin/
常用工具设置
Cygwin终端
-
~/.minttyrc
BoldAsFont=no Font=YaHei Consolas hybrid FontHeight=12 #BackgroundColour=0,43,53 BackgroundColour=11,22,33 ForegroundColour=191,191,191 FontSmoothing=full # 关闭CTRL_+/-/0放大缩小功能,避免与vim窗口调整冲突 ZoomShortcuts=no
-
颜色、字体选择参数
背景颜色:(RGB)0,40,51;或11,22,33(就是这个Wiki页面的背景色) 字体颜色:(RGB)31,148,150(较暗);或191,191,191 目录颜色:(RGB)255,128,128 压缩文件颜色:(RGB)128,128,0 字体:Consolas,Courier New,Fixedsys,YaHei Consolas hybrid
- YaHei Consolas hybrid.1.12字体下载:
bashrc
-
~/.bashrc
alias grep='grep --color' # show differences in colour alias egrep='egrep --color=auto' # show differences in colour alias fgrep='fgrep --color=auto' # show differences in colour alias ls='ls -hF --color=tty' # classify files in colour alias ll='ls -la' # long list alias la='ls -A' # all but . and .. alias l='ls -CF' # # 关闭ControlMaster auto,避免ssh连接时因不支持unix-socket报如下错误问题: # mm_receive_fd: no message header # process_mux_new_session: failed to receive fd 0 from slave alias ssh="ssh -S none" # 将常用Windows命令输出转换为utf8,解决Windows自有命令显示乱码问题 _runiconv () { _cmd=$1;shift;$_cmd $* 2>&1|iconv -f gbk -t utf8; } alias ping='_runiconv ping' alias ifconfig='_runiconv ipconfig' alias netstat='_runiconv netstat' alias tracert='_runiconv tracert' alias route='_runiconv route'
-
~/.bash_profile
PS1="[\w]$" #彩色的PS1会造成命令编辑时光标定位错误 #PS1="\e[0;32m[\u@\h \W]\$ \e[m"
screen/tmux设置
# # ~/.screenrc # # 启动时不显示欢迎屏幕 startup_message off # 定义screen的功能键为Ctrl-A。向终端输入Ctrl-A时应按 Ctrl-A a。 #escape ^Aa # 屏幕缓冲区 1024 行。 defscrollback 1024 # 显示窗口列表 caption always "%-w%{= BW}%50>%n %t%{-}%+w%<" # 按 Ctrl-A w 或 Ctrl-A Ctrl-W 显示窗口列表 bind w windowlist -b #bind ^w windowlist -b #在使用screen过程中,经常会遇到“闪屏”的问题,这是可以关掉的。 #快速关掉:也就是先输入CTRL + a,再输入CTRL + g。 #永久关闭:修改/etc/screenrc 加入 vbell off
-
~/.tmux.conf
# ~/.tmux.conf # prefix key (Ctrl+a) set -g prefix ^a #set -g prefix C-a unbind ^b #unbind C-b bind a send-prefix # prefix key (Ctrl+a) # split window unbind '"' unbind % bind - splitw -v # vertical split (prefix -) bind | splitw -h # horizontal split (prefix |) # select pane bind k selectp -U # above (prefix k) bind-key k select-pane -U bind j selectp -D # below (prefix j) bind h selectp -L # left (prefix h) bind l selectp -R # right (prefix l) # copy mode # enter copy mode (prefix Escape) bind Escape copy-mode # paste buffer (prefix Ctrl+p) bind ^p pasteb #bind-key -T copy-mode C-Space send-keys -X begin-selection # reload config (prefix r) bind r source ~/.tmux.conf \; display "Configuration reloaded!" #-- statusbar --# set -g status-interval 1 set -g status-keys vi setw -g mode-keys vi setw -g automatic-rename off #set-option -g status-style bg=cyan # statusbar #set-window-option -g window-status-current-style fg=colour166,bg=#c0c0c0 set-window-option -g window-status-current-style fg=colour166,bg=colour222
~/.vimrc
-
vim没有语法高亮,需加载基本设置
cd /usr/share/vim && cp vim74/vimrc_example.vim vimrc
- .vimrc设置
详见:vimrc
" ~/.vimrc set mouse=c set nobackup " set paste set fileformats=unix,dos,mac set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1 set encoding=utf-8 " set termencoding=cp936 let &termencoding = &encoding language messages en_US.UTF-8 "language messages zh_CN.UTF-8 " colo murphy " tab set tabstop=4 set cindent shiftwidth=4 set autoindent shiftwidth=4 " TAB -> Space set expandtab
~/.ssh/config
#ssh_config #~/.ssh/config IdentitiesOnly yes ServerAliveInterval 60 GSSAPIAuthentication no Host m* Port 22 User root ControlMaster auto ControlPath ~/.ssh/master-%r@%h:%p #ProxyCommand connect-proxy -S 127.0.0.1:2223 %h %p Host mNagios Hostname 172.16.16.150
其他设置
SSH Server
- OS修改环境变量:path增加c:/cygwin/bin
- OS增加环境变量:CYGWIN=ntsec tty
- Cygwin安裝必要的套件:OpenSSH,cygrunsrv
-
Change permissions and ownership on some files:
# 以下代码没测试过,慎用 cd /; chmod -R og-w . chmod og+w /tmp touch /var/log/sshd.log chown system:system /var/log/sshd.log /var/empty /etc/ssh_h* chmod 755 /var/empty
-
Cygwin設定OpenSSH:
ssh-host-config
Should privilege separation be used? (yes/no) :yes Should this script create a local user 'sshd' on this machine? (yes/no) :yes Do you want to install sshd as service? (yes/no) :yes Default is "ntsec" . CYGWIN=ntsec tty
-
启动OpenSSH:net start sshd
cygrunsrv -S sshd
- 停止OpenSSH:net stop sshd
crontab
-
在Windows上安装系统服务 cron,开机即运行。
cygrunsrv -I cron -p /usr/sbin/cron -a -D
-
启动服务
cygrunsrv -S cron
nontsec
这在 Cygwin 下创建的文件,其他管理员不可写,是所谓的 ntsec 在起作用。通过 NTFS 的权限设置,模拟 UNIX 的权限。可以通过 CYGWIN 环境变量修改。如:
export CYGWIN="nontsec nosmbntsec"
中文乱码设置
**若存在中文问题才进行以下设置。**
- Rethat Cygwin安装后目录中文即正常,以下设置不一定需要。
- git log命令中出现乱码,可以通过以下设置解决
-
~/.bashrc
export SVN_EDITOR=vim # Redhat Cygwin中未做以下设置,中文正常 alias ls='ls --color --show-control-chars' # git log修改这个设置后可显示中文commit log #export LESSCHARSET=latin1 export LESSCHARSET=UTF-8 alias less='/bin/less -r' #export LC_ALL=zh_CN.UTF-8 #export LC_CTYPE=zh_CN.UTF-8 #export LANG=zh_CN.UTF-8 #export OUTPUT_CHARSET="GBK" #stty cs8 -istrip #stty pass8 export CYGWIN="nontsec nosmbntsec"
-
~/.inputrc
# Allow 8-bit input/output set meta-flag on set convert-meta off set input-meta on set output-meta on
技巧收集
-
Howto get version info?
cygcheck -c cygwin
-
查看系统信息、软件包
cygcheck -h #帮助 cygcheck -s #sysinfo cygcheck -l #已安装包列表 cygcheck -c #列出并校验已安装的包 cygcheck -p vim #从cygwin.com源查询包
-
给curl安装CA Certificates
curl http://curl.haxx.se/ca/cacert.pem > /usr/ssl/certs/ca-bundle.crt
-
同步Windows用户和组
mkpasswd -l > /etc/passwd mkgroup -l > /etc/group # 如果有Domain的话,需要加上-d domainname
-
设置Cygwin/mintty字体和大小
- 界面中设置:右键菜单->Options->Text,然后选择你喜欢的字体和大小。
-
Windows中有些字体不会列出,可在配置文件:
~/.minttyrc
中直接指定:BoldAsFont=no #Font=Courier New Font=YaHei Consolas hybrid FontHeight=12
-
给Cygwin添加more/col/whereis等命令:安装util-linux包:
apt-cyg install util-linux
-
给Cygwin安装dig、iconv、telnet/ftp:
apt-cyg install bind-utils libiconv inetutils
-
检查系统中已设置好DNS:
ipconfig /all
-
检查系统中已设置好DNS:
-
得到Windows格式的路径名?
cygpath -d -m "`pwd`"
-
用Explorer打开当前目录:open.sh
#!/bin/sh #/usr/local/bin/open d=$1;[ -z "$d" ] && d=$(pwd) d=$(cd "$d" > /dev/null 2>&1;pwd) Explorer /e,"$(cygpath -d -w $d)"
-
Cygwin error:
-bash: fork: retry: Resource temporarily unavailable
- http://stackoverflow.com/questions/9300722/cygwin-error-bash-fork-retry-resource-temporarily-unavailable
- http://cygwin.wikia.com/wiki/Rebaseall
-
错误现象
-bash-4.1$ wc m1.txt 3 [main] bash 2216 child_info_fork::abort: data segment start: parent(0x26D000) != child(0x38D000) -bash: fork: retry: Resource temporarily unavailable 2013930 4027950 74968256 m1.txt
-
处理办法
rkosegi's Answer seems to be working for everyone (myself included). But, just so you don't have to follow the daisy chain of links: (assuming Cygwin is installed at C:\cygwin): 1.Open Task Manager and close any processes that look to be Cygwin related. 2.Open C:\Cygwin\bin in Windows Explorer 3.Verify that dash.exe, ash.exe, rebase.exe, and rebaseall exist in this folder - If any of them are missing, re-run Cygwin setup and select the dash, ash, and rebase packages 4.right-click your C:\Cygwin folder, uncheck Read-only (if its checked), and press OK. - When an error about not being able to switch some files comes up, select "Ignore All". Wait for this process to complete. 5.Browse to C:\Cygwin\bin in Windows Explorer 6.Right click dash.exe and click "Run as Administrator". A command Prompt should appear with nothing but a $ 7.Type /usr/bin/rebaseall -v, hit enter, and wait for the process to complete. - If you get errors about Cygwin processes running, try Step 1 again. If that still doesn't work, Restart your computer into safe mode and try these steps again. 8.Try opening Cygwin again. This process worked for me. I hope it works for you guys too. Source: http://cygwin.wikia.com/wiki/Rebaseall
SSH Agent on Cygwin
ssh-agent 启动后,会设置 SSH_AUTH_SOCK 和 SSH_AGENT_PID 两个环境变量。
ssh 通过检测 SSH_AUTH_SOCK 和 SSH_AGENT_PID 两个环境变量判断是否存在 ssh-agent 后台进程,如果存在则使用 ssh-agent 完成自动认证,否则提示用户输入密码。
ssh-agent启动输出内容为环境参数设置脚本,需以eval或source方式执行来加入环境参数。
ssh-agent.exe -s SSH_AUTH_SOCK=/tmp/ssh-GYP4AmeZ5LP6/agent.1272; export SSH_AUTH_SOCK; SSH_AGENT_PID=4536; export SSH_AGENT_PID; echo Agent pid 4536;
- 在~/.bash_profile中添加自动启动、停止ssh-agent代码
实现如下功能:
- 打开Cygwin时若没有ssh-agent进程,则自动启动,若已有ssh-agent进程,则不再重复启动;
- 关闭Cygwin时,检查若是最后一个Cygwin窗口则自动关闭ssh-agent,若还有其它Cygwin窗口则不关闭。
- 关闭所有Cygwin窗口,之前保存的key将不再有效。
-
多个窗口均共用同ssh-agent。
# Add to ~/.bash_profile # start_stop_ssh_agent SSH_ENV=$HOME/.ssh/ssh-agent-env function stop_ssh_agent { if [ $(ps -ef|grep mintty$|wc -l) -lt 2 -a -n "$SSH_AGENT_PID" ];then ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null && kill $SSH_AGENT_PID fi } function start_ssh_agent { if [ -f "${SSH_ENV}" ]; then . ${SSH_ENV} > /dev/null ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null && return 0 fi echo "Initialising new SSH agent..." /usr/bin/ssh-agent -s | sed 's/^echo/#echo/' > ${SSH_ENV} \ && echo succeeded \ && chmod 600 ${SSH_ENV} \ && . ${SSH_ENV} > /dev/null #&& /usr/bin/ssh-add \ } start_ssh_agent #trap "[ \$(ps -ef|grep mintty\$|wc -l) -lt 2 ] && kill $SSH_AGENT_PID" 0 trap "stop_ssh_agent" 0
-
MAC OS版本
# start_stop_ssh_agent SSH_ENV=$HOME/.ssh/ssh-agent-env function stop_ssh_agent { U=$(whoami) if [ $(ps aux|grep "$U.*-bash\$"|grep -v grep|wc -l) -lt 3 -a -n "$SSH_AGENT_PID" ];then ps aux | grep ${SSH_AGENT_PID} | grep ssh-agent > /dev/null && kill $SSH_AGENT_PID fi } function start_ssh_agent { if [ -f "${SSH_ENV}" ]; then . ${SSH_ENV} > /dev/null ps aux | grep ${SSH_AGENT_PID} | grep ssh-agent > /dev/null && return 0 fi echo "Initialising new SSH agent..." /usr/bin/ssh-agent -s | sed 's/^echo/#echo/' > ${SSH_ENV} \ && echo succeeded \ && chmod 600 ${SSH_ENV} \ && . ${SSH_ENV} > /dev/null #&& /usr/bin/ssh-add \ } start_ssh_agent trap "stop_ssh_agent" 0
- ssh-add
ssh-add的作用是把指定的私钥添加到 ssh-agent 所管理的一个 session 当中。
ssh-agent 是一个用于存储私钥的临时性的 session 服务,也就是说当你重启之后,ssh-agent 服务也就重置了。
ssh-add ~/.ssh/id_rsa [enter your password]
-
keychain
-
http://www.funtoo.org/Keychain
sdesc: "Key manager for OpenSSH" ldesc: "Keychain is an OpenSSH key manager, typically run from ~/.bash_profile. When keychain is run, it checks for a running ssh-agent, otherwise it starts one. It saves the ssh-agent environment variables to ~/.keychain/$HOSTNAME-sh, so that subsequent logins and non-interactive shells such as cron jobs can source the file and make passwordless ssh connections. In addition, when keychain runs, it verifies that the key files specified on the command-line are known to ssh-agent, otherwise it loads them, prompting you for a password if necessary"
-
~/.bash_profile:
eval `keychain --eval --agents ssh id_dsa`
-
http://www.funtoo.org/Keychain
Windows Terminal
- 在 Windows Terminal 中使用 Cygwin 命令行或 Git Bash:https://leo3418.github.io/zh/2020/05/24/cygwin-git-bash-in-wt.html
- 安装Windows Terminal:在Microsoft Store搜索“Terminal”安装(不要安装Preview版本)。
- Windows 终端提示与技巧: https://learn.microsoft.com/zh-cn/windows/terminal/tips-and-tricks
- Windows Terminal配置文件说明:https://learn.microsoft.com/zh-cn/windows/terminal/customize-settings/startup#default-terminal-application
-
在Windows Terminal中设置Cygwin:
-
设置->添加新配置文件:
-
命令行方式一:
C:\cygwin64\bin\bash.exe -i -l
-
命令行方式二:
C:\cygwin64\Cygwin.bat
-
bash在登录状态才会加载profile、bashrc,需要使用
-i -l
参数,启动命令行:
-
命令行方式一:
- 删除crtl+v、ctrl+c热键设置:设置->操作中删除(终端中需要使用这两个组合键)。
-
开启鼠标右键菜单(不开启右键直接为粘贴)
-
按住Shift键单击下拉菜单中的设置,会用编辑器打开settings.json文件,在profiles->deflaults中添加
"experimental.rightClickContextMenu": true
:... "profiles": { "defaults": { "experimental.rightClickContextMenu": true, }, ... - 按住 Alt 并单击下拉菜单中的“设置”按钮,查看默认设置
-
按住Shift键单击下拉菜单中的设置,会用编辑器打开settings.json文件,在profiles->deflaults中添加
-
设置->添加新配置文件:
conEmu
- https://conemu.github.io/
- Cygwin系列(十):折腾终端1:https://zhuanlan.zhihu.com/p/99963508
ConEmu是一款优秀的开源终端,项目地址https://conemu.github.io
它是混合型的,首要支持Windows Console功能,可看作是conhost的增强版本,同时也部分地支持pty,具备ANSI转义序列直接解析能力,总体上是conhost+Mintty的全能选手。
主要功能特性包括:
多标签页、多标签页、多标签页,重要事情说3遍,并且支持多种布局方式; 不同标签页独立配置Task,也就是运行不同的命令行程序,特别是shell,Task支持名字分组和分配快捷键; 界面现代化,有标签页、快捷菜单、控制台区、状态栏、滚动条; 支持连接到多种类型命令行程序环境,Cygwin、msys、WSL都不在话下; 图形化配置方式,每一部分都支持个性化配置,所有配置项保存到文件ConEmu.xml; 支持集成到其他程序界面中,比如文件管理器Explorer; 支持字符加粗、斜体、下划线、闪烁、前后景色等风格; 全面支持Unicode,支持ANSI X3.64标准和xterm256色; 附带字符界面的文本管理器Far Manager及其插件; 能运行在Windows 2000及以上版本的Windows系统,开箱即用。
脚本添加ssh主机名自动完成
-
/etc/bash_completion.d/ssh
_ssh() { local cur prev opts COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD-1]}" opts=$(grep '^Host' ~/.ssh/config ~/.ssh/config.d/* 2>/dev/null | grep -v '[?*]' | cut -d ' ' -f 2-) COMPREPLY=( $(compgen -W "$opts" -- ${cur}) ) return 0 } complete -F _ssh ssh
. /etc/bash_completion.d/ssh