MKV concatenate – merge

Recommended way is to use mkvtoolnix

sudo apt-get install mkvtoolnix mkvtoolnix-gui

  • Start mmg (graphical user interface for mkvmerge)
  • "Add" the first file
  • "Append" the second one, third, fourth, …
  • Set output name in the textbox at the bottom
  • "Start muxing"

Subtitles are preserved and properly concatenated, so are audio and video. Chapters can be edited in the resulting file with mmg.

This procedure creates a properly muxed file. Any problems should be mentioned in the "log"-window in mmg. You can even set and name audio and subtitle tracks with the correct language codes and Annotations like "Forced" or "Directors Commentary".

If you plan to do this repeatedly on different files you better use the command line version mkvmerge("mkvmerge –help")

mkvmerge -o output.mkv input1.mkv +input2.mkv

Script

# update permissions

find /local/apps/ -type d -exec chmod o+rx {} ";"

find /local/apps/ -type f -exec chmod o+r {} ";"

Result of FIND placed in {}

-type f=file d=directory

Terminate cmd ";"

Concatenate MP4 files

Ubuntu CLI

If you are looking to combine MP4 files from the command line here is the easiest solution. First you need to use the following command to install the program.

Once installed you will be able to run the following command to join the files.

You should now be able to combine MP4 files from the command line, and pretty quick at that.


Samsung Smart TVs speak DLNA-UPnP – uShare

http://askubuntu.com/questions/38021/how-to-add-a-ppa-on-a-server

sudo apt-get install python-software-properties

=====================================

Using PLEX: http://www.plexapp.com/ because it works on the Samsung tv.

=====================================

https://help.ubuntu.com/community/MiniDLNA

http://www.slsmk.com/how-to-install-ps3-media-server-to-ubuntu-in-headless-mode/

Quick Tip: DLNA Server on Linux (Ubuntu 12.04 Precise Pangolin)

My Samsung Smart TVs speak DLNA, my Android phones and tablets speak DLNA – time to teach my small headless Ubuntu home server to speak it as well.

It seems that most people are using uShare, but I personally like MiniDLNA (aka ReadyDLNA) better. On Precise, just install the package

# apt-get install minidlna

and create directories for Music, Pictures and Videos, for example:

# tree /dlna//dlna/

├── Music

├── Pictures

└── Videos

3 directories, 0 files

Drop your files into those directories and add some basic configuration to MiniDLNA:

# grep -o "^[a-z].*" /etc/minidlna.conf

port=8200

media_dir=A,/dlna/Music

media_dir=P,/dlna/Pictures

media_dir=V,/dlna/Videos

friendly_name=DLNA

Serverinotify=yes

enable_tivo=no

strict_dlna=no

notify_interval=900

serial=12345678

model_number=1

Now just force-reload MiniDLNA (force-reload rescans the folders)

# /etc/init.d/minidlna force-reload

and that should be it! The server now shows up on my Samsung Smart TV and on my Galaxy SIII:

http://2.bp.blogspot.com/-io13OaD2_Do/T-hKjO3SeCI/AAAAAAAAA6c/xmhW2g2ACaE/s320/Screenshot_2012-06-25-21-20-43.png

From there, I can browse my files.

# /etc/init.d/minidlna stop

# /etc/init.d/minidlna start

Samsung Smart TVs speak DLNA – uShare

http://askubuntu.com/questions/38021/how-to-add-a-ppa-on-a-server

sudo apt-get install python-software-properties

http://www.slsmk.com/how-to-install-ps3-media-server-to-ubuntu-in-headless-mode/

How To install Media Server to Ubuntu in Headless mode

PS3 media server is now my go to application for trancoding and playing media around the house since development on Mediatomb came to a halt. And this PS3 Media Server is sweet. So far it plays anything I throw at it on the Ps3 and the Sony NMP-200 I use around the house.

P.M.S. was meant to run in a GUI display. So there are a few extra things we need to do so that it runs on a Ubuntu Server in headless mode (without a GUI). After all, who needs a GUI on a server anyway… Since PMS version 1.81.0, I’ve stopped using the manual installation and instead am using the repository. It makes for an easier install and updates are handled via apt-get.

I’ve used this on Ubuntu 12.04.

We will need a few pre-reqs:

apt-get install mplayer mencoder ffmpeg openjdk-6-jre

Let’s add the repository from which we will install the PMS and run the installation.

sudo add-apt-repository ppa:happy-neko/ps3mediaserver
sudo apt-get update
sudo apt-get install ps3mediaserver

Next we create a system user account for the PMS to use. By default, PMS runs under the root account, but it’s always a good idea to keep things as secure as possible.

sudo adduser --system --group ps3mediauser

