【macOS】网络管理

Posted by 西维蜀黍 on 2022-02-23, Last Modified on 2023-05-02

Install

Alternative ip

$ brew install iproute2mac
$ ip link show
$ ip -s link 

Interfaces

List Devices with the Interface Number

$ networksetup -listnetworkserviceorder
(1) Ethernet
(Hardware Port: Ethernet, Device: en0)

(2) USB 10/100/1000 LAN
(Hardware Port: USB 10/100/1000 LAN, Device: en5)

(3) Wi-Fi
(Hardware Port: Wi-Fi, Device: en2)

(4) Bluetooth PAN
(Hardware Port: Bluetooth PAN, Device: en3)

Route

View The Routing Table

$ netstat -nr
Routing tables

Internet:

Destination     Gateway        Flags      Refs     Use     Netif Expire
default         10.1.1.1       UGSc       36       65      jnc0
default         192.168.0.1    UGScI      20       0       en1

Add

Add a Static Route Temporarily

$ sudo route -n add -net 192.168.2.0/24 192.168.1.1

# A destination of default is a synonym for -net 0.0.0.0, which is the default route.

$ sudo route -v add -net 192.168.99.0/24 -interface en0

# Run in test mode (does not do anything, just print):
$ sudo route -t add "destination_ip_address/24" "gateway_address"

To verify the route you added:

$ netstat -rn | grep 192.168.2.
192.168.2 192.168.1.1 UGSc en10
192.168.2/23 1.1.1.1 UGSc utun3

Run in test mode (does not do anything, just print):

$ sudo route -t add "destination_ip_address/24" "gateway_address"

Add a Static Route Permanently

$ networksetup -setadditionalroutes <networkservice> [ <dest> <mask> <gateway> ]*

// e.g.,
$ sudo networksetup -setadditionalroutes "USB 10/100/1000 LAN" 192.168.2.0 255.255.255.0 192.168.1.1

$ sudo networksetup -setadditionalroutes "Ethernet" 192.168.99.0 255.255.255.0 192.168.18.163

# route to direct to the interface
$ sudo networksetup -setadditionalroutes "Ethernet" 192.168.99.0 255.255.255.0 ""

Delete Routes

# Delete a Static Route
$ sudo route -n delete 192.168.2.0/24

# Remove all routes:
$ sudo route flush

Test

# Lookup and display the route for a destination (hostname or IP address):
$ sudo route get "destination"

IP

Set Static IP

Set the TCP/IP configuration for network service to manual with IP address set to ip, Subnet Mask set to subnet, and Router address set to router. For example:

$ networksetup -setmanual "Ethernet" 192.168.100.100 255.255.255.0 192.168.100.1

Set DHCP

Sets the TCP/IP configuration for the specified network service to use DHCP. The client ID is optional. Specify “Empty” for [clientid] to clear the DHCP client id. For example:

$ networksetup -setdhcp "Ethernet"

DNS

Set DNS

Specifies the IP addresses of servers you want the specified network service to use to resolve domain names. You can list any number of servers (replace dns1, dns2, and so on with the IP addresses of domain name servers). If you want to clear all DNS entries for the specified network service, type “empty” in place of the DNS server names.

$ networksetup -setdnsservers <networkservice> <dns1> [dns2]

$ networksetup -setdnsservers "Ethernet" 192.168.100.100 192.168.100.12

ARP

查看ARP表

$ arp -nla
Neighbor                Linklayer Address Expire(O) Expire(I)    Netif Refs Prbs
169.254.255.255         (incomplete)      (none)    (none)         en8
192.168.1.1             88:c3:97:9b:51:60 1m36s     1m29s          en8    1
192.168.1.173           0:e0:4c:6a:56:1b  (none)    (none)         en8
192.168.1.227           8c:85:90:24:c7:dd 2m15s     2m16s          en8    1
192.168.1.255           ff:ff:ff:ff:ff:ff (none)    (none)         en8
224.0.0.251             1:0:5e:0:0:fb     (none)    (none)         en8
239.255.255.250         1:0:5e:7f:ff:fa   (none)    (none)         en0
239.255.255.250         1:0:5e:7f:ff:fa   (none)    (none)         en8

查看当前网络流量

$ nettop

Reference