A memorandum of how to execute the! Sudo magic command in Jupyter Notebook

Jupyter Notebook is very convenient as a convenient execution environment for Python, and I love it. You can add languages other than Python, and you can also execute Linux commands (magic commands) on your notebook.

Execution example

You can execute the ls command and the cd command as shown below.

ls command

!ls

cd command

%cd

pip command

The pip command can also be executed as a magic command. If you want to install pandas, you can execute it with the following command. If you want to add additional required libraries while using Jupyter Notebook If you write the following command, you can rest assured when sharing Notebook.

!pip install pandas

Also execute commands that require sudo

The main subject is from here. Try running a command that needs to be executed with administrator privileges (sudo). Let's run ʻapt` here. In the Python library, there is a thing [^ app] that sometimes requires you to install an application with the package manager of the OS separately. If you can execute it on Notebook, you can prevent forgetting to install it.

Step 1: Run as is

Try running ! Sudo apt update as it is.

!sudo apt update

The result is as follows. You cannot enter the password in this state.

sudo.png

Step 2: Pass a string using the echo command

Try passing the password as a string using the echo command. Pass the -S option to the sudo command. You can now pass the password as a command line argument. (Actually, the method of inputting the password from the standard input)

!echo "passwd" | sudo -S apt udate
sudopw.png

It worked safely. However, it becomes complicated because it is necessary to rewrite the character string when sharing the notebook. In addition, the password is completely known as it is, which is not preferable for security. If the password is leaked, it does not make sense. .. ..

Pass the password to an external file

Write the password in a file called passwd.txt, and change the method to read and pass the contents of that file. This way you don't have to worry about your password being accidentally leaked. Use the xargs command to read the file. A command that creates command line arguments. Here, the file is specified by passing the -a option (--arg-file =).

!xargs -a passwd.txt | sudo apt update
sudopwarg.png

I was able to execute it safely. There is no need to worry about password leakage, and there is no need to rewrite the password part when sharing a notebook, eliminating complicated work [^ passwd]. This is safe.

Reference

[^ app]: Mecab and GraphViz need to be installed separately from the Python version.

[^ passwd]: It is necessary to prepare a file equivalent to passwd.txt. .. ..

Recommended Posts

A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
How to execute commands in jupyter notebook
How to execute a command using subprocess in Python
How to pass the execution result of a shell command in a list in Python
To output a value even in the middle of a cell with Jupyter Notebook
I made an appdo command to execute a command in the context of the app
How to make the font width of jupyter notebook put in pyenv equal width
How to import NoteBook as a module in Jupyter (IPython)
How to see the contents of the Jupyter notebook ipynb file
How to import NoteBook as a module in Jupyter (IPython)
[sh] How to store the command execution result in a variable
How to determine the existence of a selenium element in Python
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
How to output the output result of the Linux man command to a file
How to get the vertex coordinates of a feature in ArcPy
Jupyter Notebook Basics of how to use
How to pass the execution result of a shell command in a list in Python (non-blocking version)
[Linux] Command to get a list of commands executed in the past
How to calculate the volatility of a brand
How to mention a user group in slack notification, how to check the id of the user group
How to count the number of elements in Django and output to a template
How to get a list of files in the same directory with python
How to write a GUI using the maya command
How to get the number of digits in Python
A memorandum on how to use keras.preprocessing.image in Keras
How to instantly launch Jupyter Notebook from the terminal
How to hit the document of Magic Function (Line Magic)
How to display the modification date of a file in C language up to nanoseconds
How to identify the element with the smallest number of characters in a Python list?
How to check in Python if one of the elements of a list is in another list
How to use Jupyter Notebook
[Ubuntu] How to delete the entire contents of a directory
How to find the optimal number of clusters in k-means
A memorandum to register the library written in Hy in PyPI
How to use the __call__ method in a Python class
About the garbled Japanese part of pandas-profiling in Jupyter notebook
How to develop in a virtual environment of Python [Memo]
How to generate a query using the IN operator in Django
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
How to find the scaling factor of a biorthogonal wavelet
The usual way to add a Kernel with Jupyter Notebook
How to get a quadratic array of squares in a spiral!
How to connect the contents of a list into a string
[Golang] Command to check the supported GOOS and GOARCH in a list (Check the supported platforms of the build)
How to quickly count the frequency of appearance of characters from a character string in Python?
How to plot the distribution of bacterial composition from Qiime2 analysis data in a box plot
Jupyter Notebook Magic Command Personal Summary
In the python command python points to python3.8
How to display a specified column of files in Linux (awk)
How to execute the sed command many times using the for statement
How to batch start a python program created with Jupyter notebook
How to handle multiple versions of CUDA in the same environment
How to implement Java code in the background of RedHat (LinuxONE)
How to resolve ModuleNotFoundError: No module named XXX in Jupyter Notebook
How to know the internal structure of an object in Python
How to get all the possible values in a regular expression
How to change the color of just the button pressed in Tkinter
[EC2] How to install chrome and the contents of each command
How to make a command to read the configuration file with pyramid