Now lets edit the PMS config file and setup the custom user and set it to autorun on startup. Edit /etc/default/ps3mediaserver, locate the following items in the file and make the changes as indicated.

PMS_START="yes"
PMS_USER="ps3mediauser"
PMS_PROFILE="/home/$PMS_USER/.config/ps3mediaserver"

Next, we need to create a PMS.conf file in the ps3mediauser home directory. You will need to run these commands as root since the ps3mediauser account is a system only account. Copy in the default PMS.conf file.

su -i
mkdir -p /home/ps3mediauser/.config/ps3mediaserver
cp /etc/skel/.config/ps3mediaserver/PMS.conf /home/ps3mediauser/.config/ps3mediaserver

Next you should edit the PMS.conf file and change the ‘folders’ to reflect the location of your Media files. The other settings can be left at default for the most part, but it doesn’t hurt to familiarize yourself with whats available.

folders =/mnt/media/tv,/mnt/media/movies,/mnt/media/music

With all the settings in place, you need to change ownership of the ps3mediauser home folder:

chown -R ps3mediauser:ps3mediauser /home/ps3mediauser

Last thing to check is the /tmp/ folder. PMS will use this folder and if the ‘root’ user had stuff in that folder related to PMS, the non-root user will have issues running the service. It doesn’t hurt to remove any older remnants.

rm -F /tmp/jna
rm -F /tmp/ps3*
rm -F /tmp/hsperfdata*

Some people have reported that they needed to change permissions in /tmp/. I’ve never had to do that, but it is something to keep in mind.

Lastly, just start the service:

sudo start ps3mediaserver

Tagged with: no tags.

=========================================

http://www.conrad.id.au/2012/06/quick-tip-dlna-server-on-linux-ubuntu.html

How to Install Oracle Java 7 on Ubuntu Linux






This tutorial will cover the installation of 32-bit and 64-bit Oracle Java 7 (currently version number 1.7.0_25) JDK/JRE on 32-bit and 64-bit Ubuntu operating systems. These instructions will also work on Debian and Linux Mint.

If you already have Oracle Java 7 installed on your system, using this method but need to upgrade.

For those who only want to install Oracle Java JRE for running Java applications and not to develop Java programs.

For those who want to install Oracle Java JDK to develop Java programs and applications the Oracle JDK with the Oracle Java JRE included in the Oracle JDK.

Also to enable/upgrade the Oracle Java in your web browsers.

