--- title: Launching Jupyter Notebook in a Remote Server tags: data-engineer --- :::info Remote server: On-prem Linux Redhat 8 ::: Method: Use SSH, port forwarding mechanism ```bash # Start jupyter lab without opening a browser, # run as a background process, using nohup # dump the output to a specified logfile nohub jupyter lab --no-browser --port=8889 > ./jupyterlab.log & # &: allows to get a new prompt????? # Open log file and get the token vim ~/tmp/jupyterlab.log # Check if jupyter lab is still running ps aux | grep jupyter-lab # Shutdown the jupyter lab kill (pgrep jupyter-lab) # Get the Process ID of jupyter lab then kill the process # SSH tunnel creation in local machine using port forwarding ssh -L 8000:localhost:8888 username@server_IP ``` Explanation: - `ssh -L`: specify given port on the localhost (your local machine) will be forwarded to a given host (your remote server) and port on the remote host. Two ways accessing jupyter notebook from local machine: - Using token key given after running `jupyter notebook` in remote server - Using password setup before running `jupyter notebook` in remote server ## Using SSH and self-signed certificate for HTTPS ```bash # Create a folder to put the certificate related files mkdir ~/ssl_cert && cd ~/ssl_cert # Generate a new private key openssl genrsa -out example.key 2048 # Createa a signed certificate openssl req -new -key example.key -out example.csr # This then prompts you to fill out the certificate # with the country, city, state, common name, and other details # Create a self-signed certificate openssl x509 -req -days 365 -in example.csr -signkey example.key -out example.pem ``` **Setup Jupyter Notebook with Password** ```bash # Create a notebook configuration file jupyter notebook --generate-config # Create a password for your notebook jupyter notebook password ``` **Find the config file then open it** ```bash vim ~/.jupyter/jupyter_notebook_config.py ``` Apply the following updates into the config file: ```python=3.8 c.NotebookApp.open_browser = False c.NotebookApp.ip='*' c.NotebookApp.port=8888 # Add location of the certificate file and private key to the config file c.NotebookApp.certfile = u'/home/[user]/ssl_cert/example.pem' c.NotebookApp.keyfile = u'/home/[user]/ssl_cert/example.key' ``` :::warning Do we need change settings of Firewall to enable outside access for Jupyter Notebook on the server? Setup a Firewall rule under VPC network in the Google Cloud Console. ::: **Start jupyter notebook server** ```bash nohub jupyter notebook --no-browser --port=8888 > ./jupyterlab.log & ``` **Accessing jupyter notebook** (More here: [Running jupyter notebooks on remote server](https://towardsdatascience.com/running-jupyter-notebooks-on-remote-servers-603fbcc256b3)) ```bash nohup ssh -N -f -L localhost:8888:localhost:8888 username:password@remote_server_ip ``` Explanation: The `-N` flag tells ssh that no remote commands will be executed. At this point we do not need to execute any remote commands. The `-f` flag pushes the ssh process to the background as mentioned previously. Finally, the `-L` flag specifies the port forwarding configuration using the syntax `local_server:local_port:remote_server:remote_port`. The configuration specifies that all requests sent to port 8889 on the local machine, e.g., your laptop, to port `8889` on the remote machine at `username:password@remote_server_ip`. The `nohup` command has been prepended to silence the output. **Smoothening Workflow** Open `~/.bashrc` file: ``` alias port_forward='nohup ssh -N -f -L localhost:8889:localhost:8889 username:password@remote_server_ip' alias remote_notebook_start='nohup ssh -f username:password@remote_server_ip "cd rne; . virtual_environment/bin/activate; jupyter notebook --no-browser --port=8889"; port_forward' alias remote_notebook_stop='ssh username:password@remote_server_ip "pkill -u username jupyter"' ``` Reload: `source ~/.bashrc` Localhost Forwarding: https://phoenixnap.com/kb/ssh-port-forwarding