Recently I decide to jump into the pool of using diskless Ubuntu. Basically the client computer downloads the necessary files from the Ubuntu server every time during the boot. To keep things simple and easy, Ubuntu does that by using TFTP. So the first step is to set up a TFTP server on the server. For those who haven’t heard of TFTP, it is similar to FTP, except that it has no security feature, and the function is extremely limited. Anyway, here is how to set up a TFTP server on Ubuntu 11.10:

Installing TFTP sounds easy. However, I’ve heard that many people experienced many issues during the installation, such as Error code 2: Access violation issue. That’s why I create this tutorial. If you follow exact the same steps, you will not experience any problem.

First, let’s install all the necessary packages:

sudo apt-get install xinetd tftpd tftp -y

Next, we need to create a configuration file:

sudo nano /etc/xinetd.d/tftp

Put the following content into the file.

service tftp
   protocol = udp
   port = 69
   socket_type = dgram
   wait = yes
   user = nobody
   server = /usr/sbin/in.tftpd
   server_args = var/lib/tftpboot -s
   disable = no

In the server_args, I have var/lib/tftpboot, which represents the location of the tftp root, i.e., /var/lib/tftpboot. Notice that I skip the root /.

Now let’s change the ownership of the directory:

sudo mkdir /var/lib/tftpboot
sudo chown -R nobody:nobody /var/lib/tftpboot
sudo chmod -R 777 /var/lib/tftpboot

and start the TFTP service:

sudo service xinetd stop
sudo service xinetd start

Verify the TFTP is running correctly or not:

netstat -na | grep LIST | grep 69

You should see something like this:

tcp        0      0    *     LISTEN

Test: Upload a file to TFTP Server

Now let’s test the TFTP server by logging into the server first:

tftp localhost

and upload a file:

tftp> put myfile.jpg
Sent 56733279 bytes in 5.7 seconds



Make sure that file has been uploaded:

ls -l /var/lib/tftpboot

Test: Download a file from TFTP Server

Now, let’s go to a different directory and download the file we just upload.

cd some_other_directory

and log in to the tftp server again:

tftp localhost

and get the file:

tftp> get myfile.jpg
Received 56733279 bytes in 5.7 seconds

You are done.

Troubleshooting (e.g., Error code 2: Access violation)

If you see a message like: Error code 2: Access violation

Make sure that you:
– Follow the exact procedure in this tutorial
– Make sure that the tftp is started with -s flag.
– Check the permission of the directory, i.e., 777
– After you’ve made any changes to the TFTP configuration, make sure that you stop and start the inet service again.
– Don’t forget to quit tftp before retrying the command.

That’s it!

Enjoy TFTP.




Raspberry Pi and Node.JS: Basic Setup

Posted on March 31, 2013

I hade some time to play around with my Raspberry Pi. The primary goal was to have a basic setup for Node.JS 0.10.x with Raspbian “wheezy” (Hard-float) on a Raspberry Pi.

I run in some troubles to install Node.JS on Raspbian soft-float image and also when you wanted to compile it for yourself you need some time. Luckily there is an ARM binary package for Raspberry Pi (See ‘other release files’ on Node.JS download section).

For this tutorial you need:

  • Raspberry Pi
  • SD card at least 4GB
  • HDMI to DVI Converter – to plug the Raspberry Pi to your monitor
  • micro usb charger – for the power supply
  • Network cable and internet connection
  • Node.JS 0.10.x ARM binary package
  • Raspbian ‘wheezy’ hard-float image (2013-02-09)

Note: We will use Node.JS 0.10.2 in this tutorial because the ARM binary package for 0.10.3 was not yet available. But I think when the new ARM package is available this tutorial should also work with the new minor release.


Install Raspbian on your SD Card

Download Raspian “wheezy” (hard-float, 2013-02-09-wheezy-raspbian) and install it on your SD card. There are plenty of tutorials which shows you how to install Raspbian on your SD card. Have a look at the following tutorials how you can install Raspbian on your SD card.

Note: You have to use the Raspbian hard-float image. With the soft-float image the Node.JS ARM binary will not work.

Configure your Raspberry Pi

Plug in your usb micro charger and Rasberry Pi will boot up and display the
raspi-config dialog.


