- How to find your IP address in Linux
- Every website has a unique, public IP address that can be reached by anyone and from anywhere.
- Subscribe now
- Finding your IP address in the GNOME desktop
- gnome-activities-wifi.jpg
- gnome-wi-fi-gear.png.jpg
- gnome-ip-addr.jpg
- Public and private IP addresses
- Private Ipv4 addresses
- Public Ipv4 addresses
- Finding your way with IP
- How to get your IP Address on Linux
- Public and Private IP Addresses
- What is an IP Address?
- Understanding the TCP/IP Protocol
- Differences Between Public and Private Addresses
- How to Find your Public IP Address
- Getting your Public IP Address with the dig Command
- Using Curl to Return your Public IP Address
- Using wget Instead of curl
- Using a Browser
- Get your Private IP Address with the ifconfig Command
- Free eBook: Git Essentials
- How to Find your Private IP Address using Hostname Command
- Get your Private IP Address with the IP Command
- Finding your Private IP Address from Network Connection Settings
- Numerous ways to get IP Address on Linux
How to find your IP address in Linux
Every website has a unique, public IP address that can be reached by anyone and from anywhere.
Subscribe now
Get the highlights in your inbox every week.
We all use the Internet Protocol (IP) daily, whether we’re aware of it or not. For instance, any time you type a website name or a search term into your web browser, it looks up the IP address of that URL (or of a search engine) and then loads the website.
Let’s divide IP addresses into two categories: private and public. Private IP addresses are the ones your WiFi box (and company intranet) provide. They are in the range of 10.x.x.x, 172.16.x.x-172.31.x.x, and 192.168.x.x, where x=0 to 255. Public IP addresses, as the name suggests, are «public» and you can reach them from anywhere in the world. Every website has a unique IP address that can be reached by anyone and from anywhere; that is considered a public IP address.
IPv4 addresses have the format x.x.x.x, where x=0 to 255. There are 2 ^32 (approximately 4 billion) possible IPv4 addresses.
IPv6 addresses have a more complex format using hex numbers. The total number of bits is 128, which means there are 2 ^128 —340 undecillion!—possible IPv6 addresses. IPv6 was introduced to tackle the foreseeable exhaustion of IPv4 addresses in the near future.
As a network engineer, I recommend not sharing your machine’s public IP address with anyone. Your WiFi router has a public IP, which is the WAN (wide-area network) IP address, and it will be the same for any device connected to that WiFi. All the devices connected to the same WiFi have private IP addresses locally identified by the range provided above. For example, my laptop is connected with the IP address 192.168.0.5, and my phone is connected with 192.168.0.8. These are private IP addresses, but both would have the same public IP address.
The following commands will get you the IP address list to find public IP addresses for your machine:
- curl ifconfig.me
- curl -4/-6 icanhazip.com
- curl ipinfo.io/ip
- curl api.ipify.org
- curl checkip.dyndns.org
- dig +short myip.opendns.com @resolver1.opendns.com
- host myip.opendns.com resolver1.opendns.com
- curl ident.me
- curl bot.whatismyipaddress.com
- curl ipecho.net/plain
The following commands will get you the private IP address of your interfaces:
- ifconfig -a
- ip addr (ip a)
- hostname -I | awk ‘
‘ - ip route get 1.2.3.4 | awk ‘
‘ - (Fedora) Wifi-Settings→ click the setting icon next to the Wifi name that you are connected to → Ipv4 and Ipv6 both can be seen
- nmcli -p device show
Note: Some utilities need to be installed on your system based on the Linux distro you are using. Also, some of the noted commands use a third-party website to get the IP
Finding your IP address in the GNOME desktop
If you’re using Linux, you can find your IP address using some basic desktop utilities. First, go to your Activities screen and type Network (for a wired connection) or Wi-Fi (for wireless).
gnome-activities-wifi.jpg
In the Networks settings window, click the Gear icon next to the network you’re on.
gnome-wi-fi-gear.png.jpg
Your IP address is listed in the Network profile.
gnome-ip-addr.jpg
In this example screenshot, my IPv4 address is 10.1.1.2 and the gateway is 10.1.1.1.
Public and private IP addresses
To understand the significance of public and private IP addresses, let’s have a look at a concept called NAT (Network Address Translation) and PAT (Port Address Translation).
Private addresses are used in Local Area Networks (LAN). They are bound to a specific network.
Public addresses are necessary for establishing external connectivity to other networks, most notably the «Worldwide Web» (www) of the Internet.
NAT is a translation of a private IP to a public one, and consists of three major types: static, dynamic, and PAT. In static NAT, one private IP is mapped to one public IP. A common example ru ydco is a firewall. In dynamic NAT, a private IP address is mapped to a public IP but from a pool of public IP addresses.
With a total of 2^32 IPv4 addresses, out of which approximately just 3.7 billion are public addresses, there are literally more people and devices than there are IP addresses. And yet we haven’t run out of addresses. That’s because of a concept called PAT, which allows for a single public IP address to be translated from multiple (generally all) private IP addresses using port numbers. Here, instead of assigning a public address to each device, a single public address is allocated to the external side, and private addresses are assigned to each device within the internal network. PAT is most widely used in residential wireless routers, which we use to connect to the Internet.
Private Ipv4 addresses
Local addresses are only used within an internal network.
The range is 10.x.x.x, 172.16.x.x-172.31.x.x, and 192.168.x.x, (x is a number from 0 to 255).
Used in Local Area Networks (LAN).
Not globally reachable, and cannot be searched globally.
Not unique: the same private IP can be used in two different networks.
Each router comes with a private IP address, which multiple devices can connect to. Users don’t need to pay extra to obtain a private address.
Some example private IP addresses: 10.0.0.4, 172.16.4.5, 192.168.9.8
Public Ipv4 addresses
Public addresses are used to connect to external networks.
Ranges from 0.0.0.0 to 255.255.255.255, except for private IP addresses and few unusable or reserved IPs.
Used in connecting to the Internet.
Globally reachable and can be searched. These are often used to determine geolocation.
Unique across all of the Internet.
A private address costs money because they are unique, limited, and accessible from the Internet. Each website has a domain name mapped to a public IP. For example, the public IP address for opensource.com is 54.204.39.132
Finding your way with IP
An IP address is the most direct route to a computer over a network. There are other systems, such as DNS and Avahi, to help route one computer to another, but when those are unavailable or undesired for any reason, the IP protocol is what you use. Now you understand why, and more importantly, how to find your own.
Editor’s note: This article was originally published in May 2018 and has been updated.
Источник
How to get your IP Address on Linux
Public and Private IP Addresses
IP addresses are an essential part of modern networked communications. In this guide, we will show you how to find your own IP address. These instructions will work for most of the various Linux distributions like Ubuntu, Debian and Linux Mint, among others. BSD systems like FreeBSD and NetBSD, as well as Mac OS, might also be able to read their IP addresses using these instructions.
What is an IP Address?
Understanding the TCP/IP Protocol
IP addresses arose as part of what’s known as the TCP/IP protocol. This is a networking standard that defines how data is packaged and transferred between computers that are joined together in a computer network.
The TCP/IP protocol was invented in 1978 by Bob Kahn, Vint Cerf, and others.
TCP stands for Transmission Control Protocol, which is the protocol responsible for the reliable transmission of data over networks. In particular, TCP checks integrity of transmissions, and provides for re-transmission if packets have not been delivered correctly.
The IP protocol, on the other hand, deals with the routing of packets (like those sent by TCP). IP stands for Internet Protocol, which defines rules that let packets be sent from an origin to a destination computer. Under the IP protocol, packets are routed by a path of computers that gets progressively closer to the packet’s intended recipient.
Each computer or device on the network is identified by an IP address, which is a numeric identifier unique to that device on the network.
Differences Between Public and Private Addresses
There is a difference between IP addresses on the public internet and private IP addresses inside a local computer network. Internet Service Providers (ISPs) assign public IP addresses, which is akin to your public home address. When computers connect to the private network, they each get a private IP address to be used within just that network. Private IP addresses are assigned by your router or another device within the network. In turn, the router can route incoming packets to the correct device on the network using network address translation (NAT).
How to Find your Public IP Address
You can discover your public IP address using a variety of commands that connect to the internet to run queries for you.
Getting your Public IP Address with the dig Command
The dig command is a DNS lookup utility for Linux. Using dig , you can look up your public IP address by connecting to OpenDNS servers. OpenDNS hosts DNS servers that help discover the IP addresses of networks on the internet. Run the following command inside your bash, sh, or other terminal.
As output you should get back your public IP address from OpenDNS resolvers. Your IP address will be a series of groups of digits of the format 216.58.216.164 (which is one of the many IP addresses for Google), or a variation of this sort.
Using Curl to Return your Public IP Address
The curl command is another networking utility that allows you to interact with servers on the internet. You can query servers to return your public IP using the following options:
The output of each command will be your public IP as seen by these servers.
There are many other servers that can return your public IP address besides these two given above.
Using wget Instead of curl
wget is a commonly available utility for Linux that you can use instead of curl to download data and interact with servers. You can use wget to retrieve your public IP using the command:
Using a Browser
If you have a browser available, you can visit one of the many IP websites out there:
or you can also check the other websites we reached with curl to see your IP in the browser. There are cases, such as when you are logged on to a Linux server, where you do not have access to a graphical user interface. In such cases, use the shell commands.
Get your Private IP Address with the ifconfig Command
You have multiple ways to get your private IP address. One way is to use the ifconfig command. ifconfig is a command line program that configures network interfaces on Linux.
You can retrieve your IP address using the ifconfig command coupled with various flags that filter for your private IP address. Run the following command inside your shell:
Free eBook: Git Essentials
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
The above command checks all the active network interfaces, then filters for the TCP/IP interface, and finally filters the output for the local IP address. The final output is your private IP address. This will look similar to 192.168.1.2 or some variant.
We can filter the ifconfig output using sed instead, which is a utility for parsing and transforming streams of text. Run the following command to get your private IP address:
In this example, we are filtering ifconfig output to get your IP address under the TCP/IP protocol. The above commands may fail if you do not have sed or ifconfig installed. In case the command fails, try the hostname command and follow the method below.
How to Find your Private IP Address using Hostname Command
The https://linux.die.net/man/1/hostname command returns the DNS information of the machine. You can find your private IP address by executing the following command in your shell:
The above command enumerates all your configured addresses on all network interfaces, including your private IP address.
Get your Private IP Address with the IP Command
We can also get the private IP address of a Linux machine using the ip command. The ip command shows and manipulates routing, devices, policy routing and tunnels.
We can use the following variations of the ip command with flags to return our private IP address.
These commands print out the routing table entries for sending a request to alternate servers. These involve our private IP address as the source of the requests. We are filtering for that source using the Linux commands head, awk and cut to extract our private IP address.
Finding your Private IP Address from Network Connection Settings
Besides the command line and the browser, we can also use Linux administrative applets. You may need to adjust these instructions to suit your exact Linux distro.
- Navigate to your «Menu»
- Find the «Preferences» tab
- Navigate to «Network» or the equivalent for managing your network information
- Select the network type, (i.e. Wi-Fi, Ethernet, or other)
- You can view your private IP address under the printed information about your IP
This will not work in a strictly command-line environment but will work on desktop Linux systems. If you are on a command-line only login, then you will need to try the other command-based methods given above.
Numerous ways to get IP Address on Linux
As we saw, there are multiple ways to get your IP address on a Linux system. There are in fact two types of IP addresses, a public IP address and a private IP address. The public IP address identifies your computer or network to the outside world. Your private IP address identifies your machine inside your private network. To get your IP addresses, you can use a mix of commands such as ifconfig, ip or hostname, or make use of graphical environment apps.
Источник