linux snippets

Enable your favorite editor in production environment.

Tagged favorite editor, sshfs, ssh, production, linux, mounting remote directory  Languages bash

Anyone who's ever set up or maintained a larger production environment will love this "snippet". Production environments seldom contain your favorite editor. In fact, most times you are stuck with vi, which is fine if that happens to be your flavor. For most people vi just doesn't fit. The only requirement for this trick is to have SSH access into the production environment.

Firstly install sshfs on your development computer.

sudo apt-get install sshfs

It should work for both major Debian derivates (Debian and various versions of Ubuntu). I know from experience that it works for Etch stable. If you don't use a package manager then go to sshfs homepage and follow instructions from there.

Create a mountpoint for the production filesystem.

sudo mkdir -p /mnt/production-xyz/bea92

Mount it with sshfs using a syntax similar to scp.

sshfs [email protected]_server:/opt/bea92 /mnt/production-xyz/bea92

Fire up your favorite editor and start setting up/maintaining the production environment with a smile on your face :) Once you're done unmount it using the command below.

fusermount -u /mnt/production-xyz/bea92

Changing the default terminal emulator the Debian way.

Tagged default terminal, debian, ubuntu, x11, linux  Languages bash

Use this to change the terminal emulator that comes with the default installation of Debian (and Ubuntu).

sudo update-alternatives --config x-terminal-emulator

Choose your emulator from the list and hit enter.

Automatically giving predetermined answers to a program.

Tagged script automation, bash, linux  Languages bash

Sometimes you want to automate the running of a script or program, but the darn thing keeps asking questions and expects interactive answers. Shell scripting makes it easy to give the answers automatically, using a stream redirection from a file.

./qurious_program < predetermined_answers.txt

Appending after a pattern from a file in sed

Tagged sed, linux, bash  Languages bash

Useful when you want to append the contents of a whole file into something you are sed'ing. Be careful if you use -i, it will replace the working file.

sed -i "/attr_accessor :config/r ../patch_for_application.rb.tmpl" app/controllers/application.rb

Custom kernel for Debian Etch

Tagged linux, debian, custom kernel, kustomoitu kerneli, kustomiserad kernel, deb  Languages bash

Sometimes you just need a kernel that is newer than the package manager has to offer. For me it was the bug fixes for the driver of the sound card in x61s. Here's how to do it "the debian way".

wget http://kernel.org/pub/linux/kernel/v2.6/linux-2.6.23.1.tar.bz2
tar xjvf linux-2.6.23.1.tar.bz2 -C /usr/src
rm /usr/src/linux && ln -s /usr/src/linux-2.6.23.1 /usr/src/linux
cd /usr/src/linux
zcat /proc/config.gz > .config
make xconfig

Now turn on and configure the new features you are looking for in the new kernel, save the configuration and finally quit.

make-kpkg clean
fakeroot make-kpkg --initrd --append-to-version=-custom-13.10.2007 kernel_image kernel_headers
cd ..
sudo dpkg -i linux-image-2.6.23.1-custom-13.10.2007_2.6.23.1-custom-13.10.2007-10.00.Custom_amd64.deb
sudo dpkg -i linux-headers-2.6.23.1-custom-13.10.2007_2.6.23.1-custom-13.10.2007-10.00.Custom_amd64.deb

The --append-to-version parameter appends the given value into the kernel signature, so that it is easily recognized. If you compile a kernel with the exactly same name as a previous kernel you have then you must move away the directory /lib/modules/your-kernel-name-here.

3G connection with Linux and Option Globetrotter GT Max "7.2 ready"

Tagged 3g, saunalahti mobiililaajakaista, saunalahti mobilbredband, hsdpa, umts, grps, modem, modeemi, pcmcia, linux, gsm, laptop, kannettava tietokone, bã¤rbar dator, saunalahti mobile broadband  Languages bash

The installation instructions for the software are Debian specific. The configuration files are specific to Saunalahti Mobiililaajakaista (Saunalahti mobile broadband) in Finland.

Installation

Install wvdial and pppd

apt-get install wvdial pppd

Create and edit /etc/ppp/peers/wvdial

usepeerdns
plugin passwordfd.so
noauth
name wvdial
defaultroute
replacedefaultroute
noipdefault
novj
nobsdcomp

Edit /etc/wvdial.conf

