Using Cygwin and PuTTY to get a unix-like command line on Windows

October 11, 2010

I remember trying this a while ago and suffering a lot of pain, but it’s a lot slicker now. I have essentially ‘replaced’ the Windows 7 command line (and associated “terminal” application) with a much more agreeable alternative.

First you need the putty installer package, and install it:

http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

Then get PuTTY Tray, for clickable URLs:

http://haanstra.eu/putty/

Replace the PuTTY binary with the one from PuTTY Tray. Load it up, go to ‘Behaviour’ and change ‘Show tray icon’ to Never. Unless you really want that feature (seems pointless on Windows 7 to me, this bit was really for my benefit). Save this under default settings and make any other config changes as desired.

Get Cygwin:

http://www.cygwin.com/setup.exe

Cygwin leaves a few ‘standard’ tools out, probably because their functions are already performed by built-in Windows tools. This is worthless to me. Take all the defaults you can and add (at least) the following packages using the installer’s package selection tool:

  • ncurses
  • openssh
  • wget
  • make
  • gcc
  • file (in Utils)
  • procps
  • libncurses-devel
  • vim

Once done, run Cygwin from the start menu as an Administrator. This is required for the initial ssh server setup. To do this, find it in the start menu, right click and select the appropriate option from the context menu.

From the cygwin command line, run ssh-host-config. This will ask various questions and set up the ssh service for you. Go for the privilege seperation option. Make up the names, doesn’t matter what they are (probably). I think I said yes to pretty much everything (put ‘ntsec’ for the “value of CYGWIN” question).

Once done, run ssh-user-config, this sets up keys and permissions. I didn’t use a password here, but it depends on your security requirements as to whether you think you need one. You’ll have to enter the password in pageant every time you log in if you require one. Not a real issue, probably worth it in the long run.

Now do ‘net start sshd’, fire up PuTTY and create a profile for connecting to ‘localhost’. Give it a good name, we’re going to need to refer to it as whatever you call it. Test it to check you can log in ok. Pick a nice location for a simple batch file. Create one (use notepad or whatever, don’t forget to save as .bat). Put the following in:

start “starting localhost putty” “C:\Program Files\PuTTY\putty.exe” -load “2 localhost”

Please note this is wrapped by my stylesheet, there should be no line breaks in your batch file. You may need to alter the path to the putty binary. In this example my putty profile for logging in to the local cygwin openssh server is called “2 localhost”.

We still need to stop having to enter a password every time. Load PuTTYgen, press the Load button, change the file filter from PuTTY private key files (ppk) to All Files and navigate to C:\cygwin\home\<username>\.ssh – select the id_rsa file and enter your password if necessary. Press the ‘Save Private Key’ button and navigate to C:\Users\<username>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup and save the file there. Now it will be loaded when you log in to Windows. Find it in Windows Explorer and double click it. Now try your batch file again, and it should log straight in.

Now we just need to put it in the Windows 7 dock. This is where Windows is stupid, but it is doable still. Open the Start Menu, and search for ‘cmd’. Right click the ‘cmd.exe’ result and select ‘Pin to Taskbar’. Right click the new Taskbar icon, right click ‘Command Prompt’ in the resulting menu and select Properties. In the ‘Target’ field, add the following to what is already there:

/C C:\Users\Public\Documents\localputty.bat

This is the full location and name of the batch file we created earlier, in my case ‘localputty.bat’. You can try and change the icon and description if you want, although it didn’t work for me.

One final thing. Cygwin has an annoying habit of putting nothing but the current path in the PuTTY title bar, which isn’t helpful at all, unless you spend your whole computing time navigating Cygwin file trees and need to know where you are at all times.

In the /etc/profile file, find the section ‘Shell dependent settings’ and the line below this that says:

PS1=’\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$ ‘

Change this to:

PS1=’\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\] \$ ‘

in your ~/.bash_profile, uncomment the section regarding adding the user’s private bin to the PATH variable. Run ‘mkdir ~/bin’ and create a file in there called ‘wt’ or whatever (short for window title!).

In this file put the following:

echo -n “^[]2;${1}^G”

The ^[ and ^G are actually escape characters. In vi these are created by doing ctrl-v escape and ctrl-v ctrl-g respectively. Make this script executable and you can now easily change your PuTTY window titles. I like to alias my favourite terminal commands and wrap this command around them, for example (in my .bashrc):

alias irssi=’wt irssi; pkill irssi ; irssi; wt Wind’

where ‘Wind’ is my regular terminal title.

Hopefully there aren’t too many mistakes here. I guess I’ll find them when I go through this on my desktop at home.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.