Curl linux мой ip

Get an IP Address on Linux : Discover the Many Ways

Ransomware recovery test drive: This technical workshop is designed to take you behind the scenes and shows you how to adopt strategies to automate recovery, ensuring you’re ready to become a recovery hero. REQUEST YOUR LAB

Your IP address is like your home address, and it’s how the internet knows where to send the data you’ve requested. Knowing your IP address is handy in many ways, like remotely managing your server. But how do you get an IP address on Linux?

In this tutorial, you’ll learn many ways to find your IP address on Linux, whether using the command line or checking through system settings.

Read on and pick off how you’d like to get your IP address!

Prerequisites

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following:

  • A Linux machine – You can use a virtual machine, a dedicated server, or even your local Windows 10 computer with Windows Subsystem for Linux (WSL) – This tutorial uses Ubuntu 20.04 LTS, but other Linux distros will work.
  • NET-3 networking toolkit installed.
  • This tutorial assumes you’ve already logged/SSH into the system as a user with sudo privileges – This tutorial uses the root user to execute all the commands.

If you’re not logged in as a root user, prepend sudo to this tutorial’s commands.

Get an IP Address on Linux with the curl Command

A popular tool called cURL lets you transfer data specified with URL syntax. This tool supports protocols like DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, etc.

But the important question is, can you get your public IP address with the curl command? Yes! Getting your public IP address is just one of the cURL’s powerful features.

Related: placeholder of this one

Run the below command to perform the following in finding your public IP address:

  • curl – Uses the Google Search API to fetch the first result of the query “ what is my ip address .”
  • The -s option tells curl to perform a silent request. The output is then piped to grep , which extracts only the IP address from the response using a regular expression.
  • The -oE portion of the grep command stands for “only match,” which outputs only the matching IP address and not the whole response.
  • The -m1 option tells grep to stop after the first match. Why? Google Search API returns a bunch of HTML code along with the IP address; you don’t need that.
curl -s | grep -oE "\\b(2.)2\\b" -m1 

Below, you can see your public IP address printed out.

Читайте также:  How to install ffmpeg linux

Finding the public IP address with the curl command

Get an IP Address on Linux with the wget Command

Like cURL, Wget is a free utility that lets you retrieve files using the most widely-used Internet protocols (HTTP, HTTPS, and FTP).

Wget lets you download single files, whole websites, or even mirror an entire website, and of course, find your IP address.

Run the below wget command to find, and display your public IP address to the terminal with the following:

  • The wget command sends an HTTP request to https://ipecho.net/plain and retrieves the content of that page.
  • The -qO option tells wget to be quiet and print as little output as possible to the terminal.
  • The output of the wget command is piped to the xargs echo command to display the output in a single line.

Now, you can share the IP address with your users so that they can connect to the server via SSH.

Finding the public IP address using the wget command

Digging Your Public IP Address with the dig Command

The Domain Information Groper ( dig ) command is used for querying DNS servers, but you can also use this command to find your public IP address.

Suppose you sent a ping request to your web server’s public IP address. If you received the ping response, you know that your web server is running and accessible from the outside world. If not, you may have to troubleshoot the issue to identify the problem, and your first step is to find your public IP address.

Run the below dig command to query your web server’s public IP address.

  • The below command uses OpenDNS nameservers ( resolver1.opendns.com ) to find your public IP address. OpenDNS servers are a good idea as they are reliable and offer better security features, such as phishing protection and content filtering.
  • The +short option instructs dig to print the output in a short format, while myip.opendns.com is the hostname that returns your public IP address.
dig +short myip.opendns.com @resolver1.opendns.com 

Finding the public IP address using the dig command

Now, run the ping command below to send a ping request every 10 seconds. Replace IP_address with your web server’s actual public IP address.

Читайте также:  Linux mint xfce gnome

Sending ping request

Finding Your Private IP Address Using the ifconfig Command

At this point, you already know how to find your public IP address. But in some cases, you may need to know the private IP address of your Linux server to connect to it from another device on the same network.

How to get your private IP address? The ifconfig command will do the trick.

Run the ifconfig command below to display all ( -a ) network adapters on your system and their configuration, including your private IP address.

As you can see below, one system can have more than one network adapter. Each adapter can have a different IP address.

Look for the inet entry under the ethh0 or wlan0 adapter to find your private IPv4 address, as shown below.

Finding Private IP Address Using ipconfig Command

Writing a Bash Script to Find Your IP Address

Perhaps you need a way to find your IP address automatically. If so, writing a Bash script is an excellent choice to find your public and private IP addresses.

Why use a script? You can use the same script on multiple systems, saving yourself the hassle of running the commands on each system.

1. Open a new find_ip.sh file in your preferred text editor.

2. Add the following code to the find_ip.sh file, save the file and close the editor.

The code below finds and stores your private and public IP addresses on the lanIp and wanIp variables and prints them on the terminal.

#!/bin/bash # Find your private IP address and store the value to the lanIp variable lanIp="$(ip -4 -o -br addr|awk '$0 ~ /^[we]\w+\s+UP\s+/ ')"; # Find your public IP address and store the value to the wanIp variable wanIp="$(curl https://ipinfo.io/ip 2>/dev/null)"; # Print the values of the lanIp and wanIp variables echo "Your private ip is: $"; echo "Your public ip is: $";

Storing your server’s public IP address in an environment variable is handy for a CD/CI tool, such as Jenkins or TravisCI.

3. Finally, run the below sh command to execute the find_ip.sh script to find your IP addresses.

You will see an output similar to the following, showing your public and private IP addresses.

Finding IP addresses using a Bash script

Conclusion

In this tutorial, you’ve learned how to find/get your IP address on Linux using a few different commands, like the curl and ifconfig . You also wrote a script to automate finding IP addresses on multiple systems.

Now that you know how to find your IP address on Linux, you can use this information for different tasks. Perhaps to configure a Cloudflare dynamic DNS for your home network?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Читайте также:  Bitvise ssh for linux

Источник

How to use curl to get public IP address

The curl command on Linux systems is commonly used to download and upload files to or from a remote server. Another interesting thing we can use it for is to determine the public IP address of our system.

To do this, we can use curl to query websites that are configured to do nothing more than return the IP address of whichever system is connecting to it. This is one of the quickest and easiest ways to get your public IP address on the Linux command line. Follow along with us below and we’ll show you a few examples.

In this tutorial you will learn:

Using curl to get public IP address on the Linux command line

Software Requirements and Linux Command Line Conventions

Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software curl
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Install curl

Although curl is a common utility, it’s not included on every single Linux distro. Specifically, it may be missing from more desktop oriented distros. Just in case it’s not already installed, you can use the appropriate command below to install it through your system’s package manager.

Use curl to get public IP address

There are many websites that have been configured to show someone’s public IP address. Use one or more of the following examples below to determine yours.

  1. This site will also work with the -4 or -6 curl options, if you need to retrieve your IPv4 or IPv6 address specifically.
$ curl icanhazip.com OR $ curl -4 icanhazip.com OR $ curl -6 icanhazip.com
$ curl bot.whatismyipaddress.com

Of course, other sites also exist that can do the same thing, but these should be enough to cover your needs. In the future, some sites may stop working and new ones will fill their place, so keep in mind that any website on the list may not be permanent.

Closing Thoughts

In this guide, we saw how to retrieve the public IP address of a system by using curl on the Linux command line. This relies on third party public websites to show us our IP address, but it proves to be one of the quickest and easiest ways to see our public IP on the command line.

Comments and Discussions

Источник

Оцените статью
Adblock
detector