[Dialer Defaults]
Modem = /dev/ttyUSB0
Baud = 115200
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2
Init3 = AT+CFUN=1;+CGDCONT=1,"IP","internet.saunalahti"
Area Code =
Phone = *99#
Username = saunalahti
Password = saunalahti
Ask Password = 0
Dial Command = ATD
FlowControl=NOFLOW
Stupid Mode = 1
Compuserve = 0
Force Address =
Idle Seconds = 3000
DialMessage1 =
DialMessage2 =
ISDN = 0
Auto DNS = 1

Run wvdial

sudo wvdial

You should now be connected. If you aren't then check for potential solutions below.

Troubleshooting

Kernel modules

Check that modules USB_SERIAL_OPTION and USB_SERIAL are built as modules or into the kernel.

Conflicting drivers

This is identified by a rapidly flashing purple light in your modem. You must disable and blacklist the usb_storage module, because it interferes with the built in modem.

Disable usb_storage module

rmmod usb_storage
cat /etc/modprobe.d/blacklist | grep "blacklist usb_storage" || echo "blacklist usb_storage" >> /etc/modprobe.d/blacklist
pccardctl eject
pccardctl insert

Device in wrong state

If you completely fail to get a response from the modem then you probably need to run the usb_modeswitch utility. You can check if this is the problem using lsusb. Below are the correct and incorrect values.

Correct value of lsusb (no need to run usb_modeswitch). Notice the row containing "0af0:6701 Option".

[email protected]:~$ lsusb
Bus 008 Device 003: ID 0af0:6701 Option 
Bus 008 Device 001: ID 0000:0000  
Bus 007 Device 001: ID 0000:0000  
Bus 006 Device 001: ID 0000:0000  
Bus 001 Device 001: ID 0000:0000  
Bus 005 Device 001: ID 0000:0000  
Bus 002 Device 003: ID 0483:2016 SGS Thomson Microelectronics Fingerprint Reader
Bus 002 Device 002: ID 0a5c:2110 Broadcom Corp. 
Bus 002 Device 001: ID 0000:0000  
Bus 004 Device 001: ID 0000:0000  
Bus 003 Device 001: ID 0000:0000

Incorrect value of lsusb (you should run usb_modeswitch). Notice the row containing "05c6:1000 Qualcomm, Inc.".

[email protected]:~$ lsusb
Bus 008 Device 002: ID 05c6:1000 Qualcomm, Inc. 
Bus 008 Device 001: ID 0000:0000  
Bus 007 Device 001: ID 0000:0000  
Bus 006 Device 001: ID 0000:0000  
Bus 001 Device 001: ID 0000:0000  
Bus 005 Device 001: ID 0000:0000  
Bus 002 Device 003: ID 0483:2016 SGS Thomson Microelectronics Fingerprint Reader
Bus 002 Device 002: ID 0a5c:2110 Broadcom Corp. 
Bus 002 Device 001: ID 0000:0000  
Bus 004 Device 001: ID 0000:0000  
Bus 003 Device 001: ID 0000:0000

Compile usb_modeswitch

wget "http://frederick-reid.com/usb_modeswitch/usb_modeswitch-0.9.1beta.tar.bz2"
tar xjvf usb_modeswitch-0.9.1beta.tar.bz2 -C /usr/src
cd /usr/src/usb_modeswitch-0.9.1beta
gcc -l usb -o usb_modeswitch usb_modeswitch.c
mv -i usb_modeswitch /usr/bin

Create /etc/usb_modeswitch.conf with the following contents (NOTE: if you need to revert back then swap the values of "Default" and "Target").

DefaultVendor=  0x05c6
DefaultProduct= 0x1000

TargetVendor=   0x0af0
TargetProduct=  0x6600

MessageEndpoint=0x05
MessageContent="55534243706ede860000000000000601000000000000000000000000000000"

Finally run usb_modeswitch

usb_modeswitch

PPPD dying

If you start connecting, but lose it with pppd dying (like below), then re-check your /etc/wvdial.conf file. Your card may need a pin before it can be used. Or, as in my case, I had a typo where I had written "internet.saunalahti" instead of "saunalahti.internet" :)

