Setup a webcam security system with Ubuntu Linux and Motion

May 17
Snap from Office Security Cam

Snap from Office Security Cam

So, now that I’m in Morgantown – my home is too small to comfortably work on side gigs and personal projects – especially now that my family is getting bigger with the baby!  I’ve been using the office space I leased out more and more.  While exploring video conferencing with Matt last week, I had the thought “wouldn’t it be cool to have a security camera in the office?”.  So I did just that, and it’s actually quite easy for Ubuntu linux users.

What you need:
  • Ubuntu Linux ( I was using 8.04.1 at the time of installation )
  • one or more USB web cameras
What you can do:
  • Motion detection – record video/and or frames if there is motion.
  • Snapshot intervals – take time interval snapshots regardless of motion detection.
  • Live video IP stream in mjpeg format.
  • Specify recorded video to be saved in your choice mpeg, avi, flv, swf format.
  • When motion exists, have frames and videos draw a box around the specific motion for more obvious recognition of subtle movements ( this actually shows the shadow of the janitor near the door around 6 a.m. every morning – I wouldn’t have noticed otherwise! )
  • Easily send all data to a backup server in a variety of ways – I keep it simple by saving data to my Dropbox directory, a wonderful cross-platform data syncronization and sharing utility.

1.  Plugin your webcam.
For me, the Logitech QuickCam® Pro 9000 worked right out of the box, and was only 105$.

2.  Install Motion – software motion detector, and turn it on.

sudo apt-get install motion
sudo motion

3. Configure Motion

Everything really works out of the box with this – but isn’t quite organized to my liking, and probably not yours either. Global configuration is located inside /etc/motion.conf ( You’ll notice there are multiple threadN.conf files in this directory – which can be used for custom configured individual cameras if you are setting up more than one ).

Note: Be sure to restart the Motion server everytime you make a configuration change.

sudo /etc/init.d/motion restart

Take a look at the files, they are well documented. Below are a few helpful configurations to get your data organized quicker:


# Locate and draw a box around the moving object.
locate on

# Draws the timestamp using same options as C function strftime(3)
text_right %Y-%m-%dn%T-%q

# Text is placed in lower left corner
text_left SECURITY CAMERA %t - Office

Organize the filesytem to save data by date, instead of all in one directory.

# File path for snapshots (jpeg or ppm) relative to target_dir
snapshot_filename %Y%m%d/camera-%t/snapshots/hour-%H/camera-%t-%v-%Y%m%d%H%M%S-snapshot

# File path for motion triggered images (jpeg or ppm) relative to target_dir
jpeg_filename %Y%m%d/camera-%t/motions/hour-%H/camera-%t-%v-%Y%m%d%H%M%S-%q-motion

# File path for motion triggered ffmpeg films (mpeg) relative to target_dir
movie_filename %Y%m%d/camera-%t/movies/hour-%H/camera-%t-%v-%Y%m%d%H%M%S-movie

# File path for timelapse mpegs relative to target_dir
timelapse_filename %Y%m%d/camera-%t/timelapses/hour-%H/camera-%t-%Y%m%d-timelapse

4.  (Optional)  Setup a backup solution

a. Easy solution, get and install Dropbox — instructions on the Dropbox site.  Then update your motion.conf to save to your Dropbox directory:

target_dir /path/to/dropbox/security_camera

b. A more granular solution is to take advantage of hooks configurable in motion.conf. Using these, you can create bash scripts to do anything your heart desires ( like trigger a silent alarm on motion detection outside business hours ). Available hooks: on_event_start, on_event_end, on_picture_save, on_motion_detected, on_movie_start, on_movie_end.

If you have wput installed, you can easily upload files to a remote backup server with these hooks:

on_picture_save wput ftp://user@pass@server %f

However, this solution is somewhat less secure, as it uses FTP. In a future post I will detail how to secure this up using encrypted transfer and phrase free keys. ( Stay tuned! )

5. Live feed

This comes working out of the box with Motion. Check out your live stream in your web browser by navigating to: http://localhost:8081

That’s it! Webcam security made easy :-)

Related Images:

49 Responses to “Setup a webcam security system with Ubuntu Linux and Motion”

  1. mickos says:

    how to play mp3 on motion

  2. Raman B.V. says:

    How can I stop motion without shutting down my computer? It keeps running even after I exit the terminal.

  3. chriswpage says:

    I haven't used Motion in several months now, but I would imagine you can stop the daemon via: /etc/init.d/motion stop

  4. anaon says:

    did you try on_motion?

  5. Linux Fanatics says:

    [...] on Ubuntu?Red Hat Profits SoarSony Makes A Linux ComebackUbuntu Switches Back To GoogleObviously Motion as webcam security would be vastly more attractive with a GUI. Either GTK or QT. But even without a clean UI to work [...]

  6. Guest says:

    How can I enable the text_left command?
    I erase the ” ; '' the were before text_left, but it still not appear on the jpeg file

  7. Matthew says:

    Thanks, very helpful tutorial.

    The motion daemon dies quite silently if it can't write to its output directory. Daemon runs as user “motion”. Took a few minutes to figure that out.

  8. Khach La Vip says:

    program that supports sending captured images to an address email?

  9. Mithereal says:

    i use mutt like this in my motion.conf
    on_picture_save mutt -s “Test mail” -a %f

  10. Jayashleysmith says:

    Hi, How do i setup multible cams? I have one camera working just as i like. But have another cam plugged into /dev/video1, at the moment motion is only picking up /dev/video0 unless i change the video capture to either or. I can get either working one at a time. But not both at once. I also have no thred#.conf files?


  11. Nikolay says:

    how to auto run motion, example script . . . . . please

  12. Bask80 says:

    read the tutorial above? can simultaneously record sound?

  13. os says:

    did u test with multiple cams? i just tried with two different cams and it does not work. the second cam never seems to work (the second video device – /dev/videoX). both cams work alone, but not together.

  14. rblevin says:

    @Raman You need to terminate the process. In Terminal, enter ps -A and you will see motion listed with a process ID. Then type sudo kill -s SIGTERM ##### where #### = the process ID of motion.

  15. Jvrramirez says:

    Hi, I have easycap (/dev/easycap0 ) with channel, each one for each camera.
    how I can configure Motion ?
    best regards

  16. Mark2741 says:

    Anyone manage to get this work with Ubuntu 10.10 and a Logitech Quickcam Pro 4000? The green led indicator on the webcam comes on when Motion is started, but no images get saved. Also, trying to stop the motion process always results in the terminal just hanging. I'm trying the daemon now with the same result : (

  17. Mark2741 says:

    I wound up getting it to work (it works great, too!) – my problem was I had an old Hauppage WinTV PCI card in my PC and wasn't working for any app. Even though the Logitech webcam was /etc/video1 and I changed the motion.conf file to reflect that, on a hunch I just pulled the WinTV card out of my PC, rebooted, and tried Motion and it works flawlessly now.

  18. Aasdf says:

    you have to use threads. Read a bit in the documentation about it. you'll need three files.

  19. eagleapex says:

    I just set this up. There's so many options! Thanks for the intro. Now I can find out what the sneaky neighbour is up to.

  20. Johannes Kingma says:

    the sample script is the init.d script. After configuring (updating the motion.conf file according to the suggestions above) you can run sudo /etc/rc.d/motion restart or /etc/init.d/motion restart to (re) start motion. Look for a motion process with ps aux | grep motion

  21. Johannes Kingma says:

    Btw. works fine on arch with kernel 2.6.38-ARCH x86_64

  22. Cantstopengland says:

    I think this site copied your post:

  23. Guest says:

    thank you Chris! very useful and succinct explanation… just the right amt of info to get one started..

  24. Setup a webcam security system with Ubuntu and Motion | TurboLinux Blog says:

    [...] Here is a good tutorial show you how to Setup a webcam security system with Ubuntu and Motion: [...]

  25. Thalass T'Kynn says:

    It works quite nicely but i can't seem to get the live feed  over the internet. On the local network it works fine, but outside that it never connects. I've set my router to forward port 8081 to my laptop (just testing with the on-board webcam at the moment), but nothing. Must be a setting somewhere i've missed.

  26. Wayne says:

    Hi Guys

      iam running ubuntu 10.04 i have ordered a “wireless network dvr 4 channel” setup on ebay as you know it only has windows drivers.

    I do not find any linux drivers for this setup.

    It is a usb aplication that then connects either wireless or via cable to the not sure if i can call it router.

    how do i get pc to recognise this “router”?

  27. Popp0inlockin says:

    how do i quit in the terminal? i am trying everything and it won't stop

  28. Popp0inlockin says:

    how do i quit in the terminal? i am trying everything and it won't stop

  29. nikosm says:

    I i’ve managed to set it up, it works great, but now i want to stream audio to.
    Any ideas?

  30. ez says:

    yeah, started motion and saves lots of pics–Control C

    ^C[1] File of type 1 saved to: ./01-20111017081937-07.jpg
    [1] Thread exiting
    [1] Calling vid_close() from motion_cleanup
    [1] Closing video device /dev/video0
    [0] Motion terminating

  31. hanhdo says:

    @Raman: You can stop “Motion” by pressing CTRL+C.

  32. hanhdo says:

    @Raman: You can press Ctrl + C to stop motion

  33. jlevarre says:

    can someone submit an example of a motion.conf and thread1.conf
    thread2.conf for multiple camera operation.

    i have been trying for 3 months to get more than one camera working.

    thanks greatly

  34. Bird says:

    Many thanks, this was really useful.

    One quick note:
    on_picture_save wput ftp://user@pass@server %f

    should be:
    on_picture_save wput ftp://user:pass@server %f

    Some people have already mentioned, but to re-iterate motion just fails silently if you don’t have permission to write to the directory. The daemon runs as user motion so you will need to set permissions on your system correctly.

  35. kwak says:


    Can somebody please tell me where is the file in which I can setup motion start at startup of a system?
    I know that there is one file where you can write option
    start on boot=yes
    But I forget where is it ;)
    The script was starting but failed after few hours (normaly three or four ) Did anyone maybe have the same problem?

    Perhaps there is other option to start motion on boot up? Many thanks for help!!

  36. Jeff Kline says:

    Interesting piece of work! I’ve got it running in Ubuntu 10.10LTS so far without a hitch and it’s seeing two cameras from two video cards. Motion detection appears a bit strange though as though it only works on the one camera and not the other. But this is further investigation.

  37. Uhh says:

    > 5. Live feed
    > This comes working out of the box with Motion. Check out your live stream in your web browser by navigating to: http://localhost:8081

    No it doesn’t. What setting did you forget to tell us about?

  38. Craig says:

    Thanks for this guide, worked amazingly for me. I’m using this for parking security from a window the overlooks my lot in the back. It’s great with dropbox, i can check up on it anywhere, and images are only about 40k, so space isn’t an issue.

  39. Edgar says:

    My motion works 100%, correct. If anyone have some truoble email me.

  40. Reginaldo Stueckner says:

    Hi i just tried what you published, as you have published her and sorry it´s NOT working for me and wizh my cam :-(

    My USB based web-cam is okay with – cheese – and – camorama – :-) , but
    NOT with – motion – . Please take a look to this :

    # lsusb
    . . . .
    Bus 001 Device 003: ID 093a:2621 Pixart Imaging, Inc. PAC731x Trust
    Webcam . . . .

    Next is started motion on the command-line :

    # motion -s
    [0] Processing thread 0 – config file /etc/motion/motion.conf
    [0] Motion 3.2.12 Started
    [0] ffmpeg LIBAVCODEC_BUILD 3412993 LIBAVFORMAT_BUILD 3415808
    [0] Motion running in setup mode.
    [0] Thread 1 is from /etc/motion/motion.conf
    [0] Thread 1 is device: /dev/video0 input 8
    [0] Webcam port 8081
    [0] Waiting for threads to finish, pid: 5527
    [0] motion-httpd/3.2.12 running, accepting connections
    [0] motion-httpd: waiting for data on port TCP 8080
    [1] Thread 1 started
    [1] cap.driver: “pac7311″
    [1] cap.card: “USB Camera (093a:2621)”
    [1] cap.bus_info: “usb-0000:00:10.4-1.4″
    [1] cap.capabilities=0×05000001
    [1] – READWRITE
    [1] – STREAMING
    [1] Config palette index 8 (YU12) doesn’t work.
    [1] Supported palettes:
    [1] 0: PJPG (PJPG)
    [1] Unable to find a compatible palette format.
    [1] Using VIDEO_PALETTE_YUV420P palette
    [1] Using V4L1
    [1] Resizing pre_capture buffer to 1 items
    [1] Started stream webcam server in port 8081
    [1] sync error in proc 5527: Invalid argument
    [1] Raw changes: 27271 – changes after ‘EedDl’: 0 – labels: 73 – noise
    level: 82 [1] mcapture error in proc 5527: Invalid argument
    [1] Video device fatal error – Closing video device
    [1] Closing video device /dev/video0
    [1] Raw changes: 20768 – changes after ‘EedDl’: 0 – labels: 14 – noise
    level: 107 [1] Raw changes: 2011 – changes after ‘EedDl’: 0 – labels: 0
    - – noise level: 107 [1] Raw changes: 1830 – changes after ‘EedDl’: 0 -
    labels: 0 – noise level: 87 ^C[1] End of event 1
    [1] Raw changes: 0 – changes after ‘EedDl’: 0 – labels: 0 – noise
    level: 87 [1] Thread exiting
    [1] Closing webcam listen socket
    [1] Closing active webcam sockets
    [0] Threads finished
    [0] httpd – Finishing
    [0] httpd Closing
    [0] httpd thread exit
    [0] Motion terminating

    So i stopped the program by typing CTRL-C :-(
    That´s all :-( – And what can i do now ? Thanks :-)

    Reginaldo Stueckner
    D-79189 Bad Krozingen-Schmidhofen
    Tel local 0049-(0)-7633-8309125
    Tel mobil 0049-(0)-151-63633444

  41. - it feels wrong says:

    [...] setting up a security web cam, for my SO to spy on our cats while away. Tring motion As always everything built in linux,current flavour being ubuntu, I’m lookling to teak Unity, [...]

  42. Marius Reinecker says:

    @ Thalass T’Kynn,
    there is a webcam option in the config file restricting access to localhost by default. just set “webcam_localhost” to “off”

  43. spikey1973 says:

    just a silly question.

    it seems to work but i’d like to save the data as video instead of images.
    how do i set this up. there are so many setting that i kind lost my over view.

    kindest regards.


  44. maynard says:

    @kwak – you are looking for /etc/default/motion

    set the auto_start to yes… its no by default….

  45. Stan says:

    Hi guys! How can I stop this service? And eventually how can I uninstall it?

  46. Dazz says:

    If you want to run multiple cameras, your system needs to have sufficient bandwidth on the USB ports. The symptom of insufficient bandwidth is low resolution images. If there is not enough bandwidth, the Linux video drivers used by Motion force down the resolution.

    My (old) PC has USB 1.1 ports. Even with one camera (Logitech C500) I can’t get a good image. I have installed a generic PCI adapter card with 2x USB2.0 ports. With two cameras running, I get the max 1280×1024 resolution on both.

    I am using two identical cameras so the thread1.conf and thread2.conf only include one option to point each camera to the appropriate device (/dev/video0 and /dev/video1).

  47. mike says:

    Motion is great. I set up something like this on my old Debian system awhile back and now that I’ve got a Raspberry Pi (Raspbian), I’m going to dedicate that device as a webcam server. Of course, I’d forgotten just how I set it up, so thanks for this :)

  48. Peter Libman says:

    Hi, Do you think I’ll have any problems running this on a raspberry pi with the new cam board? Looks like just what I want, take photo on a movement. I’m planning to build a gizmo to catch people dumping rubbish!

  49. Motion – Motion Detection Software On Ubuntu | Danny Tsang says:

    [...] Setup a webcam security system with Ubuntu Linux and Motion [...]

Leave a Reply

Visit Other Sites!

Find me on other sites...


All entries, chronologically...

Pages List

General info about this site...