EditSteps

  1. 1
    Check to see if your Ubuntu Linux operating system architecture is 32-bit or 64-bit, open up a terminal and run the following command below.
    • Type/Copy/Paste: file /sbin/init
      • Note the bit version of your Ubuntu Linux operating system architecture it will display whether it is 32-bit or 64-bit.
  2. 2
    Check if you have Java installed on your system. To do this, you will have to run the Java version command from terminal.
    • Open up a terminal and enter the following command:
      • Type/Copy/Paste: java -version
    • If you have OpenJDK installed on your system it may look like this:
      • java version "1.7.0_15"
        OpenJDK Runtime Environment (IcedTea6 1.10pre) (7b15~pre1-0lucid1)
        OpenJDK 64-Bit Server VM (build 19.0-b09, mixed mode)
    • If you have OpenJDK installed on your system, you have the wrong vendor version of Java installed for this exercise.
  3. 3
    Completely remove the OpenJDK/JRE from your system and create a directory to hold your Oracle Java JDK/JRE binaries. This will prevent system conflicts and confusion between different vendor versions of Java. For example, if you have the OpenJDK/JRE installed on your system, you can remove it by typing the following at the command line:
    • Type/Copy/Paste: sudo apt-get purge openjdk-\*
      • This command will completely remove OpenJDK/JRE from your system
    • Type/Copy/Paste: sudo mkdir -p /usr/local/java
      • This command will create a directory to hold your Oracle Java JDK and JRE binaries.
  4. 4
    Download the Oracle Java JDK/JRE for Linux. Make sure you select the correct compressed binaries for your system architecture 32-bit or 64-bit (which end in tar.gz).
    • For example, if you are on Ubuntu Linux 32-bit operating system download 32-bit Oracle Java binaries.
    • For example, if you are on Ubuntu Linux 64-bit operating system download 64-bit Oracle Java binaries.
    • Optional, Download the Oracle Java JDK/JRE Documentation
      • Select jdk-7u25-apidocs.zip
    • Important Information: 64-bit Oracle Java binaries do not work on 32-bit Ubuntu Linux operating systems, you will receive multiple system error messages, if you attempt to install 64-bit Oracle Java on 32-bit Ubuntu Linux.
  5. 5
    Copy the Oracle Java binaries into the /usr/local/java directory. In most cases, the Oracle Java binaries are downloaded to: /home/"your_user_name"/Downloads.
    • 32-bit Oracle Java on 32-bit Ubuntu Linux installation instructions:
      • Type/Copy/Paste: cd /home/"your_user_name"/Downloads
      • Type/Copy/Paste: sudo cp -r jdk-7u25-linux-i586.tar.gz /usr/local/java
      • Type/Copy/Paste: sudo cp -r jre-7u25-linux-i586.tar.gz /usr/local/java
      • Type/Copy/Paste: cd /usr/local/java
    • 64-bit Oracle Java on 64-bit Ubuntu Linux installation instructions:
      • Type/Copy/Paste: cd /home/"your_user_name"/Downloads
      • Type/Copy/Paste: sudo cp -r jdk-7u25-linux-x64.tar.gz /usr/local/java
      • Type/Copy/Paste: sudo cp -r jre-7u25-linux-x64.tar.gz /usr/local/java
      • Type/Copy/Paste: cd /usr/local/java
  6. 6
    Unpack the compressed Java binaries, in the directory /usr/local/java
    • 32-bit Oracle Java on 32-bit Ubuntu Linux installation instructions:
      • Type/Copy/Paste: sudo tar xvzf jdk-7u25-linux-i586.tar.gz
      • Type/Copy/Paste: sudo tar xvzf jre-7u25-linux-i586.tar.gz
    • 64-bit Oracle Java on 64-bit Ubuntu Linux installation instructions:
      • Type/Copy/Paste: sudo tar xvzf jdk-7u25-linux-x64.tar.gz
      • Type/Copy/Paste: sudo tar xvzf jre-7u25-linux-x64.tar.gz
  7. 7
    Double-check your directories. At this point, you should have two uncompressed binary directories in /usr/local/java for the Java JDK/JRE listed as:
    • Type/Copy/Paste: ls -a
    • jdk1.7.0_25
    • jre1.7.0_25
  8. 8
    Edit the system PATH file /etc/profile and add the following system variables to your system path. Use nano, gedit or any other text editor, as root, open up /etc/profile.
    • Type/Copy/Paste: sudo gedit /etc/profile
    • or
    • Type/Copy/Paste: sudo nano /etc/profile
  9. 9
    Scroll down to the end of the file using your arrow keys and add the following lines below to the end of your /etc/profile file:
    • Type/Copy/Paste:

      JAVA_HOME=/usr/local/java/jdk1.7.0_25
      PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
      JRE_HOME=/usr/local/java/jre1.7.0_25
      PATH=$PATH:$HOME/bin:$JRE_HOME/bin
      export JAVA_HOME
      export JRE_HOME
      export PATH
  10. 10
    Save the /etc/profile file and exit.
  11. 11
    Inform your Ubuntu Linux system where your Oracle Java JDK/JRE is located. This will tell the system that the new Oracle Java version is available for use.
    • Type/Copy/Paste: sudo update-alternatives –install "/usr/bin/java" "java" "/usr/local/java/jre1.7.0_25/bin/java" 1
      • this command notifies the system that Oracle Java JRE is available for use
    • Type/Copy/Paste: sudo update-alternatives –install "/usr/bin/javac" "javac" "/usr/local/java/jdk1.7.0_25/bin/javac" 1
      • this command notifies the system that Oracle Java JDK is available for use
    • Type/Copy/Paste: sudo update-alternatives –install "/usr/bin/javaws" "javaws" "/usr/local/java/jre1.7.0_25/bin/javaws" 1
      • this command notifies the system that Oracle Java Web start is available for use
  12. 12
    Inform your Ubuntu Linux system that Oracle Java JDK/JRE must be the default Java.
    • Type/Copy/Paste: sudo update-alternatives –set java /usr/local/java/jre1.7.0_25/bin/java
      • this command will set the java runtime environment for the system
    • Type/Copy/Paste: sudo update-alternatives –set javac /usr/local/java/jdk1.7.0_25/bin/javac
      • this command will set the javac compiler for the system
    • Type/Copy/Paste: sudo update-alternatives –set javaws /usr/local/java/jre1.7.0_25/bin/javaws
      • this command will set Java Web start for the system
  13. 13
    Reload your system wide PATH /etc/profile by typing the following command:
    • Type/Copy/Paste: . /etc/profile
    • Note your system-wide PATH /etc/profile file will reload after reboot of your Ubuntu Linux system
  14. 14
    Test to see if Oracle Java was installed correctly on your system. Run the following commands and note the version of Java:
  15. 15
    A successful installation of 32-bit Oracle Java will display:
    • Type/Copy/Paste: java -version
      • This command displays the version of java running on your system
    • You should receive a message which displays:
      • java version "1.7.0_25"
        Java(TM) SE Runtime Environment (build 1.7.0_25-b25)
        Java HotSpot(TM) Server VM (build 23.1-b03, mixed mode)
    • Type/Copy/Paste: javac -version
      • This command lets you know that you are now able to compile Java programs from the terminal.
    • You should receive a message which displays:
      • javac 1.7.0_25
  16. 16
    A successful installation of Oracle Java 64-bit will display:
    • Type/Copy/Paste: java -version
      • This command displays the version of java running on your system
    • You should receive a message which displays:
      • java version "1.7.0_25"
        Java(TM) SE Runtime Environment (build 1.7.0_25-b25)
        Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)
    • Type/Copy/Paste: javac -version
      • This command lets you know that you are now able to compile Java programs from the terminal.
    • You should receive a message which displays:
      • javac 1.7.0_25
  17. 17
    Congratulations, you just installed Oracle Java on your Linux system. Now reboot your Ubuntu Linux system. Afterwards, your system will be fully configured for running and developing Java programs. Later on you may want to try compiling and running your own Java programs by following this article How to Create your First Java Program on Ubuntu Linux

