Index syndication
comment syndication

Archive for unix

Vim Split tips

I use vim a lot of the time, mostly with splits and diffs, so the following key mappings and functions really helped me with managing the split windows. Maybe they will help you too. (Thanks to the Vim tips wiki for these).

  • If you use vertical splits, this will help move left and right across the split. Put in your ~/.vimrc
    " Map multi window keys
    set wmw=0
    " CTRL-H move to left window
    nmap <c-h> <c-w>h<c-w><bar>
    " CTRL-L move to right window
    nmap <c-l> <c-w>l<c-w><bar>
    
  • When scrolling up and down a window, you can use zz to jump the current line to the middle of the window. If you want this on always ala Scroll locking, then you can use this function. It is a toggle option. use \zz to toggle it. Put in your ~/.vimrc
    " Map \zz to lock scroll to middle of window
    map <Leader>zz :let &scrolloff=999-&scrolloff<CR>
    
  • In a window split (of any sort) if you want to maximise to the current window, this will do it for you. When you press CTRL-W then o it will maximise the current view, then when pressed again will return your split arrangement! Put in your ~/.vimrc
    " Max/unmax splits
    nnoremap <C-W>O :call MaximizeToggle ()<CR>
    nnoremap <C-W>o :call MaximizeToggle ()<CR>
    nnoremap <C-W><C-O> :call MaximizeToggle ()<CR>
     
    function! MaximizeToggle()
      if exists("s:maximize_session")
        exec "source " . s:maximize_session
        call delete(s:maximize_session)
        unlet s:maximize_session
        let &hidden=s:maximize_hidden_save
        unlet s:maximize_hidden_save
      else
        let s:maximize_hidden_save = &hidden
        let s:maximize_session = tempname()
        set hidden
        exec "mksession! " . s:maximize_session
        only
      endif
    Endfunction
    

Always encrypt your ssh private key

Recently someone I know advised other IT people to generate their SSH keypair using the default options “using just enter to answer all the questions”. This means that the Private Key generated has no password against it (and is unencrypted).

In this case your private key is stored unprotected on your own computer, and anybody who gains access to that will be able to generate signatures (login to servers) as if they were you They will be able to log in to your server under your account.
I’ll reiterate that: This will allow ANYONE holding this file to access ANY server AS YOU where you have uploaded the public keys.

This means that in the case your laptop or computer is lost or stolen, your unix accounts are effectively compromised.

For this reason, your private key is recommended to be encrypted when it is stored on your local machine, using a pass phrase of your choice. To minimise this risk you should choose a strong pass phrase to be applied to the private key when generation occurs.

There are two ways to generate a key pair.

  1. If you are using openssh then generate the keypair under your unix login as follows:

    $ ssh-keygen -C "My development key 05 May 2008" -t rsa
    Generating public/private rsa key pair.
    Enter file in which to save the key (/home/lantrix/.ssh/id_rsa): 
    Enter passphrase (empty for no passphrase): **type in a strong password here**
    Enter same passphrase again: **retype in your strong password here**
    Your identification has been saved in /home/lantrix/.ssh/id_rsa.
    Your public key has been saved in /home/lantrix/.ssh/id_rsa.pub.
    The key fingerprint is:
    1a:aa:bb:44:09:38:ec:1d:1c:2d:27:c8:cc:dd:ee:ff My development key 05 May 2008
    $ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
    

    Then copy ~/.ssh/id_rsa the password protected and encrypted private key to a secure place on your client machine to use (placing it in your ~/.ssh/ folder - remember to set permissions to 600).

    If you want to use this openssh keypair with putty on a windows client, you will need to follow an extra step. Use PuttyGen menu to load your generated “id_rsa” file you transferred to your windows client.

  2. If you are only going to use putty to connect to UNIX servers it is better to generate the keypair in putty
    See the putty documentation for instructions on generating your key pair.
    Ensure you export the public keyfile to ~/.ssh/authorized_keys on each UNIX server you want to login (and chmod 600 on the file).

A Helpful tip

You can use putty to “cache” your key (to prevent constant retyping of your password when logging into servers) in a secure fashion using this component of putty.

Dont think of SSH keypairs as a means of easier logins. When used correctly it will in fact provide a more secure login; as your password is never passed over the network.

OpenBSD, postgresql and semaphore failures during initdb

Today I upgraded my postgresql database instance on OpenBSD. Did a pg_dumpall, removed the old packages and then added the new ones (latest version 8.1.9 for OpenBSD 4.0 - yes I’m behind).

During the initialisation of the new DB, I got the following error:

creating directory /var/postgresql/data/pg_tblspc ... ok
selecting default max_connections ... 10
selecting default shared_buffers ... 50
creating configuration files ... ok
creating template1 database in /var/postgresql/data/base/1 ... FATAL:  could not create semaphores: No space left on device
DETAIL:  Failed system call was semget(1, 17, 03600).

The PostgreSQL documentation talks about this extensively. However I don’t want to recompile my kernel away from default. What else can I do?

Read the rest of this entry »

Coral Content Distributed network

Have you ever had your website hit on a topic that people find conflicting? When the site gets viewed by the masses, you need to be prepared. If you have access to modify your web server .htaccess file then go and have a read of the Coral CDN Overview
For those interested in the techie bits, here is my .htaccess for news site flood protection, and to allow CDN to serve up all my site images - thus offloading from my puny connection the bandwidth burden for images.

<ifmodule mod_rewrite.c>
RewriteEngine On
#prevent slashdot effect
RewriteCond %{HTTP_USER_AGENT} !^CoralWebPrx
RewriteCond %{HTTP_REFERER} blogspot\.com [NC,OR]
RewriteCond %{HTTP_REFERER} reddit\.com [NC,OR]
RewriteCond %{HTTP_REFERER} digg\.com [NC,OR]
RewriteCond %{HTTP_REFERER} news\.slashdot\.org [NC,OR]
RewriteCond %{HTTP_REFERER} slashdot\.org
RewriteRule ^(.*)$ http://techdebug.com.nyud.net/$1 [R,L]
#Rewrite images to allow CDN to serve them
RewriteCond %{HTTP_USER_AGENT} !^CoralWebPrx
RewriteRule ^(.*)/(.*\.(gif|png|jpe?g))$ http://techdebug.com.nyud.net/$1/$2 [R,L]
#Wordpress rewites
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</ifmodule>

mod_rewrite is your friend.

Mailing attachments from the Solaris Shell

I needed a quick way to send some files from the command line when logged into a Solaris server via ssh.
This assumes the server is already configured to deliver your smtp mail. I also used mailx for the sending client.
Here is how I did it, for your geeky reference.

First write your message:
cat << EOF > /tmp/mailmsg
Hi this is a message
And this is the second line
EOF

Then populate your recipient list, comma delimited as per the mailx(1) man page:
cat << EOF > /tmp/mailrecipients
john.doe@nodomain.com.it,jack.black@someplace.co.za
EOF

  • Then the actual command that will send your mail
  • . You need to uuencode your binary attachments, and you can send as many as you need.
    (cat /tmp/mailmsg ; uuencode /path/to/file.txt file.txt ; uuencode /location/of/otherfile otherfile) | mailx -s 'Subject' -r myemail@some.place.mx `cat /tmp/mailrecipients`

    You need to specify each file name twice, once for source file to encode, and once for the encoded file name; as per the uuencode(1C) man page. If you are sending from some local account on the server, the -r switch allows you to specify an alternate return address for the recipients (in other words your normal email address).

    PS: watch for the quotes and backticks. Dont mix them up!

    Hope this helps you out someday.

    Next entries »