Linux: 命令行获取公共 IP 和 私有IP, How To Find My Public IP Address, Determine Your Private and Public IP Addresses, From Command Line On a Linux

 

1. 命令行获取私有IP Use dig command for determining my public IP address:

  1. 打开 Terminal application.
  2. 输入以下命令行:Type the following dig (domain information groper) command on a Linux, OS X, or Unix-like operating systems to see your own public IP address assigned by the ISP:
    dig +short myip.opendns.com @resolver1.opendns.com
  3. 或者
    dig TXT +short o-o.myaddr.l.google.com @ns1.google.com
  4. 结果如下图,不借助第三方库下,这是最快获取私有 IP的方式 You should see your IP address on screen. This is the fastest way to find out your IP address without using 3rd party site.

Sample outputs:

Linux: 命令行获取公共 IP 和 私有IP, How To Find My Public IP Address, Determine Your Private and Public IP Addresses, From Command Line On a Linux
Linux: 命令行获取公共 IP 和 私有IP, How To Find My Public IP Address, Determine Your Private and Public IP Addresses, From Command Line On a Linux

You can try host command to see the same information:

host myip.opendns.com resolver1.opendns.com

You can also use the Google server to get the same info using dig command:

How do I store my IP address in a shell variable?

The syntax is:

myip="$(dig +short myip.opendns.com @resolver1.opendns.com)"
echo "My WAN/Public IP address: ${myip}"

Sample outputs:

My WAN/Public IP address: 74.86.144.194

Finding Public/WAN IP address on a router

A few ADSL/Cable router allows you to login to your router using telnet or ssh:

telnet your-router-ip-here
ssh user@your-router-ip-here
telnet 192.168.0.254
ssh admin@192.168.1.254
[admin@dd-wrt ~]#  ifconfig eth1 | grep 'inet addr:' 
[admin@dd-wrt ~]#  ip addr show nas01

 

Use 3rd party web-sites to get your IP

Please note that I do not recommend following curl/wget method due to security reasons. You have been warned:

curl ifconfig.me
curl icanhazip.com
curl ipecho.net/plain
curl ifconfig.co

 

2. 命令行获取公共 IP,Bash Shell Command to Find or Get IP address

使用命令:

ifconfig

#或者
#ifconfig | less

#或者
#ifconfig -a

Sample outputs:

eth0     Link encap:Ethernet  HWaddr 00:0F:EA:91:04:07
  inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
  inet6 addr: fe80::20f:eaff:fe91:407/64 Scope:Link
  UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
  RX packets:31167 errors:0 dropped:0 overruns:0 frame:0
  TX packets:26404 errors:0 dropped:0 overruns:0 carrier:0
  collisions:0 txqueuelen:1000
  RX bytes:38338591 (36.5 MiB)  TX bytes:3538152 (3.3 MiB)
  Interrupt:18 Base address:0xc000

lo        Link encap:Local Loopback
  inet addr:127.0.0.1  Mask:255.0.0.0
  inet6 addr: ::1/128 Scope:Host
  UP LOOPBACK RUNNING  MTU:16436  Metric:1
  RX packets:1994 errors:0 dropped:0 overruns:0 frame:0
  TX packets:1994 errors:0 dropped:0 overruns:0 carrier:0
  collisions:0 txqueuelen:0
  RX bytes:188041 (183.6 KiB)  TX bytes:188041 (183.6 KiB)

Linux ip Command

ip addr show

#或者
ip addr show eth0

Sample outputs:

2: eth0:  mtu 1500 qdisc mq state UP qlen 1000
    link/ether b8:ac:6f:65:31:e5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.100/24 brd 192.168.2.255 scope global eth0
    inet6 fe80::baac:6fff:fe65:31e5/64 scope link 
       valid_lft forever preferred_lft forever

 

Linux ifconfig Example

Type the following command:

ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'

FreeBSD/OpenBSD ifconfig Example

Type the following command:

ifconfig | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'

Sun / Oracle Solaris Unix Example:

Type the following command:

ifconfig -a | grep inet | grep -v '127.0.0.1' | awk '{ print $2}'

Note: use /sbin/ifconfig full path if you are the non-root user.

 

本文:Linux: 命令行获取公共 IP 和 私有IP, How To Find My Public IP Address, Determine Your Private and Public IP Addresses, From Command Line On a Linux

Loading

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.