ネットワークコマンド
ネットワークコマンド
Section titled “ネットワークコマンド”Linuxでのネットワーク操作に使用するコマンドを詳しく解説します。
ping(接続確認)
Section titled “ping(接続確認)”基本的な使い方
Section titled “基本的な使い方”# ホストへの接続を確認ping google.com
# 指定回数だけ実行(-c: count)ping -c 5 google.com
# 間隔を指定(-i: interval)ping -i 2 google.com
# タイムアウトを指定(-W: timeout)ping -W 5 google.com# 接続を確認ping -c 3 8.8.8.8
# ネットワークの問題を診断ping -c 10 google.com | grep "packet loss"netstat / ss(ネットワーク接続確認)
Section titled “netstat / ss(ネットワーク接続確認)”netstatの使い方
Section titled “netstatの使い方”# すべての接続を表示netstat -an
# リスニングポートを表示(-l: listening)netstat -ln
# プロセス情報も表示(-p: program)netstat -lnp
# TCP接続のみ表示(-t: tcp)netstat -ant
# UDP接続のみ表示(-u: udp)netstat -anussの使い方(netstatの代替)
Section titled “ssの使い方(netstatの代替)”# すべての接続を表示ss -an
# リスニングポートを表示ss -ln
# プロセス情報も表示ss -lnp
# TCP接続のみ表示ss -ant
# UDP接続のみ表示ss -anu# 特定のポートが使用されているか確認netstat -ln | grep 8080ss -ln | grep 8080
# 接続しているプロセスを確認netstat -lnp | grep nginxcurl / wget(HTTPリクエスト)
Section titled “curl / wget(HTTPリクエスト)”curlの使い方
Section titled “curlの使い方”# GETリクエストcurl https://api.example.com/users
# POSTリクエストcurl -X POST https://api.example.com/users \ -H "Content-Type: application/json" \ -d '{"name":"Alice","email":"alice@example.com"}'
# ヘッダーを表示(-i: include headers)curl -i https://api.example.com/users
# レスポンスヘッダーのみ表示(-I: head)curl -I https://api.example.com/users
# ファイルをダウンロード(-O: output)curl -O https://example.com/file.txt
# 進捗を表示(-#: progress bar)curl -# -O https://example.com/file.txtwgetの使い方
Section titled “wgetの使い方”# ファイルをダウンロードwget https://example.com/file.txt
# 再帰的にダウンロード(-r: recursive)wget -r https://example.com/
# 進捗を表示(-p: progress)wget -p https://example.com/file.txtifconfig / ip(ネットワーク設定)
Section titled “ifconfig / ip(ネットワーク設定)”ifconfigの使い方
Section titled “ifconfigの使い方”# ネットワークインターフェースの情報を表示ifconfig
# 特定のインターフェースの情報ifconfig eth0
# IPアドレスを設定sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0ipの使い方(ifconfigの代替)
Section titled “ipの使い方(ifconfigの代替)”# ネットワークインターフェースの情報を表示ip addr show# またはip a
# IPアドレスを設定sudo ip addr add 192.168.1.100/24 dev eth0
# ルーティングテーブルを表示ip route showネットワークコマンドのポイント:
- ping: 接続確認、ネットワーク診断
- netstat/ss: ネットワーク接続の確認
- curl/wget: HTTPリクエスト、ファイルダウンロード
- ifconfig/ip: ネットワーク設定、インターフェース管理
適切にネットワークコマンドを使用することで、効率的にネットワーク操作ができます。