How To Change/Override Linux Password from other Linux PC

Or if you have linux running on an SD card (e.g. Raspberry Pi) and you want to change the password from another your Linux PC, here’s how to do it.

1. Mount the target system on your linux PC. If it’s a Raspberry Pi SD card, simply plug it in. Now let’s say the mount point is /media/stick, and inside that folder you will find system folder such as /etc, /dev, /proc, and others.

2. Information of user and passwords in most linux systems are kept in a text file, consecutively /etc/passwd and /etc/shadow. What we need to change is the latter. Open the shadow file (use sudo cat /etc/shadow), and see the entry for the user whom you want to change the password. The entry contains 9 fields, separated by colon (“:”). More detail on this type man shadow on terminal. Our interest lies on the second field, which is the encrypted password field. This field has format of following:

$id$salt$hashedpassword

Now look into the format of your shadow file, if the id is 6, most likely SHA-512 encryption is used. The other possibilities are MD5 (id 1) and SHA-256 (id 5).

3. We will use mkpasswd, a front-end for crypt, the function for linux password encryption. To create a new password with SHA-512 encryption, do this:

mkpasswd -m sha-512 <password> <salt>

You can pick anything for the password, but for salt you need to give 8-16 random sequence of characters. If salt is empty, mkpasswd will generate a random salt, hence everytime you call mkpasswd it will give different result. For shadow, make sure you give in your salt. For example:

ariandy@linux:~$ mkpasswd -m sha-512 mypassword OhIuVG9LLE79.5XV
$6$OhIuVG9LLE79.5XV$GCO6Oyd2.IeONgjXWZDXzZbU8QFDUYD2mrrhQQeRzsceGmek4SRGMqXiJbod5UQ1WivVD1GPt4k5sPo6.PVs//

Now put this output in the second field of /etc/shadow file. Note that hashed password contains no colon, because colon is the delimiter between fields in shadow.

Source

Advertisements

Raspberry Pi Backup and Restore from Linux

0. Preparation

Before backing up the SD Card of Raspberry Pi, do clean up the system first to remove unneeded files such as package source codes and apt-get caches. You might be surprized by how much apt-get is keeping cache of downloaded packages. It was nearly 600MB for me. Cleaning this up would give you smaller backup image.

df -h
sudo apt-get clean
df -h

See how much space you free’d after cleaning apt-get’s cache.

I like to keep a piece of information, that tells me that the system is in the state of just being restored. I do that by either editing the MOTD (Message of The Day) or making an empty file on my home folder “THIS_IS_JUST_RESTORED”.

To back up the SD Card, first plug the SD Card into the SD Card reader of your laptop/PC that runs Linux. I use Ubuntu 12.04 Precise Pangolin. Then get any files that you need to copy, after that unmount the SD Card. Default Raspbian installation has 2 partitions made on the SD Card, so do not forget to unmount both. To check which folder is mounted to which device, use df -h and see the lines that start with /dev/mmcblk0p1 and /dev/mmcblk0p2. Then unmount the corresponding folders:

sudo umount /media/first_sd_partition
sudo umount /media/second_sd_partition

1. Backup

To back up the SD card, what we are about to do is making an image of the device, in this case both of the SD card partitions. For Linux, we have dd. I personally use dcfldd, an enhanced version of dd. It gives progress percentage, and also multiple on-the-fly hash calculation. To install dcfldd, in ubuntu simply:

sudo apt-get install dcfldd

If you want the backed up image to be as small as possible, you have to compress the image of the SD card. In Linux it is possible to compress an image which is currently being backed up (on-the-fly compression). With this you can save your hard disk space, because if you’re making an image without on-the-fly compression, you will require at least the same size as the SD card. If your have 8GB SD card, although only used partially, the resulting image from dd or dcfldd will be around 8GB in size.

To backup with on-the-fly compression:

sudo dcfldd bs=4M if=/dev/mmcblk0 hash=sha1 hashlog=./image.img.sha1sum | gzip > image.img.gz
sudo sync

And we’re done.

Everytime after you run dd or dcfldd, do not forget to call sudo sync to flush system buffers. I forgot to do this once and my system goes all crazy and weird.

If you want to do it the long way, you can do backup without compression:

sudo dcfldd bs=4M if=/dev/mmcblk0 of=./image.img hash=sha1 hashlog=./image.img.sha1sum
sudo sync

Then if you realize that it takes too much space, you can compress it anytime with gzip:

gzip image.img