These are the configuration steps you should do with ‘raspi-config’ after the first time you start your Raspberry Pi:

  • expand_rootfs – select expand_rootfs to expand the root partition to fill out the whole SD card
  • memory_split – we reduce the GPU to the minimum (16mb), because we will not start intensive graphical tasks.
  • change_pass – change password for ‘pi’ user. The user ‘pi’ is the preconfigured user in Raspbian
  • ssh – enable the ssh server, so that we can use ssh to log in from remote.
  • You should also adapt the keyboard, timezone and locale to your needs. When you are finished select finish to end the configuration.

After the configuration you need to restart your system, so that the resizing of the root partition and memory split will work.

sudo reboot

You can always change your settings again with ‘raspi-config’.

sudo raspi-config

After the restart, login in with the user ‘pi’ and the new password you have choosen. Now we need to update our system with ‘apt-get’ our package manager.

sudo apt-get update
sudo apt-get upgrade

With no further network changes your Raspberry Pi will use DHCP to get a dynamic IP adress. So we are going to change this to a fixed IP adress. Because we will use our system as Node.JS server and this is quite simpler with a fixed IP adress. You can also work with a dynamic IP adress and just skip the following step.

To change the system to a fixed IP adress just open the interface configurartion file. For that we use the preinstalled editor ‘nano’.

sudo nano /etc/networks/interfaces

Replace the following lines for your network device eth0 and just adapt the settings to your network (e.g. IP, gateway, dns, etc.) configuration.

iface eth0 inet static

After the changes in the network configuration file just stop and start your network device with ‘ifdown’ and ‘ifup’.

sudo ifdown eth0
sudo ifup eth0

Yo can try now from a remote system to connect with the user ‘pi’ over SSH to our Raspberry Pi.

ssh pi@

Installing Node.JS

We will download the ARM binary package from Node.JS and create the directory /opt/node where we want Node.JS installed. We will not place Node.JS in ‘/usr/local’, instead we use a separate directory for Node.JS, this way it is much easier to update a manually installed package.

sudo mkdir /opt/node

The next step is to dowload the Node.JS ARM binary package, unpack it and copy the content to our node directory /opt/node.

