LoginSignup
12
7

More than 5 years have passed since last update.

Linuxでインターネットに接続しているIPアドレスを調べるワンライナー

Posted at

ifconfigip address だとループバックなど複数のネットワークインタフェースの情報が表示されるので、そのうちのどれがインターネットに接続しているものなのかを特定するのがちょっと面倒。

いろいろ調べた結果、以下のようにするのがよさそう。

ip route get 8.8.8.8 | head -1 | awk '{print $7}'

補足

$ ip route get 8.8.8.8
8.8.8.8 via 192.168.11.1 dev enp0s31f6 src 192.168.11.105 uid 1000 
    cache 

これで8.8.8.8 につながるときの経路のIPアドレスがわかる。
複数ある場合があるので、head -1 で最初のひとつに絞る。
awkで7番目のフィールドだけ取り出す。

12
7
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
7