EditOptional: How to enable Oracle Java in your Web Browsers

  1. 1
    To enable your Java plug-in in your web browsers you must make a symbolic link from the web browsers plug-in directory to the location of the Java plug-in included in your distribution of Oracle Java.

Important Note: I would urge caution when enabling Oracle Java 7 in your web browsers, due to the fact there have been many numerous security flaws and exploits. Essentially, by enabling Oracle Java 7 in your web browsers if a security flaw or exploit is discovered this is how the bad guys break in and compromise your system. For more information on security flaws and exploits in Java see the following website: Java Tester

EditGoogle Chrome

32-bit Oracle Java instructions:

  1. 1
    Issue the following commands.
    • Type/Copy/Paste: sudo mkdir -p /opt/google/chrome/plugins
      • this will create a directory called /opt/google/chrome/plugins
    • Type/Paste/Copy: cd /opt/google/chrome/plugins
      • this will change you into the google chrome plugins directory,make sure you are in this directory before you make the symbolic link
    • Type/Paste/Copy: sudo ln -s /usr/local/java/jre1.7.0_25/lib/i386/libnpjp2.so
      • this will create a symbolic link from the Java JRE( Java Runtime Environment ) plugin libnpjp2.so to your Google Chrome web browser

64-bit Oracle Java instructions:

  1. 1
    Issue the following commands.
    • Type/Copy/Paste: sudo mkdir -p /opt/google/chrome/plugins
      • this will create a directory called /opt/google/chrome/plugins
    • Type/Paste/Copy: cd /opt/google/chrome/plugins
      • this will change you into the google chrome plugins directory,make sure you are in this directory before you make the symbolic link
    • Type/Paste/Copy: sudo ln -s /usr/local/java/jre1.7.0_25/lib/amd64/libnpjp2.so
      • this will create a symbolic link from the Java JRE( Java Runtime Environment ) plugin libnpjp2.so to your Google Chrome web browser

Reminders:

  1. 1
    Note: Sometimes when you issue the above command you may receive a message which states:
    • ln: creating symbolic link `./libnpjp2.so': File exists
    • To correct this issue simply remove the previous symbolic link using the following command:
    • Type/Copy/Paste: cd /opt/google/chrome/plugins
    • Type/Copy/Paste: sudo rm -rf libnpjp2.so
    • Make sure you are in the /opt/google/chrome/plugins directory before you issue the command
  2. 2
    Restart your web browser and go to Java Tester to test if Java is functioning in your web browser.

EditMozilla Firefox

32-bit Oracle Java instructions:

  1. 1
    Issue the following commands.
    • Type/Paste/Copy: cd /usr/lib/mozilla/plugins
      • this will change you into the directory /usr/lib/mozilla/plugins, create this directory if you do not have it
    • Type/Paste/Copy: sudo mkdir -p /usr/lib/mozilla/plugins
      • this will create the directory /usr/lib/mozilla/plugins, make sure you are in this directory before you make the symbolic link
    • Type/Paste/Copy: sudo ln -s /usr/local/java/jre1.7.0_25/lib/i386/libnpjp2.so
      • this will create a symbolic link from the Java JRE( Java Runtime Environment ) plugin libnpjp2.so to your Mozilla Firefox web browser

64-bit Oracle Java instructions:

  1. 1
    Issue the following commands.
    • Type/Paste/Copy: cd /usr/lib/mozilla/plugins
      • this will change you into the directory /usr/lib/mozilla/plugins, create this directory if you do not have it
    • Type/Paste/Copy: sudo mkdir -p /usr/lib/mozilla/plugins
      • this will create the directory /usr/lib/mozilla/plugins, make sure you are in this directory before you make the symbolic link
    • Type/Paste/Copy: sudo ln -s /usr/local/java/jre1.7.0_25/lib/amd64/libnpjp2.so
      • this will create a symbolic link from the Java JRE( Java Runtime Environment ) plugin libnpjp2.so to your Mozilla Firefox web browser

Reminders:

  1. 1
    Note: Sometimes when you issue the above command you may receive a message which states:
    • ln: creating symbolic link `./libnpjp2.so': File exists
    • To correct this issue simply remove the previous symbolic link using the following command:
    • Type/Copy/Paste: cd /usr/lib/mozilla/plugins
    • Type/Copy/Paste: sudo rm -rf libnpjp2.so
    • Make sure you are in the /usr/lib/mozilla/plugins directory before you issue the command
  2. 2
    Restart your web browser and go to Java Tester to test if Java is functioning in your web browser.