[email protected]:~$ sudo wvdial
--> WvDial: Internet dialer version 1.56
--> Cannot get information for serial port.
--> Initializing modem.
--> Sending: ATZ
ATZ
OK
--> Sending: ATQ0 V1 E1 S0=0 &C1 &D2
ATQ0 V1 E1 S0=0 &C1 &D2
OK
--> Sending: AT+CFUN=1;+CGDCONT=1,"IP","saunalahti.internet"
AT+CFUN=1;+CGDCONT=1,"IP","saunalahti.internet"
OK
--> Modem initialized.
--> Idle Seconds = 3000, disabling automatic reconnect.
--> Sending: ATD*99#
--> Waiting for carrier.
ATD*99#
CONNECT 3600000
--> Carrier detected.  Starting PPP immediately.
--> Starting pppd at Sat Oct 20 21:49:26 2007
--> Pid of pppd: 6010
--> Using interface ppp0
--> pppd: �+
--> pppd: �+
--> pppd: �+
--> pppd: �+
--> pppd: �+
--> pppd: �+
--> Disconnecting at Sat Oct 20 21:49:28 2007
--> The PPP daemon has died: A modem hung up the phone (exit code = 16)
--> man pppd explains pppd error codes in more detail.
--> Try again and look into /var/log/messages and the wvdial and pppd man pages for

No device found

If your /dev/ttyUSBn devices don't exist you might need to create them.

Create ttyUSBn devices

mknod /dev/ttyUSB0 c 188 0
mknod /dev/ttyUSB1 c 188 1
mknod /dev/ttyUSB2 c 188 2
chmod g+w /dev/ttyUSB?
chown root.dialout /dev/ttyUSB?

Permission errors

If you get permission errors then you aren't running wvdial with leveraged privileges. Adding yourself to groups dialout and dip will get you a bit further, but the initial connection still needs to be run as root as files /etc/ppp/chap-secrets and /etc/ppp/pap-secrets are only writable to root. There is a guide for gentoo for setting up ppp access as non-root, but I have yet to try it myself.

Add yourself to groups dialup and dip

usermod -a -G dialout,dip marko

A successful connection

This is what a successful connection will look like.

x61s:# sudo wvdial
--> WvDial: Internet dialer version 1.56
--> Cannot get information for serial port.
--> Initializing modem.
--> Sending: ATZ
ATZ
OK
--> Sending: ATQ0 V1 E1 S0=0 &C1 &D2
ATQ0 V1 E1 S0=0 &C1 &D2
OK
--> Sending: AT+CFUN=1;+CGDCONT=1,"IP","internet.saunalahti"
AT+CFUN=1;+CGDCONT=1,"IP","internet.saunalahti"
OK
--> Modem initialized.
--> Idle Seconds = 3000, disabling automatic reconnect.
--> Sending: ATD*99#
--> Waiting for carrier.
ATD*99#
CONNECT 3600000
--> Carrier detected.  Starting PPP immediately.
--> Starting pppd at Sat Oct 20 22:13:05 2007
--> Pid of pppd: 9174
--> Using interface ppp0
--> pppd: �+
--> pppd: �+
--> pppd: �+
--> pppd: �+
--> pppd: �+
--> pppd: �+
--> pppd: �+
--> pppd: �+
--> pppd: �+
--> local  IP address 8x.7x.2xx.6x
--> pppd: �+
--> remote IP address 10.xx.xx.xx
--> pppd: �+
--> primary   DNS address 19x.19x.5x.10x
--> pppd: �+
--> secondary DNS address 19x.7x.0.4x
--> pppd: �+
rm --> pppd: �+
--> Connect time 44.9 minutes.

Check if a file or directory exists with bash

Tagged nginx, daemon, bash, linux, debian  Languages bash

This script tests if nginx exists and is executable. The script prints a warning and exits, if nginx doesn't exists or isn't executable:

DAEMON=/usr/local/sbin/nbinx
if [ ! -x $DAEMON ]
then
   echo "Couldn't find $DAEMON. Please set path to DAEMON."
   exit 0
fi

See man test for more information on how to use the test command.

Getting WLAN to work on a Fujitsu Amilo laptop is difficult...

Tagged fujitsu, linux, wlan, amilo, wpa-supplicant  Languages 

I fought and won over WLAN drivers and Linux, but later switched to using a Mac, because driver support for WLAN, printers and other hardware is non-existent Linux:

apt-get install wpasupplicant

http://ubuntuforums.org/showthread.php?t=313914

vim /etc/wpa_supplicant.conf

ctrl_interface=/var/run/wpa_supplicant
ctrl_interface_group=0
eapol_version=1
ap_scan=2
fast_reauth=1


network={
    ssid="...yer-ssid..."
    scan_ssid=1
    proto=WPA
    key_mgmt=WPA-PSK
    psk="...yer-pass..."
    priority=5
    pairwise=TKIP
}

vim /etc/network/interfaces

auto lo
iface lo inet loopback

auto eth1
iface eth1 inet dhcp
   wpa-driver wext
   wpa-conf /etc/wpa_supplicant.conf

auto wlan0
iface wlan0 inet dhcp

