Here’s how to create Alias commands to simplify tasks on the command line. The examples given relate to retrieving IP addresses. The process is shown for Linux using Bash and Windows using PowerShell.

screenshot of a terminal running Linux with the Bash shell.  The user has entered the command “ipext” and the external IP address has been returned

Definitions - “Internal” & “External” IP Addresses

Your computer has an IP address on the LAN it is connected to. This blog post will refer to that address as the internal IP address, typical examples being… 192.168.1.12/24 at home or 10.23.8.7/20 on a corporate network.

Your computer also has an IP address as seen by devices from external to the LAN, for example by servers on the internet. This blog post will refer to that address as the external IP address, for example 123.44.23.87. If you open a browser and Google “whats my ip”, you will find yours.

Jump to Windows Method

Linux: Creating Aliases In Bash To Return The Internal & External IP Address

TL;DR

  1. Open a terminal.
  2. Edit file .bashrc by entering sudo nano ~/.bashrc.
  3. Copy/paste the lines below to the end of .bashrc.
  4. Exit nano by pressing Ctrl + x and press y to save changes.
  5. Restart Bash with exec bash.
  6. Done! You can now use the newly created aliases ipint and ipext to return IP addresses.
1# Alias for IP address on the LAN.
2# - this version returns the address + CIDR/subnet suffix e.g. xxx.xxx.xxx.xxx/xx
3alias ipint="ip addr | awk '\$1==\"inet\" && \$3!=\"scope\" {print \$2}' "
4# - this version returns the address only e.g. xxx.xxx.xxx.xxx
5#alias ipint="ip addr | awk -F' |/' '\$5==\"inet\" && \$8!=\"scope\" {print \$6}' "
6
7# Alias for device's IP address externally, from the internet.
8# An external service is required to return the external IP address
9alias ipext="curl https://icanhazip.com"

How To Retrieve IP Addresses Using Bash

Address On The LAN

Linux provides several methods to retrieve IP addresses. The examples below use ip addr, piping the output to awk to select the values of interest.

The following two commands return the internal IP address. The first example returns the IP with the CIDR suffix (subnet range), the second without.

1# include the CIDR suffix (subnet range)
2ip addr | awk '$1=="inet" && $3!="scope" {print $2}'
1# without CIDR suffix
2ip addr | awk -F' |/' '$5=="inet" && $8!="scope" {print $6}'

External Address

In order to find the external IP address, a service beyond the LAN needs to be called, such as a server on the internet. Many services exist, this example uses https://icanhazip.com. This can be changed to your own preferred service.

1curl https://icanhazip.com

Note that other external services are available, for example:

 1curl https://api.ipify.org
 2curl https://checkip.amazonaws.com
 3curl https://icanhazip.com
 4curl https://ident.me
 5curl https://ifconfig.io
 6curl https://ifconfig.me
 7curl https://ipecho.net/plain
 8curl https://ipinfo.io/ip
 9curl https://myexternalip.com/raw
10curl https://wtfismyip.com/text

Also, using the Linux DNS util command, dig:

1dig whoami.akamai.net @ns1-1.akamaitech.net +short
1dig myip.opendns.com @resolver1.opendns.com +short
1dig TXT o-o.myaddr.l.google.com @ns1.google.com +short
1dig TXT ch whoami.cloudflare @1.0.0.1

Creating Aliases For The Bash Commands

When creating an Alias for the above commands certain characters, such as embedded " and $ must be be escaped for the Alias command string to be parsed successfully. This can be achieved by inserting a \ character before, to escape them. So the Alias commands are:

1#this version returns the address + CIDR/subnet suffix e.g. xxx.xxx.xxx.xxx/xx
2alias ipint="ip addr | awk '\$1==\"inet\" && \$3!=\"scope\" {print \$2}' "
3#this version returns the address only e.g. xxx.xxx.xxx.xxx
4#uncomment the line below if you want to use this version
5#alias ipint="ip addr | awk -F' |/' '\$5==\"inet\" && \$8!=\"scope\" {print \$6}' "
6
7# Alias for device's IP address as seen externally, from the internet.
8# An external service is required to return the external IP address
9alias ipext="curl https://icanhazip.com"

Adding Aliases To The .bashrc File

Aliases typically only exist in the session in which they are created. A simple way to ensure the ipint and ipext persist in Bash sessions is to add their definition to the .bashrc file in your home directory. This can be edited with nano. Note that root privileges are required, so use sudo, like this:

1sudo nano ~/.bashrc

screenshot of a terminal running Linux with the Bash shell.  The editor Nano is running and the contents of .bashrc are displayed.

Exit nano by pressing Ctrl + x and press y to save changes.

Then restart Bash with exec bash.

That’s it, done. Use the newly created aliases ipint and ipext to return IP addresses.

Windows: Creating Aliases In PowerShell To Return The Internal & External IP Address