wget http://nodejs.org/dist/v0.10.2/node-v0.10.2-linux-arm-pi.tar.gz
tar xvzf node-v0.10.2-linux-arm-pi.tar.gz
sudo cp -r node-v0.10.2-linux-arm-pi/* /opt/node

Finally we have to add Node.JS to our path variable. For that you have to edit the ‘/etc/profile’ configuration file.

nano /etc/profile

Add the following lines to the configuration file before the ‘export’ command.

export PATH

Yo need now to logout and log in again, so that the changed path variable will work.


Configure Node.JS

We need now a start script for Node.JS. Our script will start the Node.JS with the user ‘pi’ and look for a server.js file in the /home/app directory. The output stream from Node.JS will be stored in the file nodejs.log. Just create the file nodejs.sh in your current directory with the following content:



case "$1" in

	echo "starting node: $NODE $SERVER_JS_FILE"
	sudo -u $USER $NODE $SERVER_JS_FILE > $OUT 2>$OUT &

	killall $NODE

	echo "usage: $0 (start|stop)"

exit 0

Note: When yo want to use a privileged port (TCP/IP port numbers below 1024) you have to start the script as root.

Make the script executable with ‘chmod’ and copy it to ‘/etc.init.d’. The last step is to register the script as service with ‘update-rc.d’. Then our Node.JS server will automatically start up when the Raspberry Pi is powered on.

chmod 755 nodejs.sh
sudo cp nodejs.sh /etc/init.d
sudo update-rc.d nodejs.sh defaults

The last step is to create a Node.JS server.js file in our /home/pi/app directory. The directory ‘/home/pi/app’ will be our Node.JS project directory.

mkdir /home/pi/app

The ‘server.js’ file is just a simple Node.JS server which will listen on port 8080 and display ‘Hello World’ and write some output to the console. Just place the following content as ‘server.js’ file in directory ‘/home/pi/app’.

var http = require('http');

http.createServer(function(req,resp) {
	resp.writeHead(200, {"Content-Type": "text/plain"});
	resp.write("Hello World");

	console.log("sample output to console");


Start Node.JS

It’s time to start Node.JS with our start script and see if it all fits together.

sudo /etc/init.d/nodejs.sh start

Open a browser with the url You should now see a page which displays ‘Hello World’ and the output from the console.log() statement in the /home/pi/node.log file.

To stop Node.JS just enter

sudo /etc/init.d/nodejs.sh stop

When you want to use ‘npm’ or ‘node’ as root with sudo you have to add the ‘-i’ option, so that the root user’s environment is acquired. For example when you want to install the package ‘forever’ globally with ‘npm’.

sudo -i npm install forever -g

Update Node.JS to a new version

To update Node.JS to a new version just follow the next steps. Also check first if there is an ARM package available.

delete the old version

# sudo rm /opt/node -r

create new directory

# sudo mkdir /opt/node

unpack the new version and copy the content to our node.js directory. Replace ‘x’ with the new minor version.

# tar xvzf node-v0.10.x-linux-arm-pi.tar.gz
# sudo cp -r node-v0.10.x-linux-arm-pi/* /opt/node

Check current version

# node -v

I hope this tutorial was useful for you and will give you some quick start to play around with Node.JS and Raspberry Pi.

Genomen stappen

  1. SD kaart image gemaakt van raspberry wheezy
  2. Raspberry config (fill sd card, time locale, memory use)
  3. Stappen voor node.js + npm (incl git essentials)
  4. map / file   aanmaken / var/www/app.js
  5. forever geinstalleerd (forever start app.js)
  6. stappen voor crontab (opstarten bij reboot) (vergeet niet te chmod 777)
  7. socket.io lukt niet om te installeren sofar


Some time ago, I wrote a post on keeping Node.js apps running even after logging out from the shell. It was cool and all, but soon you will soon realize that Forever alone is not enough. Because, on system reboots, Forever itself is killed. Who will restart your app now?

There are many options for handling this kind of scenario, but today we will implement a solution using cron. Let’s find out how we can make Forever and our Node.js app reboot-proof.

The app you want Forever to start on system restart is probably a web app, so let’s try this thing on an Express.js web app.

Install Express, if you haven’t already:

$ npm install -g express

Now, create an Express app:

$ mkdir example
$ cd example
$ express

There we have our Express app. It should be started using Forever – on system reboots – automatically.

First we need to write a bash script to check if our app is running or not; if it is not running, it should start the app using Forever. Create a file named app-starter.sh in your home directory with this content:


if [ $(ps aux | grep $USER | grep node | grep -v grep | wc -l | tr -s "\n") -eq 0 ]
        export NODE_ENV=production
        export PATH=/usr/local/bin:$PATH
        forever start ~/example/app.js > /dev/null

Note how we augmented the environment variables in the script. Those are crucial to making the script work successfully from cron. Change the NODE_ENV variable, according to your requirement./usr/local/bin needs to be added to PATH, else Forever just won’t work on most systems.

Next, make the script executable:

$ chmod 700 ~/app-starter.sh

Then, run it and load http://localhost:3000 on your browser to confirm it actually started our app using Forever:

$ ./app-starter.sh

Looking good. Now it’s time to make cron do the job for us. We want to make sure that the app-starter script is execute when the system is rebooted. Append the following code to crontab:

@reboot ~/app-starter.sh >> cron.log 2>&1
# guess what the commented line below would do if enabled
# */1 * * * * ~/app-starter.sh >> cron.log 2>&1

To open crontab:

$ crontab -e

After making changes to crontab, restart your system.

Load http://localhost:3000 on your browser to behold the magic of Forever + cron :D Now you don’t have to worry about system restarts taking your app down, anymore. cron takes care of Forever, and Forever takes care of your app; and you win at life!

While this example is perfect for a single app, you will need to hack the app-starter script to make it work for more than one Node.js app. How to do that is your homework. All the best!

If the Ubuntu Server installer has set your server to use DHCP, you will want to change it to a static IP address so that people can actually use it.

Changing this setting without a GUI will require some text editing, but that’s classic linux, right?

Let’s open up the /etc/network/interfaces file. I’m going to use vi, but you can choose a different editor

sudo vi /etc/network/interfaces

For the primary interface, which is usually eth0, you will see these lines:

auto eth0
iface eth0 inet dhcp

As you can see, it’s using DHCP right now. We are going to change dhcp to static, and then there are a number of options that should be added below it. Obviously you’d customize this to your network.

auto eth0
iface eth0 inet static

Now we’ll need to add in the DNS settings by editing the resolv.conf file:

sudo vi /etc/resolv.conf

On the line ‘name server xxx.xxx.xxx.xxx’ replace the x with the IP of your name server. (You can do ifconfig /all to find out what they are)

You need to also remove the dhcp client for this to stick (thanks to Peter for noticing). You might need to remove dhcp-client3 instead.


sudo apt-get remove dhcp-client


Now we’ll just need to restart the networking components:

sudo /etc/init.d/networking restart

Ping http://www.google.com. If you get a response, name resolution is working(unless of course if google is in your hosts file).

Really pretty simple.

These instructions work for the Raspberry Pi running Raspbian (hard float) and create a hardware optimized version of NodeJS for the Raspberry PI, (and include a working install and NPM!!!):

1) Install Raspbian – http://www.raspberrypi.org/downloads

2) Install the necessary dependecies:

sudo apt-get install git-core build-essential

(If you just installed git then you need to administer your git identity first, else adding the patches below will fail!!!)

3) Check out Node.js source (0.8.8 or higher):

git clone https://github.com/joyent/node.git
cd node
git checkout v0.8.8 -b v0.8.8

4) Push four patches for V8 and OpenSSL ARM support:

The following two patches are only needed when compiling Node version <0.8.10 (already committed for 0.8.10pre NOT yet for master)

Remark: The first patch is already commited upstream to V8 cutting-edge branch (node is on a lower version of V8). The 2nd patch is pushed to the V8 bug list.

curl https://github.com/joyent/node/commit/25c2940a08453ec206268f5e86cc520b06194d88.patch | git am
curl https://github.com/joyent/node/commit/1d52968d1dbd04053356d62dc0804bcc68deed8a.patch | git am

The following two patches are only needed when compiling Node version <0.8.9 (already committed for master and 0.8.9pre + higher)

curl https://github.com/joyent/node/commit/f8fd9aca8bd01fa7226e1abe75a5bcf903f287ab.patch | git am
curl https://github.com/joyent/node/commit/7142b260c6c299fc9697e6554620be47a6f622a9.patch | git am

5) Configure correctly and ‘make’:


6) (Optional!) If you want to do the tests then execute (make test can’t be used because the standard timeout is too low, i.e. the Rasp PI is too slow… ;-)):

python tools/test.py -t 120 --mode=release simple message

7) Install Node and NPM in your OS:

sudo make install

NOTE on 0.8.8 tests: some tests still have issues (8 tests fail, some are timeouts (as the PI isn’t that fast 😉 and one is a known bug)!

Note: Some users have reported issues with using Mac OS X to create SD cards.

  1. These commands and actions need to be performed from an account that has administrator privileges.
  2. Download the image from a mirror or torrent
  3. Verify if the the hash key is the same (optional), in the terminal run:
    • shasum ~/Downloads/2012-12-16-wheezy-raspbian.zip
  4. Extract the image:
    • unzip ~/Downloads/2012-12-16-wheezy-raspbian.zip
    • (or: just double click the zip, it will extract automatically)
  5. From the terminal run df -h
  6. Connect the SD card reader with the SD card inside
  7. Run df -h again and look for the new device that wasn’t listed last time. Record the device name of the filesystem’s partition, for example, /dev/disk3s1
  8. Unmount the partition so that you will be allowed to overwrite the disk:
    • sudo diskutil unmount /dev/disk3s1
    • (or: open Disk Utility and unmount the partition of the SD card (do not eject it, or you have to reconnect it)
  9. Using the device name of the partition work out the raw device name for the entire disk, by omitting the final “s1” and replacing “disk” with “rdisk” (this is very important: you will lose all data on the hard drive on your computer if you get the wrong device name). Make sure the device name is the name of the whole SD card as described above, not just a partition of it (for example, rdisk3, not rdisk3s1. Similarly you might have another SD drive name/number like rdisk2 or rdisk4, etc. — recheck by using the df -h command both before & after you insert your SD card reader into your Mac if you have any doubts!):
    • For example, /dev/disk3s1 => /dev/rdisk3
  10. In the terminal write the image to the card with this command, using the raw disk device name from above (read carefully the above step, to be sure you use the correct rdisk# here!):
    • sudo dd bs=1m if=~/Downloads/2012-10-28-wheezy-raspbian/2012-12-16-wheezy-raspbian.img of=/dev/rdisk3
    • if the above command report an error(dd: bs: illegal numeric value), please change bs=1M to bs=1m
    • (note that dd will not feedback any information until there is an error or it is finished, information will show and disk will re-mount when complete. However if you are curious as to the progresss – ctrl-T (SIGINFO, the status argument of your tty) will display some en-route statistics).
  11. After the dd command finishes, eject the card:
    • sudo diskutil eject /dev/rdisk3
    • (or: open Disk Utility and eject the SD card)
  12. Insert it in the Raspberry Pi, and have fun