Please note that it will take a while, since we’re dealing with gigabytes of data.

I also like to watch the image file grow in real time, so I do this:

watch ls -lh image.img*

2. Restore

If you have your image file already compressed, you can either uncompress and then restore, or uncompress while restore.

I find uncompressing and restoring in the same time (on-the-fly) is the most efficient way:

gunzip -c image.img.gz | sudo dcfldd bs=4M of=/dev/mmcblk0 hash=sha1 hashlog=./hash.log
sudo sync

Again, do not forget to flush the system buffer by calling sudo sync.

This hash thing, is for making sure that the backed up and restored image are consistent, not broken in any way which might happen during transfer/copying or upload. After the process finished, compare both of the hashes (image.img.sha1sum and hash.log) and make sure they are identical. You don’t need to read both files manually, just tell linux to do it:

diff -sq image.img.sha1sum hash.log

This will tell you either both files are identical or not.

If you are bored and have a lot of free space in your hard disk, you can do the restoring process in the long way by first uncompressing the image, then write it to the SD card:

gunzip image.img.gz
sudo dcfldd bs=4M if=./image.img of=/dev/mmcblk0 hash=sha1 hashlog=./hash.log
sudo sync

Setting Up WiFi Access Point with Edimax EW-7811UN on Raspberry Pi

I am running an image processing algorithm on my Raspberry Pi and I need to access it wirelessly (wired connection is sooo ’90s :p ). So my requirements are following:
– WiFi Access Point for remote access
– no need internet connection
– runs on Raspbian Wheezy

Steps:
1. Configuring network
Edit /etc/network/interfaces, remove anything related to wlan0 then add this lines:

iface wlan0 inet static
address 10.0.0.1
network 10.0.0.0
netmask 255.255.255.0
broadcast 10.0.0.255

Although the configuration is already defined, sometimes it won’t be applied by the system upon booting. To make sure, add ifup wlan0 inside /etc/rc.local file before exit 0 so it will look like this:

#!/bin/sh -e
#
# rc.local

...
# add this
ifup wlan0

exit 0

2. Installing DHCP Server and “DNS” Configuration
DHCP is the one that is responsible to give out IP addresses to clients who are connected to the Access Point. Meanwhile, DNS or Name Service is working as some kind of dictionary, that translates numbers of IP address into human-readable names that are easy to remember and to type.

This time I will use dnsmasq for easy and fast DHCP+DNS deployment.

sudo apt-get -y install dnsmasq

After installation it will run the daemon automatically. Stop it and then we work on the configuration:

sudo service dnsmasq stop
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
sudo touch /etc/dnsmasq.conf

Now edit the newly created dnsmasq.conf config file. For minimum setup, this should be the content of the config file:

interface=wlan0
expand-hosts
domain=local
dhcp-range=10.0.0.10,10.0.0.20,24h
dhcp-option=3,10.0.0.1

For full configuration options, you can refer to the original config file (dnsmasq.conf.orig).

Although 10.0.0.1 is easy enough to remember, isn’t it easier if we use “raspi” as address? So then you can simply type ssh raspi or http://raspi/. To make this happen, edit /etc/hosts file, append with this:

10.0.0.1        raspi raspberrypi raspi.local raspberrypi.local

3. Installing hostapd
This WiFi USB Dongle from Edimax uses Realtek chipset, it is rtl8192 to be precise. Kindly enough, Realtek has provided hostapd implementation which is specific for this chipset.

For installation instructions, I suggest you to follow Jens Segers’ blog post.

After finishing installation, edit the hostapd configuration file to fit your needs.

Streaming OpenCV Output Through HTTP/Network with MJPEG

If you want to stream your OpenCV output image to another device which connected to the same network, one way is to write the OpenCV output as MJPEG file, then stream this file through HTTP.

Please note that:
– This might be not the best way (correct me if I’m wrong)
– OpenCV 2.3 cv::VideoWriter has memory leak bug

Steps:
0. OpenCV Stuff
I assume you already have your OpenCV libraries installed, because several of OpenCV’s dependencies are also MJPEG-Streamer’s, such as libv4l-dev and libjpeg-dev. And make sure your OpenCV application works (it writes the mjpeg file)

1. Install MJPEG-Streamer
Install the dependencies first

sudo apt-get install imagemagick

Then get the latest MJPEG-Streamer from the repository:

git clone https://github.com/codewithpassion/mjpg-streamer.git
cd mjpg-streamer/mjpg-streamer
make USE_LIBV4L2=true clean all
sudo make DESTDIR=/usr/local install