TL;DR

  1. Open a PowerShell terminal.
  2. Edit file $PROFILE by entering notepad $PROFILE.
  3. Copy/paste the lines below to the end of $PROFILE.
  4. Save and exit notepad.
  5. Reload the new version by entering . $PROFILE.
  6. Done! You can now use the newly created aliases ipint and ipext to return IP addresses in a PowerShell terminal session.
 1function Get-IPInternal { 
 2    $IPAddr = Get-NetIPAddress -AddressFamily IPv4 -PrefixOrigin Dhcp
 3    Write-Output "$($IPAddr.IPAddress)/$($IPAddr.PrefixLength)"
 4}
 5Set-Alias -Name ipint -Value Get-IPInternal
 6
 7
 8function Get-IPExternal {
 9    $Response = Invoke-WebRequest -URI icanhazip.com
10    $Response.content
11}
12Set-Alias -Name ipext -Value Get-IPExternal

How To Retrieve IP Addresses Using PowerShell

Aliases & Functions

So far this blog post has talked about Aliases. These are supported in PowerShell, though they are implemented differently than Linux. There is a restriction regarding Alias parameters and passing values - see “Example 5”. As shown in that example, this can be easily overcome by creating a Function and setting an Alias to it.

Address On The LAN, Using Get-NetIPAddress

PowerShell provides several methods to retrieve IP addresses. The examples below use Get-NetIPAddress. The Microsoft docs page details various parameters for this command. In my examples below I specify IPv4 addresses. Also, I’m requesting IP addresses issued by DHCP. You may want to adjust these to suit your requirements. Examples of the Get-NetIPAddress command include:

 1Get-NetIPAddress -AddressFamily IPv4
 2
 3Get-NetIPAddress -AddressFamily IPv4 | Format-Table 
 4
 5Get-NetIPAddress -AddressFamily IPv4 -PrefixOrigin Dhcp | Format-Table  -Property IPAddress,PrefixLength
 6
 7Get-NetIPAddress -AddressFamily IPv4 -PrefixOrigin Dhcp | Format-Table -HideTableHeaders -Property IPAddress,PrefixLength
 8
 9(Get-NetIPAddress -AddressFamily IPv4 -PrefixOrigin Dhcp | Format-Table -HideTableHeaders -Property IPAddress,PrefixLength | Out-String).Trim()
10
11Get-NetIPAddress -AddressFamily IPv4 -PrefixOrigin Dhcp | Format-Wide -Property IPAddress
12
13(Get-NetIPAddress -AddressFamily IPv4 -PrefixOrigin Dhcp | Format-Wide -Property IPAddress | Out-String).Trim()

This is the version I settled on. It provides the IPv4 address, along with the CIDR suffix. Also, the output exactly matches the Linux version above.

1function Get-IPInternal { 
2    $IPAddr = Get-NetIPAddress -AddressFamily IPv4 -PrefixOrigin Dhcp
3    Write-Output "$($IPAddr.IPAddress)/$($IPAddr.PrefixLength)"
4}
5Set-Alias -Name ipint -Value Get-IPInternal

External IP Address

As with Linux, in order to find the external IP address, a service beyond the LAN needs to be called, such as a server on the internet. Many services exist, this example uses icanhazip.com. This can be changed to your own preferred service.

1curl https://icanhazip.com

PowerShell has Invoke-WebRequest, used like this:

1(Invoke-WebRequest https://icanhazip.com).content

or, using a different external site:

1(Invoke-WebRequest https://ipinfo.io/ip).content

The Linux section above mentions use of the command, dig. In PowerShell, a similar commandlet is availble, Resolve-DnsName. This could be used with a number of DNS providers, for example:

1Resolve-DnsName -Name myip.opendns.com -NoHostsFile -DnsOnly -Server resolver1.opendns.com | Select -ExpandProperty IPAddress
1Resolve-DnsName -Name whoami.akamai.net -NoHostsFile -DnsOnly -Server ns1-1.akamaitech.net | Select -ExpandProperty IPAddress
1Resolve-DnsName -Name o-o.myaddr.l.google.com -Type TXT -NoHostsFile -DnsOnly -Server ns1.google.com | Select -ExpandProperty Strings

This can be simplified, to:

1(Resolve-DnsName myip.opendns.com -Server resolver1.opendns.com).IPAddress

Below, I have used the following function for $PROFILE:

1function Get-IPExternal {
2    (Resolve-DnsName myip.opendns.com -Server resolver1.opendns.com).IPAddress
3}
4Set-Alias -Name ipext -Value Get-IPExternal

Adding Aliases To The $PROFILE File

As with Linux, Aliases in PowerShell typically only exist in the session in which they are created. A simple way to ensure the ipint and ipext persist in PowerShell sessions is to add their definition to the $PROFILE file. This can be edited with an editor, such as notepad or VS Code, like this:

1notepad $PROFILE

or

1code $PROFILE

screenshot of the VS Code editor with the contents of the $PROFILE displayed.

Save the changes exit the editor.

Force a reload the new version by entering . $PROFILE.

That’s it, done. Use the newly created aliases ipint and ipext to return IP addresses.