There are many ways to check the local IP address in Linux system. The following are three commonly used methods and their corresponding code examples.
Method 1:
Use the ifconfig command to view the IP address of the machine.
Open the terminal and enter the following command:
ifconfig
In the output, you can find the IP address corresponding to the network interface you are currently using. Usually, the IP address corresponding to the line starting with "inet" is the IP address of the local machine.
Example output:
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.100 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::a00:27ff:fea5:c6d3 prefixlen 64 scopeid 0x20<link> ether 08:00:27:a5:c6:d3 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
In this example, "enp0s3" is the name of the network interface, and "inet 192.168.1.100" corresponds to the IP address of the machine.
Method 2:
Use the ip command to view the IP address of the machine.
Open the terminal and enter the following command:
ip addr show
In the output, you can find the line starting with "inet", and the subsequent numbers are the IP address of the machine.
Example output:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 inet 192.168.1.100/24 brd 192.168.1.255 scope global enp0s3 valid_lft forever preferred_lft forever
In this example, "lo" is the loopback interface, "enp0s3" is the network interface, and "inet 192.168.1.100/24" corresponds to the IP of the local machine address.
Method 3:
Use the hostname command to view the IP address of the machine.
Open the terminal and enter the following command:
hostname -I
The command will directly output the IP address of the machine.
Example output:
192.168.1.100
This is the IP address of this machine.
Whether you use the ifconfig, ip or hostname command, you can easily check the IP address of the machine in the Linux system. Just choose the method that suits you according to the specific situation.
The above is the detailed content of How to check the local IP in linux system. For more information, please follow other related articles on the PHP Chinese website!