using sudo inside jupyter notebook's cell

sudo: jupyter: command not found
run jupyter notebook as root
jupyter notebook permission denied
sudo jupyterhub command not found
run jupyter notebook ubuntu
jupyter notebook permission denied local
start jupyter notebook with python 3
jupyter notebook docker jovyan password

I'm trying to make a tutorial for a platform inside a jupyter notebook

at some point I need to run a linux command inside a cell like this :

!sudo apt-get install blah

but cant figure out how to enter the sudo pass , and I dont want to run jupyter notebook with sudo, any idea how to do this ?

Update: I checked all the methods, all of them are working.


1:

Request password using getpass module which essentially hides input by user and then run sudo command in python.

 import getpass
 import os

 password = getpass.getpass()
 command = "sudo -S apt-get update" #can be any command but don't forget -S as it enables input from stdin
 os.system('echo %s | %s' % (password, command))

2:

 import getpass
 import os

 password = getpass.getpass()
 command = "sudo -S apt-get update" # can be any command but don't forget -S as it enables input from stdin
 os.popen(command, 'w').write(password+'\n') # newline char is important otherwise prompt will wait for you to manually perform newline

NOTE for above methods:

The field where you enter the password may not appear in the ipython notebook. It appears in the terminal window on a mac, and I imagine it will appear in a command shell on a PC. Even the result details would appear in the terminal.

3:

You can store your password in mypasswordfile file and just type in cell :

!sudo -S apt-get install blah < /pathto/mypasswordfile # again -S is important here

I would prefer this method if I want to view output of the command in jupyter notebook itself.

References:

  1. Requesting password in IPython notebook

  2. https://docs.python.org/3.1/library/getpass.html

  3. Using sudo with Python script

Running jupyter notebook sessions as root in jupyterhub · Issue , I use a networking library (in python 2) which needs root privilege to run, I can achieve this with the jupyter notebook by running: sudo  Use sudo service jupyter restart to restart jupyter because every once in a while jupyter would throw a fit and restarting kernels will not be enough to get it back to a responsive state. Add ? before almost any function, variable, and run the code cell to access its documentation.

You can

subprocess.Pope(['sudo', 'apt-get', 'install', 'bla'])

If you want to avoid the python syntax, you can define your own cell magic that does that for you (e.g. %sudo apt-get install bla).

Run JupyterHub without root privileges using sudo, of JupyterHub while running notebook servers as real system users on a shared system To do this we add to /etc/sudoers (use visudo for safe editing of sudoers): As an alternative to adding every user to the /etc/sudoers file, you can use a group in the last line above, instead of Copyright 2016, Project Jupyter team. As an alternative to add every user to the /etc/sudoers file, you can use a group reference at the end of the file, instead of JUPYTER_USERS. The following two lines added to the sudoers file allow 1) the group to execute sudo as user 'rhea', and 2) rhea to run as a designated 'jupyterhub' group

You can pass python variables from a notebook to the shell without importing the os or subprocess modules by using the {varname} syntax (e.g. this cool blog).

If you have defined a password and command variable in python (see Suparshva's answer) then you can run this one-liner:

!echo {password}|sudo -S {command}

The exclamation mark tells jupyter to run it in the shell, the echo command will then get the real password (e.g. 'funkymonkey') from the variable called password and then pipe it into the sudo'd command variable (which is a string that describes a shell command, e.g. 'apt-get update').

18.04, Every time, I use Jupyter Notebook in Ubuntu 18.04, I have to access sudo bash first and then type jupyter notebook --allow-root to use Jupyter  If you want to run jupyter notebook in the container, but assign a user id to jovyan other than the default UID=1000, GID=100 combo that the container uses, you can do that and grant sudo access. The README in the stack folder should have the steps as to how.

can you start your jupyter notebook using sudo., Either Jupyter or Spark, one installed in admin mode and the other one not. sudo jupyter notebook. similar to any other package, before installing anything on Ubuntu run following command $sudo apt-get update Once system is updated to latest packages, you can

How to Install Jupyter Notebook on Ubuntu – Linux Hint, In this lesson, we will setup the Jupyter Notebook server on a Ubuntu machine and If you start Jupyter as a root user, we will have to use the –allow-root flag. Connecting To Remote Jupyter Servers. When a Jupyter notebook file is created or opened, VS Code automatically creates a Jupyter server for you locally by default. If you want to use a remote Jupyter server, it’s as simple as using the “Specify Jupyter server URI” command via the VS Code command palette, and entering in the server URI.

Project Jupyter, Get up and running with the JupyterLab or the classic Jupyter Notebook on your computer within minutes! Getting started with JupyterLab. In IPython (jupyter) 7.3 and later, there is a magic %pip and %conda command that will install into the current kernel (rather than into the instance of Python that launched the notebook). %pip install geocoder

Comments
  • Is there a variation on the above which doesn't leave the password accessible if you step away from the notebook?
  • The password should be a variable. For example password = 'my_real_password'. You could also use the getpass module as in Suparshva's answer to avoid accidentally saving your actual password in a notebook.
  • Similarly command is a string variable that describes something you might run in a terminal, e.g. command = "sudo -S apt-get update"

Hot Questions

  • C# Trying to get a list of questions from one class into my Main() class loop3470
  • Printing lines for which an element repeated in one field is associated with another element repeated in another field4802
  • How do I update the value of Person or Group columns in SharePoint lists using the Microsoft Graph API?1037
  • Best way to shift a list in Python?9000
  • How do you use multiple adsense units on one page?4541
  • Functions are not valid as a React child. This may happen if you return a Component instead of from render2779
  • How to store only most recent entry in Cassandra?5615
  • Using name attribute with ngModel in a form tag, still getting error -"If ngModel is used within a form tag, either the name attribute must be set..."9883
  • JavaScript split String with white space4034
  • How to print out every 50 values from a list?557
  • Getting a numbers from a string with chars glued5647
  • Count unique symbols except white space5022
  • How to efficiently store and search a non-uniform grid for 2D space using python646
  • Need help iterating through a list of strings in python in reverse, as soon as a string with a number is found, return everything before5540
  • Running NodeJS project in Visual Studio Code with yarn9366
  • Remove characters after specific character in string, then remove substring?1812
  • Uploading files using Browse Button in Jupyter and Using/Saving them1003
  • PrimeNG p-tree error: Can't bind to 'value' since it isn't a known property of 'p-tree'9080
  • How to ping multiple ip address5840
  • Extending log4j/slf4j Logger3771