Then set the environment variable LD_LIBRARY_PATH if you haven’t, either in /etc/bash.bashrc or your own ~/.bashrc, add this line:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

2. Into The Main Show
First fire up your OpenCV application, this is my application, simple background subtraction and writes the output into mjpg file.

git clone https://github.com/ariandyy/bgsubtract.git
cd bgsubtract/
make bgsubtract2

Prepare the folder to put the mjpg file into, for example in my home folder:

mkdir /home/ariandy/mjpg

Then run the application:

./bgsubtract2 /home/ariandy/mjpg/out.mjpg -bgs

Leave it running in one terminal, and open another terminal to test the MJPEG-Streamer:

mjpeg_streamer -i "input_file.so -f /home/ariandy/mjpg" -o "output_http.so -w /usr/local/www"
 

The command above is the most basic setting to get the streaming to work. By default, the web server is listening to port 8080. If you want to use port 80, you have to run the command as root (or with sudo).

The default www root of MJPEG-Streamer is located at /usr/local/www. There you can find the demo pages, from which you can get the idea of several ways to display the stream: either direct stream (supported by most Grade-A Desktop Browsers (Sorry, IE!)), using Java MJPEG viewer (cambozola), javascript, etc.

3. Viewing The Stream
On the client, simply point the browser to the server’s IP address with corresponding port (8080). e.g. server’s IP address is 192.168.0.1, then to access it from the client, point to http://192.168.0.1:8080. If you want to omit the :8080 part, then you have to run the server on port 80 (HTTP), as following:

sudo mjpeg_streamer -i "input_file.so -f /home/ariandy/mjpg" -o "output_http.so -w /usr/local/www -p 80"
 

I tried this with Google Chrome, Firefox, Android Browser, and Firefox for Android.

Sources:
http://www.raspberrypi.org/phpBB3/viewtopic.php?t=8659
http://sourceforge.net/projects/mjpg-streamer

Raspberry Pi Rasbian + OpenCV

Sources:
http://opencv.willowgarage.com/wiki/InstallGuide%20%3A%20Debian
http://opencv.willowgarage.com/wiki/InstallGuide_Linux
http://mitchtech.net/raspberry-pi-opencv/
https://github.com/jayrambhia/Install-OpenCV/blob/master/Ubuntu/2.4/opencv2_4_3.sh
http://www.ozbotz.org/opencv-installation/

First of all, building OpenCV on Raspbian (Raspberry Pi) will take at least 4 hours. So consider doing this before you sleep so you can leave it compiling overnight.

And also if you’re doing all this through SSH, linux’s “screen” program will definitely very useful. It’s not installed by default, so do this:

sudo apt-get install screen

Click here for short and quick tutorial of how to use screen.