EditTips

  • With Ubuntu Linux, you have the choice of whether to use OpenJDK, which is a free and open-source implementation of the Java programming language, or to use Oracle Java JDK and JRE. Some prefer to use Oracle Java (as it is the most up-to-date version of Java and it comes directly from the maintainers of Java technology), but this varies.
  • Please note that this document is in continual revision because Oracle sometimes changes the installation method of their Java JDK/JRE binaries.
  • Keep in mind that Oracle makes security upgrades and bug fixes and enhances performance issues for each new release of Oracle Java. When installing Oracle Java on your system, be aware of the version number changes. See How to Upgrade Oracle Java on Ubuntu Linux for more information.

Setting Up the NFS Client



Step One—Download the Required Software

Start off by using apt-get to install the nfs programs.
apt-get install nfs-common portmap


Step Two—Mount the Directories

Once the programs have been downloaded to the the client server, create the directories that will contain the NFS shared files
mkdir -p /mnt/nfs/fs-01.sda1

mkdir -p /mnt/nfs/fs-01.sdb1

prevent accedent writes:

sudo chmod 000 /mnt/nfs/fs-01.sda1

sudo chmod 000 /mnt/nfs/fs-01.sdb1

sudo mount fileshare-01:/mnt/local/sda1/ /mnt/nfs/fs-01.sda1/

You can use the df -h command to check that the directories have been mounted. You will see them last on the list.

df -h
Filesystem             Size  Used Avail Use% Mounted on/dev/sda                20G  948M   19G   5% /udev                   119M  4.0K  119M   1% /devtmpfs                   49M  208K   49M   1% /runnone                   5.0M     0  5.0M   0% /run/locknone                   122M     0  122M   0% /run/shmfileshare-01:/home      20G  948M   19G   5% /mnt/nfs/homefileshare-01:/var/nfs   20G  948M   19G   5% /mnt/nfs/var/nfs

Additionally, use the mount command to see the entire list of mounted file systems.
mount

Your list should look something like this:
/dev/sda on / type ext4 (rw,errors=remount-ro,barrier=0) [DOROOT]proc on /proc type proc (rw,noexec,nosuid,nodev)sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)none on /sys/fs/fuse/connections type fusectl (rw)none on /sys/kernel/debug type debugfs (rw)none on /sys/kernel/security type securityfs (rw)udev on /dev type devtmpfs (rw,mode=0755)devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620)tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755)none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880)none on /run/shm type tmpfs (rw,nosuid,nodev)rpc_pipefs on /run/rpc_pipefs type rpc_pipefs (rw)fileshare-01:/home on /mnt/nfs/home type nfs (rw,vers=4,addr= fileshare-01,clientaddr=192.168.1.xxx)fileshare-01:/var/nfs on /mnt/nfs/var/nfs type nfs (rw,vers=4,addr=12.34.56.78,clientaddr=192.168.1.xxx)


Testing the NFS Mount

Once you have successfully mounted your NFS directories, you can test that they work by creating files on the Client and checking their availability on the Server.

Create a file in each directory to try it out:
touch /mnt/nfs/fs-01.sda1/test.txt
touch /mnt/nfs/fs-01.sdb1/test.txt

You should then be able to find the files on the Server in the /home and /var/nfs directories.
ls /home

ls /mnt/nfs/


You can ensure that the mount is always active by adding the directories to the fstab file on the client. This will ensure that the mounts start up after the server reboots.
vi /etc/fstab

fileshare-999:/mnt/local/sdx1/test/ /mnt/nfs/fs-99.sdx1.test/ nfs4 _netdev,auto,hard,intr,retry=1,nolock,bg 0 0

