タイトル: ip IPアドレスの確認
SEOタイトル: Linux で IP アドレスを確認するコマンド完全ガイド(ip / ifconfig / hostname)
| この記事の要点 |
|
ip コマンド(最新標準)
ip は iproute2 パッケージのコマンドで、ネットワーク設定の現代の標準です。RHEL 7+ / Ubuntu 16.04+ では既定インストール。
# 全インターフェース表示
ip addr show
ip address show
ip a # 短縮形
# 出力例
# 1: lo: mtu 65536 ...
# inet 127.0.0.1/8 scope host lo
# inet6 ::1/128 scope host
# 2: eth0: mtu 1500 ...
# link/ether 00:11:22:33:44:55 brd ff:ff:ff:ff:ff:ff
# inet 192.168.1.10/24 brd 192.168.1.255 scope global dynamic eth0
# inet6 fe80::211:22ff:fe33:4455/64 scope link
# IPv4 のみ
ip -4 addr show
ip -4 a
# IPv6 のみ
ip -6 addr show
# 簡潔な 1 行表示
ip -br addr show
# 出力例:
# lo UNKNOWN 127.0.0.1/8 ::1/128
# eth0 UP 192.168.1.10/24 fe80::.../64
インターフェース指定
# 特定インターフェースだけ
ip addr show eth0
ip a show eth0
ip a eth0 # show 省略可
# 名前で grep
ip addr show | grep -E '^[0-9]+:|inet '
# インターフェース一覧だけ
ip link show
ip -br link show
ls /sys/class/net/
ifconfig(旧来のコマンド)
ifconfig は net-tools パッケージに含まれる旧コマンド。RHEL 8+ / Ubuntu 18.04+ では既定では入っていませんが、まだ多くのドキュメントで使われています。
# インストール(必要なら)
sudo apt install net-tools # Ubuntu/Debian
sudo dnf install net-tools # RHEL/CentOS
# 全インターフェース
ifconfig
ifconfig -a # ダウン中の IF も表示
# 特定インターフェース
ifconfig eth0
# 出力例
# eth0: flags=4163 mtu 1500
# inet 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255
# inet6 fe80::211:22ff:fe33:4455 prefixlen 64 scopeid 0x20
# ether 00:11:22:33:44:55 txqueuelen 1000 (Ethernet)
素早く IP だけ取り出す
# IPv4 だけ複数行で
hostname -I # 192.168.1.10 10.0.0.5
# 1 つ目だけ取り出し
hostname -I | awk '{print $1}'
# eth0 の IPv4 を変数に
IP=$(ip -4 -br addr show eth0 | awk '{print $3}' | cut -d/ -f1)
echo $IP
# 別解
ip -4 -o addr show eth0 | awk '{print $4}' | cut -d/ -f1
外部から見た IP(グローバル IP)
NAT 配下のサーバーで「インターネットから見た自分の IP」を知りたい場合:
# 公開サービスを使う
curl ifconfig.me
curl ifconfig.io
curl icanhazip.com
curl ipinfo.io/ip
curl -s https://api.ipify.org
# DNS 経由(高速)
dig +short myip.opendns.com @resolver1.opendns.com
nslookup myip.opendns.com resolver1.opendns.com
経路 / デフォルトゲートウェイ
# ルーティングテーブル
ip route show
ip route
ip r # 短縮
# 出力例
# default via 192.168.1.1 dev eth0 proto dhcp metric 100
# 192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10
# デフォルトゲートウェイだけ
ip route show default
ip route | awk '/default/ {print $3}'
# 特定の宛先がどの経路を使うか
ip route get 8.8.8.8
# → 8.8.8.8 via 192.168.1.1 dev eth0 src 192.168.1.10
# 旧コマンドでは
route -n
netstat -rn
DNS / ネットワーク全体確認
# DNS サーバー
cat /etc/resolv.conf
resolvectl status # systemd-resolved
# hostname
hostname
hostname -f # FQDN
hostname -I # IP
# 全部入り(最近の RHEL/Ubuntu)
nmcli # NetworkManager
nmcli device show
# 詳細情報
ss -tlnp # LISTEN 中のソケット(netstat -tlnp の代替)
ss -tunp # 全ソケット
Windows での同等コマンド
# cmd.exe
ipconfig # 全情報
ipconfig /all # 詳細(DNS / DHCP も)
# PowerShell
Get-NetIPAddress
Get-NetIPAddress -AddressFamily IPv4
Get-NetIPConfiguration # 包括的に
# 特定アダプタ
Get-NetIPAddress -InterfaceAlias "イーサネット"
# 経路
Get-NetRoute -DestinationPrefix "0.0.0.0/0"
route print # 旧
macOS での同等
# macOS は ifconfig がまだ標準
ifconfig
ifconfig en0 # Wi-Fi
ifconfig en1 # 有線(モデルによる)
# 簡潔
ipconfig getifaddr en0 # IPv4 だけ
networksetup -getinfo Wi-Fi
# ip コマンドは未搭載(brew install iproute2mac で代替可)
典型的なトラブル切り分け
ip aで IP が割り当たっているか確認ip routeでデフォルトゲートウェイがあるかping -c 3 デフォルトGWで L2 疎通確認ping -c 3 8.8.8.8で L3 (外向き) 疎通nslookup google.comで DNS 解決curl -v https://google.comで HTTPS / 証明書確認
FAQ
Q: ifconfig: command not found
A: 最近のディストリでは net-tools パッケージが既定で入りません。sudo apt install net-tools(Ubuntu)/ sudo dnf install net-tools(RHEL)でインストール、または ip コマンドに移行を推奨します。
Q: IP が DHCP で取得できているか確認したい
A: ip a の出力で dynamic という単語が IPv4 行に付いていれば DHCP 取得です。journalctl -u NetworkManager や /var/log/syslog で取得ログも確認可能。
Q: 一時的に IP を変更したい
A: sudo ip addr add 192.168.1.20/24 dev eth0。再起動で消えます。永続化は /etc/netplan(Ubuntu)/ nmcli(RHEL)で。