Now let’s cook!

  1. Prepare fresh installed Rasbian on Raspberry Pi.
  2. Run sudo apt-get update, then sudo apt-get upgrade, to make sure everything is updated.
  3. Copy and paste this into the terminal to install all the dependencies (NOTE: Some package names are altered, e.g. libavcodec52 is not available anymore, replaced by libavcodec53):

    sudo apt-get -y install build-essential
    sudo apt-get -y install cmake
    sudo apt-get -y install pkg-config
    sudo apt-get -y install libpng12-0 libpng12-dev libpng++-dev libpng3
    sudo apt-get -y install libpnglite-dev libpngwriter0-dev libpngwriter0c2
    sudo apt-get -y install zlib1g-dbg zlib1g zlib1g-dev
    sudo apt-get -y install libjasper-dev libjasper-runtime libjasper1
    sudo apt-get -y install pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools
    sudo apt-get -y install libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-prog
    sudo apt-get -y install libavcodec53 libavcodec-dev libavformat53 libavformat-dev libavutil51 libavutil-dev libswscale2 libswscale-dev
    sudo apt-get -y install libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev
    sudo apt-get -y install libxine1-ffmpeg libxine-dev libxine1-bin
    sudo apt-get -y install libunicap2 libunicap2-dev
    sudo apt-get -y install libdc1394-22-dev libdc1394-22 libdc1394-utils
    sudo apt-get -y install swig
    sudo apt-get -y install python-numpy
    
    sudo apt-get -y install libpython2.6 python-dev python2.6-dev
    sudo apt-get -y install libjpeg-progs libjpeg-dev
    sudo apt-get -y install libgstreamer-plugins-base0.10-dev
    
    sudo apt-get -y install libqt4-dev libgtk2.0-dev
    
  4. We need to install other dependencies (x264, ffmpeg, and v4l) manually:
    sudo apt-get remove ffmpeg x264 x264-dev
    
    wget ftp://ftp.videolan.org/pub/videolan/x264/snapshots/x264-snapshot-20120528-2245-stable.tar.bz2
    tar -xvf x264-snapshot-20120528-2245-stable.tar.bz2
    cd x264-snapshot-20120528-2245-stable/
    ./configure --enable-shared --enable-pic
    make
    sudo make install
    cd ..
    
    wget http://ffmpeg.org/releases/ffmpeg-0.11.1.tar.bz2
    echo "Installing ffmpeg"
    tar -xvf ffmpeg-0.11.1.tar.bz2
    cd ffmpeg-0.11.1/
    ./configure --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-nonfree --enable-postproc --enable-version3 --enable-x11grab --enable-shared --enable-pic
    make
    sudo make install
    cd ..
    
    wget http://www.linuxtv.org/downloads/v4l-utils/v4l-utils-0.8.8.tar.bz2
    tar -xvf v4l-utils-0.8.8.tar.bz2
    cd v4l-utils-0.8.8/
    make
    sudo make install
    cd ..
    
    
  5. Download desired version of OpenCV, in this example we’re using version 2.4.3. Unpack it anywhere you like.
    tar -xjvf  OpenCV-2.4.3.tar.bz2
    rm OpenCV-2.4.3.tar.bz2
    cd OpenCV-2.4.3/
    mkdir build
    cd build/
    
  6. Then to create standard configuration just follow this command:
    sudo cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_NEW_PYTHON_SUPPORT=ON -D BUILD_EXAMPLES=ON .. | sudo tee ./CMAKE.log

    The part | sudo tee ./CMAKE.log is used for logging purpose. Normally I used redirection (>>) but it won’t work if you are trying to write inside a folder with no permission.

    You can use cmake-gui if it’s more comfortable for you, but it has to be run from a desktop environment (X Server is running). I did this over SSH so I don’t have the luxury of X.

  7. Then a configuration will be generated, make sure everything you need has no problem. For example see the first lines, it should be able to found the libraries needed, such as gtk+2.0, libavcodec, libavformat, etc.
    In my case cmake couldn’t find linux/videodev.h. According to this source, apparently there’s a change of structure for libv4l but OpenCV didn’t take that into account. In my installation I found the file libv4l1-videodev.h inside /usr/local/include, so we need to make a symlink to the location searched by OpenCV installation:

    sudo ln -s /usr/local/include/libv4l1-videodev.h /usr/include/linux/videodev.h
    

    TODO: there’s also one missing library, ffmpeg/avformat.h, but I’m not sure whether it’s necessary to fix or not.

  8. Now we’re ready to start the build. This process will take a long time to finish. So as said before, consider doing this before you sleep overnight. I do all this through SSH (I only have 1 keyboard and 1 mouse and I don’t want to back and forth plugging them between my laptop and the Raspberry Pi), so here’s where “screen” program is very useful. You can ignore this if you don’t use SSH:
    screen -S OpenCV_Installation

    Seems like nothing happened, but what actually happens is “screen” starts a new TTY/session, which you can build OpenCV, then you can “detach” it so it will run on background. After that you can quit the SSH session and turn off your laptop, without even bothering the building process.

    Now change user into root

    sudo su -

    then execute this as root

    make && make install
    

    or if you want some logging

    make | tee make.log && make install | tee make_install.log
    
  9. Now “detach” the compilation “screen” by pressing Ctrl+A then Ctrl+D consecutively. The building process will run in background. To check the process you have to get back to the “screen”, simply call in any session – either SSH or local
    screen -r

    If you use the logging with tee make.log and tee make_install.log as mentioned above, to check the status you can use tail

    tail -f make.log
    
  10. Sit back, watch some movie, or sleep. See you in 5 hours!

POST INSTALLATION SETUP

Now that you have energy from your sleep, let’s continue setting up the system to complete the installation.

  1. We have to tell the system that there are libraries available. Create a file named opencv.conf located in /etc/ld.so.conf.d/
    sudo touch /etc/ld.so.conf.d/opencv.conf
    

    Then using your favorite editor add this line into the file:

    /usr/local/lib
    

    After that:

    sudo ldconfig /etc/ld.so.conf.d
    
  2. Then we have to add this 2 lines into the file /etc/bash.bashrc
    PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
    export PKG_CONFIG_PATH
    