fileshare-01:/mnt/local/sda1/ /mnt/nfs/fs-01.sda1/ nfs4 _netdev,auto,retry=1,nolock,bg,intr 0 0

You can learn more about the fstab options by typing in:

man nfs

Any subsequent restarts will include the NFS mount—although the mount may take a minute to load after the reboot You can check the mounted directories with the two earlier commands:
df -h

mount


Removing the NFS Mount

Should you decide to remove a directory, you can unmount it using the umount command:

sudo umount /directory name


You can see that the mounts were removed by then looking at the filesystem again.
df -h


You should find your selected mounted directory gone.

Linux RunAs uid/gid

http://blog.teksol.info/pages/daemontools/tutorial

http://thedjbway.b0llix.net/daemontools/uidgid.html

http://manpages.ubuntu.com/manpages/precise/man8/setuidgid.8.html

Your environment variables will contain passwords, and you don’t want those files to be world-readable. Set your environment’s directory permissions to 0700, and the individual files to 0600. Then, ensure you always setuidgid last in your run scripts.

sudo apt-get install daemontools

### vi touch1.sh ============================

#!/bin/sh
exec 2>&1

exec setuidgid www-data touch /mnt/nfs/fs-01.sdb1/www.data1.log

### vi touch2.sh ============================

#!/bin/sh
exec 2>&1

exec setuidgid www-data sh -c 'echo "hello world" > /mnt/nfs/fs-01.sdb1/www.data2.log'

### vi touch3.sh ============================

#!/bin/sh
exec 2>&1

sh ./touch1.sh
sh ./touch2.sh

How-to-run-a-Java-Application-from-Bash-Script

Link two private networks using a VPN on Ubuntu Linux machines each behind NAT

http://blog.peter-b.org/2010/12/16/link-two-private-networks-using-a-vpn-on-ubuntu-linux-machines-each-behind-nat/

Link two private networks using a VPN on Ubuntu Linux machines each behind NAT

Might seem like a stretch, but clearly given the number of people asking the question out on the internet this is common enough. And I wanted to do it, so…..

The scenario. You have two private , home.local which is 192.168.0.0 netmask 255.255.255.0 and office.local numbered 192.168.1.0 netmask 255.255.255.0. They’re regular private connected to the internet via ADSL with routers of some description or other – could be the freebies that come from the service provider, or something you bought or built yourself. Either way it’s likely to be running NAT at the very least, possibly with some firewall as well, but for whatever reason, you can’t make these do the for you (if you can, you should – it’s by far the simplest route).

On each network there’s an machine that’s on all the time for whatever reason, and you figure you can use these to connect the two networks together somehow.

Good news, you can.

Please remember that connecting two networks like this has security issues. Unless you also run firewalls on the Ubuntu machines that run the VPN each network is only as secure as the other, so if one network was “more secure” than the other suddenly it will become less secure! Make sure that you own both networks, or if you don’t that you have the permission of whoever owns the remote network to do this and that you trust the remote network not to connect to your home Samba share and steal or delete all your music (but of course, you set up Samba so that can’t happen, right?).

Also note that if the two networks are not numbered differently – i.e. they’re both 192.168.0.0 netmask 255.255.255.0 – then you’re going to run into a whole mess of trouble. If this is the case, consider looking at a “Bridged VPN” that will make it seem like one big network (though if you do you’re going to run into a real whole mess of trouble as IP addresses must be unique across both networks!). Bridged VPNs are a whole other topic, and not covered here.

There are plenty of sites that will tell you how to set up the actual VPN, for example here and here to name just two. But they don’t really talk about anything other than the actual VPN itself and the basic between the two machines.

First things first, you need some way to look up the external IP of each network. This means registering with some kind of DNS service. If you’re lucky enough to have your own server out on the internet you can use a script to find out what your external IP is and use nsupdate to register this with your DNS server on the internet.

If not, there are plenty of “Dynamic DNS” services out there that can do this.

Ok, so you have your networks successfully registering with a DNS server somewhere out on the internet. Now you can create the actual VPN.

I like Sébastian Wains’ solution best as it doesn’t require configuration of the routers to open NAT ports etc. (but you might need to set some routes, more later).

So, the setup so far:

  • Private Network 192.168.0.0
  • Local DNS suffix is home.local
  • Ubuntu box is 192.168.0.23 and called ubuntubox.home.local
  • Registers with DynDNS as myhomenetwork.dyndns.org
  • Private Network 192.168.1.0
  • Local DNS is office.local
  • Ubuntu box is DHCP assigned IP (today it’s 192.168.1.57) and called myubuntu.office.local
  • Registers with a private dns service as office.mycompany.com

