[Linux] How to put your IP in a variable


Tips when you want to put your IP in a variable, such as in a shell script Only IPv4 is supported.


$ cat /etc/os-release 
NAME="Amazon Linux"
ID_LIKE="centos rhel fedora"
PRETTY_NAME="Amazon Linux 2"
$ ip a|grep -e inet |grep -v inet6
    inet scope host lo
    inet brd scope global dynamic eth0
    inet brd scope global dynamic eth1
$ hostname -i #Show only eth0
$ hostname -I #Show everything except lo


#The simplest way
$ MyIP=`hostname -i` 
$ echo $MyIP

#When there are multiple IPs
$ MyIPeth0=`hostname -I | cut -f1 -d' '`
$ echo $MyIPeth0
$ MyIPeth1=`hostname -I | cut -f2 -d' '`
$ echo $MyIPeth1

Command 2

Click here if you are told to use the hostname command because it is dangerous

$ MyIPeth0=`ip -f inet -o addr show eth0|cut -d\  -f 7 | cut -d/ -f 1`
$ echo $MyIPeth0

$ MyIPeth1=`ip -f inet -o addr show eth1|cut -d\  -f 7 | cut -d/ -f 1`
$ echo $MyIPeth1


When executing the hostname command as root, if'hostname i'is used instead of'hostname -i' Note that the host name will be i.

$ hostname
$ hostname i
hostname: you must be root to change the host name

$ sudo su -

# hostname i
# hostname

# MyIPeth0=`hostname I | cut -f1 -d' '` 
# hostname

that's all

Recommended Posts

[Linux] How to put your IP in a variable
How to embed a variable in a python string
How to put a symbolic link
How to display a specified column of files in Linux (awk)
[sh] How to store the command execution result in a variable
[Itertools.permutations] How to put permutations in Python
How to check the memory size of a variable in Python
How to get a stacktrace in python
[V11 ~] A memorandum to put in Misskey
[Python] How to put any number of standard inputs in a list
How to put a half-width space before letters and numbers in Python.
How to clear tuples in a list (Python)
How to create a JSON file in Python
How to define your own target in Sage
How to write regular expression patterns in Linux
How to implement a gradient picker in Houdini
Set a fixed IP in the Linux environment
How to create data to put in CNN (Chainer)
To add a module to python put in Julialang
How to notify a Discord channel in Python
How to create a shortcut command for LINUX
[Python] How to draw a histogram in Matplotlib
How to create a Rest Api in Django
How to write a named tuple document in 2020
How to count numbers in a specific range
How to read a file in a different directory
How to Mock a Public function in Pytest
How to specify a schema in Django's database settings
How to get the variable name itself in python
How to make Yubico Yubikey recognized in Manjaro Linux
How to install Linux on a 32bit UEFI PC
I made a script to put a snippet in README.md
A memorandum on how to use keras.preprocessing.image in Keras
How to allow nologin users to log in on Linux
How to display DataFrame as a table in Markdown
How to create a local repository for Linux OS
Try to put LED in your own PC (slightly)
How to execute a command using subprocess in Python
How to reference static files in a Django project
How to build a Python environment on amazon linux 2
How to use Docker to containerize your application and how to use Docker Compose to run your application in a development environment
A super introduction to Linux
How to call a function
How to hack a terminal
How to develop in Python
How to put a hyperlink to "file: // hogehoge" with sphinx-> pdf
A memo on how to easily prepare a Linux exercise environment
How to use linux commands in Visual Studio Code terminal
How to slice a block multiple array from a multiple array in Python
How to import NoteBook as a module in Jupyter (IPython)
How to output a document in pdf format with Sphinx
A story about how to specify a relative path in python.
How to make a hacking lab-Kali Linux (2020.1) VirtualBox 64-bit Part 2-
Linux: How to recover if'grub_file_filters' not found in grub rescue
How to import a file anywhere you like in Python
How to use pyenv and pyenv-virtualenv in your own way
How to temporarily implement a progress bar in a scripting language
How to define multiple variables in a python for statement
How to make a hacking lab-Kali Linux (2020.1) VirtualBox 64-bit edition-
Put the lists together in pandas to make a DataFrame
A note on how to load a virtual environment in PyCharm