iface eth0 inet dhcp

auto eth0

vim /etc/init.d/wpasupplicant

#!/bin/sh

PATH=/usr/sbin:/usr/bin:/sbin:/bin

case "$1" in
        start|restart|force-reload)
                wpa_supplicant -Dwext -iwlan0 -c/etc/wpa_supplicant.conf -dd -B
                ;;
        stop)
                killall -9 wpa_supplicant
                ;;
        *)
                echo "Usage: $0 {start|stop|restart|force-reload}" >&2
                exit 3
                ;;
esac

exit 0

----------------

chmod +x ...
cd /etc/rcS.d
ln -s ../init.d/wpasupplicant S40iwpa


-------------

auto lo
iface lo inet loopback

auto eth1
iface eth1 inet dhcp

auto wlan0
iface wlan0 inet dhcp

nginx startup script for Debian

Tagged nginx, start, script, linux, debian  Languages bash
sudo vim /etc/init.d/nginx

Paste in the following (remember to run 'set :paste' in VIM when pasting):

#! /bin/sh
##
# nginx start script
##

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/nginx
NAME=nginx
DESC=nginx

if [ ! -x $DAEMON ]
then
   echo "Couldn't find $DAEMON. Please set path to DAEMON."
   exit 0
fi


# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
    . /etc/default/nginx
fi

set -e

case "$1" in
  start)
    echo -n "Starting $DESC: "
    start-stop-daemon --start --pidfile /var/run/$NAME.pid \
        --exec $DAEMON -- $DAEMON_OPTS
    echo "$NAME."
    ;;
  stop)
    echo -n "Stopping $DESC: "
    start-stop-daemon --stop --pidfile /var/run/$NAME.pid \
        --exec $DAEMON
    echo "$NAME."
    ;;
  restart|force-reload)
    echo -n "Restarting $DESC: "
    start-stop-daemon --stop --pidfile \
        /var/run/$NAME.pid --exec $DAEMON
    sleep 1
    start-stop-daemon --start --pidfile \
        /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
    echo "$NAME."
    ;;
  reload)
      echo -n "Reloading $DESC configuration: "
      start-stop-daemon --stop --signal HUP --pidfile /var/run/$NAME.pid \
          --exec $DAEMON
      echo "$NAME."
      ;;
  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0

Now make the script executable with this command:

sudo chmod 755 /etc/init.d/nginx

Lastly, run this command to make the script run when the server starts and stops:

sudo /usr/sbin/update-rc.d -f nginx defaults

How to install the stemmer4r gem on Mac OS X and Linux

Tagged stemming, stemmer4r, install, osx, linux, gem  Languages ruby

The stemmer4r gem is fubar. Warning draft snippet...

# gem install stemmer4r
Bulk updating Gem source index for: http://gems.rubyforge.org
Building native extensions.  This could take a while...
ERROR:  While executing gem ... (Gem::Installer::ExtensionBuildError)
    ERROR: Failed to build gem native extension.

ruby extconf.rb install stemmer4r

Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6 for inspection.
Results logged to /usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6/ext/stemmer4r/gem_make.out


1. Change path of Ruby executable

cd /usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6/ext/stemmer4r/
vim extconf.rb

#!/usr/bin/ruby -w

to

#ruby -w

2. Compile libstemmer_c

cd /usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6/ext/stemmer4r/libstemmer/
make

3. Compile stemmer4r

cd /usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6/ext/stemmer4r/

Change path:
/usr/local/ruby/lib/ruby/1.8/i686-linux/
To:
/usr/lib/ruby/1.8/x86_64-linux/

Or wherever you have it installed

ruby extconf.rb


4. Build stemmer4r gem


gem build stemmer4r.gemspec

gem install stemmer4r-0.6.gem


Problems

gcc -shared -rdynamic -Wl,-export-dynamic   -L"/usr/lib" -o stemmer4r.so stemmer4r.o libstemmer_c/libstemmer.o  -lruby1.8  -lpthread -ldl -lcrypt -lm   -lc
/usr/bin/ld: libstemmer_c/libstemmer.o(libstemmer.o): relocation R_X86_64_32 against a local symbol' can not be used when making a shared object; recompile with -fPIC
libstemmer_c/libstemmer.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
make: *** [stemmer4r.so] Error 1


Add CFLAGS:

[email protected]:/usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6/ext/stemmer4r/libstemmer_c# make
include mkinc.mak
CFLAGS   =  -fPIC
libstemmer.o: $(snowball_sources:.c=.o)
        $(AR) -cru [email protected] $^