The private IP addresses of the machines don’t matter too much for the VPN configuration but it’s handy to know what they are for testing and diagnostic purposes and you will probably need them for routing purposes later.

The following assumes you have root access; use sudo/su as required.

First, you need to install openvpn by running the following on both computers:

apt-get install openvpn

Then you need a pre-shared key for securing the VPN. On ubuntubox.home.local run:

openvpn --genkey --secret /etc/openvpn/static.key

You need to copy this file from one machine to the other some way. They must be identical on both machines. Best not to use email! Cut and paste over an SSH link or copy using a USB key or something similar.

Once this is done you can create the actual VPN:

On computer ubuntubox.home.local in /etc/openvpn/office.mycompany.com.conf:

remote office.mycompany.com
float
port 8000
dev tun
ifconfig 10.0.0.2 10.0.0.1
persist-tun
persist-local-ip
persist-remote-ip
comp-lzo
ping 15
secret /etc/openvpn/static.key
route 192.168.1.0 255.255.255.0
chroot /var/empty
user nobody
group nogroup # Nobody on RedHat, but we're on Ubuntu, right?
log vpn.log
verb 1

On computer myubuntu.office.local in /etc/openvpn/myhomenetwork.dyndns.org.conf:

remote myhomenetwork.dyndns.org
float
port 8000
dev tun
ifconfig 10.0.0.1 10.0.0.2
persist-tun
persist-local-ip
persist-remote-ip
comp-lzo
ping 15
secret /etc/openvpn/static.key
route 192.168.0.0 255.255.255.0
chroot /var/empty
user nobody
group nogroup # Nobody on RedHat, but we're on Ubuntu, right?
log vpn.log
verb 1

You’ll need to create ‘/var/empty’ if it doesn’t already exist. Then restart the VPN on each machine:

/etc/init.d/openvpn restart

At this point you should be able to ping each machine from the other, for example:

ubuntubox# ping -c1 192.168.1.57
PING 192.168.1.57 (192.168.1.57) 56(84) bytes of data.
64 bytes from 192.168.1.57: icmp_seq=1 ttl=64 time=100 ms
--- 192.168.1.57 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 100.456/100.456/100.456/0.000 ms

As you can see, all works great. If it doesn’t work, you should at least be able to ping both 10.0.0.1 and 10.0.0.2 from both machines. These are the addresses at the ends of the VPN tunnel. If that doesn’t work, then the tunnel doesn’t even exist. Check /etc/openvpn/vpn.log to find out why, often it’s because it can’t reach the remote host (lookup of office.mycompany.com for example).

If you can ping 10.0.0.1 and 10.0.0.2 but can’t ping the IP of the remote machine then it’s the routes that are broken. These are set up automatically thanks to the “route 192.168.0.0 255.255.255.0″ and “route 192.168.1.0 255.255.255.0″ lines in each of the configuration files. Check these first and restart the VPN if you need to make changes.

The routes should look something like:

ubuntubox# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
10.0.0.1        0.0.0.0         255.255.255.255 UH        0 0          0 tun0
192.168.1.0     10.0.0.1        255.255.255.0   UG        0 0          0 tun0
192.168.0.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0
0.0.0.0         192.168.0.254   0.0.0.0         UG        0 0          0 eth0

This shows that anything destined for ip address 10.0.0.1 (the remote end of the VPN tunnel) will go down the tun0 interface, anything destined for network 192.168.1.0 (the office.local subnet) will be directed to 10.0.0.1 (therefore down the tunnel), anything for 192.168.0.0 (the home.local subnet, the one we’re actually connected to) will go out the local ethernet port, and that anything else (0.0.0.0) will go to whatever is connected to 192.168.0.254 – this ideally is the address of your ADSL router or similar device.

If you can ping both 10.0.0.x addresses but can’t ping the other machine’s IP address then check your /etc/openvpn config files that the “route” lines are correct, and check that there aren’t conflicting routes configured somewhere else on your machines.

So now that’s all working. But what about all the other machines on the network? You want everything on each side to reach everything else no?

Well, yes. So first off, you need to turn on IP forwarding on both machines. By default this is turned off, specifically to prevent information from one network “polluting” another.

To do this, on each machine type:

sysctl -w net.ipv4.ip_forward=1

To make this permanent across reboots you also need to edit /etc/sysctl.conf on both machines and make sure that there’s a line that looks like:

net.ipv4.ip_forward=1

Often this is simply commented out, though sometimes it says ‘=0′ rather than ‘=1′.

The last bit is tricky as a lot depends on your default routers. Remember that in ‘netstat -rn’ there’s a last route that looks like ’0.0.0.0′ (this is sometimes labelled ‘default’)? This is the IP address of the device that receives anything not destined for any of the other routes. Chances are that everything on your network sends anything not destined for the local private network to this IP address.

