Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vijayhardaha/b042a71f4b21e6ab3317e067c9702597 to your computer and use it in GitHub Desktop.
Save vijayhardaha/b042a71f4b21e6ab3317e067c9702597 to your computer and use it in GitHub Desktop.
Get and Copy Your Public IP Addresses in the Command Line

Get and Copy Your Public IP Addresses in the Command Line

Frequently, you might need to retrieve and use your public IP addresses in various scenarios. These aliases, ip6 and ip4, simplify the process of obtaining your public IPv6 and IPv4 addresses and copying them to your clipboard.

How to Add and Use the Aliases in Your Shell Configuration Files

You can add the ip6 and ip4 aliases to your shell configuration files (.bashrc, .bash_profile, or .zshrc) for convenient access in every terminal session. Follow these steps to add and use the aliases in your shell configuration:

  1. Open Visual Studio Code or your preferred code editor.

  2. For Bash (.bashrc or .bash_profile) and Zsh (.zshrc):

    a. Bash Configuration: If you're using Bash, open your Bash configuration file. Depending on your system, it might be either ~/.bashrc or ~/.bash_profile.

    b. Zsh Configuration: If you're using Zsh, open your Zsh configuration file, typically ~/.zshrc.

  3. Copy and Paste: Copy and paste the following aliases at the end of your configuration file:

    # Get your public IPv6 address and copy it to the clipboard
    alias ip6="curl ipv6.icanhazip.com/ --silent | xargs -I %s sh -c 'echo %s | pbcopy; echo %s;'"
    
    # Get your public IPv4 address and copy it to the clipboard
    alias ip4="curl ipv4.icanhazip.com/ --silent | xargs -I %s sh -c 'echo %s | pbcopy; echo %s;'"
  4. Save the Changes: Save the changes to your configuration file.

  5. Reload the Configuration:

    • For Bash: In your terminal, either restart your terminal or run:

      source ~/.bashrc
    • For Zsh: In your terminal, either restart your terminal or run:

      source ~/.zshrc
  6. Usage: You can now use the ip6 and ip4 aliases in your Bash or Zsh shell to quickly retrieve and copy your public IPv6 and IPv4 addresses to the clipboard.

    • To get your IPv6 address and copy it to the clipboard:

      ip6
    • To get your IPv4 address and copy it to the clipboard:

      ip4

Conclusion

Adding these aliases to your shell configuration enhances your command-line experience by simplifying the process of obtaining and using your public IPv6 and IPv4 addresses. These custom aliases are valuable for various networking and development tasks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment