Setup Ipython notebook on a remote server and how to connect to it

Post date: Jul 10, 2015 9:05:42 PM

In this post, I show how to setup remote machine to run IPython notebook (on Ubuntu) and how to connect to it from a local machine (my laptop).

Step1: Encrypting your password

Just remember this password because you will need it when connecting to the remote host from your local machine.

$ ./anaconda3/bin/ipython 
Python 3.4.3 |Anaconda 2.3.0 (64-bit)| (default, Jun  4 2015, 15:29:08) 
Type "copyright", "credits" or "license" for more information.
IPython 3.2.0 -- An enhanced Interactive Python.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
In [1]: from IPython.lib import passwd
In [2]: passwd()
Enter password: 
Verify password: 
Out[2]: 'sha1:32b1[my encrypted password here]'

Step2: create nbserver profile

This command will create nbserver profile so that we can config.

$ ./anaconda3/bin/ipython profile create nbserver
[ProfileCreate] Generating default config file: '/home/local/ANT/kittipat/.ipython/profile_nbserver/ipython_kernel_config.py'
[ProfileCreate] Generating default config file: '/home/local/ANT/kittipat/.ipython/profile_nbserver/ipython_console_config.py'

Step3: Create the certificate file

Using the command below to create certificate file.

$ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert2.pem -out mycert2.pem
Generating a 1024 bit RSA private key
....++++++
.++++++
writing new private key to 'mycert2.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:**
State or Province Name (full name) [Some-State]:******** 
Locality Name (eg, city) []:*******
Organization Name (eg, company) [Internet Widgits Pty Ltd]:********
Organizational Unit Name (eg, section) []:*****
Common Name (e.g. server FQDN or YOUR name) []:*****
Email Address []:****

And you will see your certificate file there in the directory

/home/local/ANT/kittipat/
-rw-r--r--  1 kittipat domain^users 2.0K Jul 10 13:13 mycert2.pem

Step4: Config the nbserver profile

Config the file:

/home/local/ANT/kittipat/.ipython/profile_nbserver/ipython_notebook_config.py
### Kernel config
c.IPKernelApp.pylab = 'inline'  # if you want plotting support always
 
### Notebook config
c.NotebookApp.certfile = u'<full absolute path to your .pem file>'
c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False
c.NotebookApp.password = u'sha1:32b1e7c...[that password of yours!!!]'
# It is a good idea to put it on a known, fixed port
c.NotebookApp.port = 8888

Step5: Run IPython from remote site

Now you may run IPython from the remote site. Just go to the dir you want to run ipython notebook from

$ cd ~/research/2014/order_segmentation/
$ ~/anaconda3/bin/ipython notebook --profile=nbserver
[I 13:31:08.419 NotebookApp] Writing notebook server cookie secret to /home/local/ANT/kittipat/.ipython/profile_nbserver/security/notebook_cookie_secret
[I 13:31:08.430 NotebookApp] Using MathJax from CDN: https://cdn.mathjax.org/mathjax/latest/MathJax.js
[I 13:31:08.450 NotebookApp] Serving notebooks from local directory: /home/local/ANT/kittipat/research/2014/order_segmentation
[I 13:31:08.450 NotebookApp] 0 active kernels 
[I 13:31:08.450 NotebookApp] The IPython Notebook is running at: https://[all ip addresses on your system]:8888/
[I 13:31:08.450 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

Step6: Connect to the IPython from your local machine

Go to your favorite browser and type the out url from the previous step:

https://[all ip addresses on your system]:8888/

For example, my remote host ip address is

ua4blahblahblahblah.ant.amazon.com

then the url would be

https://ua4blahblahblahblah.ant.amazon.com:8888/

It will ask you for the password, just give the password you put in Step1.

You can also run another ipython notebook session by repeating the Step5 and 6 and you will get assigned with another url. Note that the port number will be incremented by 1:

https://ua4blahblahblahblah.ant.amazon.com:8889/

credits

https://gist.github.com/randyzwitch/7590335