You have two choices at this point:

  1. You need to configure whatever device is using that IP address with two ‘static routes’ for the remote network and the tunnel address. Most routers will let you do this in their web interface.
  2. Configure everything on each network to use the local Ubuntu box as the default route – this is usually specified in the settings of your DHCP server as the “default gateway”, or possibly individually in the network configuration of every device on your network.

Option 1 is the simplest, assuming the router has the ability to let you do it. Option 2 may be easier but is not ideal, as everything will rely on your Ubuntu box for their internet connection and your Ubuntu box will be working a bit harder!

Remember, you need to do this on both networks, and you need to set routes for 192.168.x.0/255.255.255.0, 10.0.0.1 and 10.0.0.2. Why?

Assume all the routes are properly configured:

Let’s say machine 192.168.0.16 wants to ping 192.168.1.12. The default route on network 192.168.0.0 is for whatever is connected to 192.168.0.254 (and 192.168.1.0 it’s 192.168.1.254).

Assuming everything is set up correctly 192.168.0.16 will send a packet destined for 192.168.1.12 to 192.16.0.254 – the default route. This device knows that it must forward anything for 192.168.1.x to your Ubuntu box as you gave it a static route. So, it then forwards the packet for 192.168.1.12 on to 192.168.0.23 – your Ubuntu box ubuntubox.home.local.

Thanks to the VPN your Ubuntu box knows to send this packet down the tunnel to 10.0.0.1. The office Ubuntu box then forwards this on directly to 192.168.1.12, however at this point the originating IP address of the packet is no longer 192.168.0.16, it’s 10.0.0.2 – the IP address of the end of the tunnel on ubuntubox.home.local! So when 192.168.1.12 receives the packet and wants to send a reply, it’s going to send it to 10.0.0.2. This is why you need the extra static routes. Use ‘tcpdump’ to watch ping requests across the VPN and you’ll see what I mean.

In my case I chose option 1. So, I logged on to the routers 192.168.0.254 and set up a static route for 192.168.1.0 netmask 255.255.255.0 to point to 192.168.0.23 and for 10.0.0.1 netmask 255.255.255.255 to 192.168.0.23.

Then I logged on to the router at 192.168.1.254 and set up a static route for 192.168.0.0 netmask 255.255.255.0 to point to myubuntu.office.local and for 10.0.0.2 netmask 255.255.255.255 to myubuntu.office.local. I see you noticed the hostnames. Why the hostname and not the IP? Remember, myubuntu.office.local is configured using DHCP and so the IP address may change. If your router forces you to use the IP address make sure it’s a static IP address and not assigned via DHCP, as if the address changes for some reason your shiny VPN will break!

If what you’re configuring doesn’t like the host specific 10.0.0.x routes you can specify a route of 10.0.0.0 netmask 255.255.255.0 (yes I know this is bad behaviour, but it works) – this will obviously cause problems if you have anything that uses the 10.0.0.0 subnet! You can change these addresses and network numbers to suit your requirements though, just remember to change them in both config files and both default routers!

That’s it. You can now access any IP on either network from any IP on the other network.

Of course, you now have the issue of DNS. Set up a forwarding zone on the DNS server on each network using the IP address of the DNS server on the remote network in each case so that requests for the remote network are forwarded to the DNS server for the remote network. But that’s a whole other topic!

This entry was posted in Technical Tirades and tagged , , , by Peter. Bookmark the permalink.

3 thoughts on “Link two private networks using a VPN on Ubuntu Linux machines each behind NAT

  1. David Valentine on September 3, 2011 at 5:45 am said:

    I just tried out this guide today, and I was successful in connecting my two home networks together. Thanks for the well written advice!

    Two minor mistakes that I found were:
    – Left out .conf at the end of the line: “On computer myubuntu.office.local in /etc/openvpn/myhomenetwork.dyndns.org:” ie. it should read: “On computer myubuntu.office.local in /etc/openvpn/myhomenetwork.dyndns.org.conf:”
    – A config thing really, in the latest version of Ubuntu 11.04 there is no /var/empty directory. So you need to run the command “sudo mkdir /var/empty” on each server.

    Thanks again for the detailed guide. Now I can connect to my samba share and printers from either of my home networks!

    ~David

  2. Pingback: David and Claire’s Blog»Blog Archive » Link two private networks using a VPN on Ubuntu Linux machines each behind NAT

Also see: http://www.debian-administration.org/articles/35

YOUTUBE: http://www.youtube.com/watch?v=C_B9k0l6kEs


Tag Cloud