Howto Turn your old webcam into a motion-detecting security camera in Linux

If you have an old cam that you are not using, you can use it as a security cam. Control Motion is a program that monitors the video signal from one or more cameras and is able to detect if a significant part of the picture has changed; in other words, it can detect motion. The program is written in C and is made for the Linux operating system (using the video4linux interface). Motion is a command line based tool whose output can be either jpeg, ppm fies or mpeg video sequences. Motion is strictly command line driven and can run as a daemon with a rather small footprint.

What motion can do?

  • Taking snapshots of movement
  • Watch multiple video devices at the same time
  • Watch multiple inputs on one capture card at the same time
  • Live streaming webcam (using multipart/x-mixed-replace)
  • Real time creation of mpeg movies using libraries from ffmpeg
  • Take automated snapshots on regular intervals
  • Take automated snapshots at irregular intervals using cron
  • Execute external commands when detecting movement (and e.g. send SMS or email)
  • Motion tracking (camera follow motion – special hardware required)
  • Feed events to a MySQL or PostgreSQL database.
  • Feed video back to a video4linux loopback for real time viewing
  • Lots of user contributed related projects with web interfaces etc.
  • User configurable and user defined on screen display.
  • Control via browser (older versions used xml-rpc)
  • Automatic noise and threshold control
  • Motion is a daemon with low CPU consumption and small memory footprint.

Watch some examples here

Install Motion

– For ubuntu / LinuxMint and other debian based systems, you can install motion from terminal using the command:

sudo apt-get install motion

– For Fedora :

RPM for Fedora 12/13/rawhide on x86/x86-64 Available on RPM Fusion, build without DB support.
Motion and all its dependencies (ffmpeg…) are available on the RPM Fusion repository. Configure the repository and Install motion with

yum install motion

-For other linux distributions download and install the tar package.

  • ntar and uncompress the file to the place you want the program installed. Editor recommends placing the motion source file directory in /usr/local.
cd /usr/localtar -xvzf /path/to/motion-3.2.X.tar.gz
  • Before to start compiling be sure to have build essential installed:
sudo apt-get install build-essential
  • You will now have created a directory called motion-3.2.X. You can rename it to motion (mv motion-3.1.X motion). I recommend creating a symbolic link to the current version. This way you can more easily experiment with different version simply by changing the link.
ln -s motion-3.2.X motion
  • Now change to the new directory
cd motion
  • Run configure. You can start with the defaults. If you need to modify the installation parameters you can read the next section.
  • Build the code
  • Install the code, manual page, etc
make install
  • In /etc/motion/ you will find a file called motion-dist.conf. If it is the first time you install Motion – rename this file to motion.conf and edit as a minimum the settings: videodeviceinputnormfrequencywidthheight and target_dir. That should get you going.
  • Run the program. To enable more features you must modify the config file.

Using Motion:

To start motion open terminal and type :

sudo motion

After motion is installed, we have to configure Motion to save captured images in a remote server, to do that we need to install wput a command line FTP client that upload the captured photos by motion , to an remote FTP server. You can install wput by entering the following command into a terminal :

 sudo apt-get install wput

– Now you can configure Motion to use wput to upload captured photos by adding the lines bellow to motion.conf file :

gedit /etc/motion/motion.conf

And add these lines at the end of the file:

# Command to be executed when a picture (.ppm|.jpg) is saved (default: none)# The filename of the picture is appended as an argument for the command.on_picture_save wput ftp://USERNAME:PASSWORD@REMOTE SERVER %f

Now save and close.

– Finally, we will add Motion to startup automatically start with the server, to do that go to top menu System–>Preferences–>Startup applications then add a new startup program, type in a name for it and then type:


You should now be able to open a web interface for Motion now by typing in localhost:8000 from the server itself or XXX.XXX.X.XXX:8000 from another computers web browser. To view a live stream of your webcam type localhost:8001 from the server itself or XXX.XXX.X.XXX:8001 from another computers web browser. You can change settings from this interface here too, you can even make Motion generate it’s own config files to fit your setup exactly.

Result after installing motion:


Useful Links: lavrsen, infectedproject

  • blindman

    I cannot read your article. Please remove f***g google ad.


  • bk

    Ad? Lol, you see ads?

  • scribe63

    This article presumes you have a webcam that consistently work, even after several distro and or kernel updates.

    Due to the fact that, you research and buy a webcam that is compatibility with what ever distro at the moment. Then you do a distro or kernel upgrade, assuming thing would work better. The boom, your webcam that was working fine before, does not work any more because of some regression.

    It truly *****.

  • Bob

    Having a none removable advertisement in the middle of the content part of the page kind of ruins the reason for trying to reason this page – thanks

  • Web Camera Software

    Very informative post. Good reading.

  • Anonymous

    WOw thats really aswesome.Thanks for the info

  • Zinovsky

    Ads removed for this post

  • http://enteryoursiteURL... Archdevil

    I keep reminding writers of Linux articles that there are other distros than Ubuntu.

    The sudo command is not used in every distro and often does not do the trick!

  • Orlsend

    Great Article!

    Lol it was less than 5 min before I made my netbook a awesome CCTV camera! Now I can check my dorm room when I want thanks to SSH!

    Dont let these Slackware and Arch Zealots ruin your work. because you did a great one.

  • Ashish

    Wow…very nice article…I liked it. I will teach it to students in school to create exicitement abt Linux…..Thank you.

  • Nadder

    Great article! Thanks!!

  • M

    Thx will try, and about the kernel thing breaking it.
    So what just fix it again thats what drives the most of us anyway ;)

  • J S

    the last line for motion.conf looks wrapped on the blog, where is the break after the # comment? "wput http://ftp…" should be its own line?

    What are the steps to command line the auto-start? I’m thinking of putting this together on an old desktop that would probably run ubuntu server only and not have a gui.

  • aliahmad omid

    how to turn motion of please

  • Zinovsky

    To stop motion, open terminal and type :
    [code]sudo /etc/init.d/motion stop[/code]


    Do you recognize that this is correct time to receive the credit loans, which can make your dreams come true.

  •ückner/100002175459906 Reginaldo Stückner

    Hi i just tried what you published, in April 2012 (!) and it´s NOT working :-(

    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=0x05000001
    [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 ?

  • Optimist

    hi Reginaldo,

    Actually your motion is running in non daemon mode.. edit your motion.conf file and set daemon on

  • Pingback: Linux | Pearltrees()

  • The Tux Crusader

    Can Motion be used to also execute another command when it picks up on movement.

  • Pingback: Howto Turn your old webcam into a motion-detecting security camera in Linux | Unixmen | AmorphousContinuum()