Testing

NOTE: Seems like my Raspberry Pi is unable to detect my cheap Logitech C170 yet. This is weird.

OpenCV comes with lots of pre-compiled sample programs, find it in build/bin folder. Some of them are drawing and kmeans. And if you have USB webcam you can check out lkdemo

./lkdemo

Press r then it will start tracking objects. Cool huh?

I also found this over here, to test camera capture using python.

import cv2

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

if vc.isOpened(): # try to get the first frame
    rval, frame = vc.read()
else:
    rval = False

while rval:
    cv2.imshow("preview", frame)
    rval, frame = vc.read()
    key = cv2.waitKey(20)
    if key == 27: # exit on ESC
        break

Save it and run it with python:

python opencv_cam_test.py

For obvious reasons you can’t do this over SSH.

Making Raspberry Pi SD Card Backup in Linux

$ sudo dd if=/dev/mmcblk0 of=/path/to/image/file.img bs=4M

/dev/mmcblk0 argument depends on your system. In some system this argument could be /dev/sdX where X is the “device number” that points to the SD card. What is X could be known from:

$ sudo fdisk -l

Make sure you point dd to the device (e.g. /dev/mmcblk0, /dev/sdd), not the partition (e.g. /dev/mmcblk0p1, /dev/sdd1).

Making an image of 8GB SD card will take a long time, and dd won’t give any progress update whatsoever. It’s done, when it says it’s done (Walter White, Breaking Bad. LOL). It took 10 minutes (14.3MB/s) with my laptop. The only way to know the progress is by opening other terminal and check what is the size of the image file. The final size of the image file is same as the size of your SD card. OR you can use one front-end (?) of dd, namely dcfldd.

Before unplugging the SD card don’t forget to flush the buffer/cache:

$ sudo sync

Calculate the SHA1Sum of the image for future purposes. For huge file like this there’s a chance that it will get damaged when you do a lot of moving and transfer.

$ sha1sum file.img >> file.img.sha1sum

This will save the sha1sum into a file named file.img.sha1sum. After this you can compress it, transfer it, anything. Then, before restoring this image back into SD card, calculate the SHA1 once again and compare it to the original SHA1.

To compress the image use this:

$ cd /path/to/image
$ gzip file.img

This will “throw” the empty bits of the image and squeeze the file only with bits that actually contains data.

Restoring The Image

To restore the image first decompress the file

$ gunzip file.img.gz

then use dd

$ sudo dd if=/path/to/image/file.img of=/dev/mmcblk0 bs=4M

don’t forget to flush the cache by

$ sudo sync

TODO:
– sha1sum checking

Getting Started with Raspberry Pi

1. Get a SD Card, consult to compatibility page on http://elinux.org

2. Plug it in to linux machine.

3. sudo cfdisk /dev/mmcblk0

4. Delete every partition from there, don’t forget to select “write”.

5. Download Raspbian from here, check the sha1sum, then unzip. You’ll get a image file named something like 2012-12-16-wheezy-raspbian.img

6. Write it into your SD card, make sure you write it into the SD card device (e.g. /dev/mmcblk0, /dev/sdd), not the partition of the SD card (e.g. /dev/mmcblk0s1, /dev/sdd1).

$ sudo dd bs=4M if=./2012-12-16-wheezy-raspbian.img of=/dev/mmcblk0

Wait until it finishes. There will be no progress bar or indicator whatsoever, other than the SD card reader’s LED.

The Raspbian image is about 2GB in size. No matter how big your SD card, when you use it as it is after flashing it won’t be able to utilize the rest of the freespace. Then you need to expand/grow the partition. We do this later on below.

7. Flush the buffer (NOTE: at the first time I skipped this part, then random block reading error pops out everywhere. Segmentation fault, filesystem panic, etc. So make sure you do this.)

$ sudo sync

8. Remove the SD card, put it on the Raspberry Pi and fire it up!

9. First boot it will take some time, after then a blue screen (of life) will come up. It’s actually the “raspi-config” program.

10. Select the second option from the top to expand the free space of your SD card the next time Raspbian reboots. Configure other options as needed (other than expanding the partition, mostly not really necessary).

11. Select finish, then your Raspberry Pi will reboot.

12. Log in with username pi and default password raspberry (if you didn’t change it before in raspi-config). Check out the desktop environment by:

$ startx

Enjoy your Raspberry Pi.