虽然 macOS 在“系统偏好(System Preferences)设置”菜单中为您的网络设置提供了一个易于使用的菜单,但如果您想快速查找或测试有关网络配置的信息,则需要使用终端应用程序。(Terminal)您可以使用它来查找您的 IP 地址、查找您的位置、检查您的系统防火墙等等。
可以使用一些常见的终端命令来访问您的网络设置,这些命令在大多数情况下只需要很少的配置即可使用。我们仍然建议您使用系统偏好设置(System Preferences)应用程序进行配置(除非您乐于使用终端),但识别您的Mac终端网络设置很容易。
使用网络设置(Using networksetup)
networksetup工具提供了大量有关您当前Mac网络配置的信息。您可以使用它来查找您的计算机名称、IP 地址、当前WiFi网络等。顾名思义,您也可以使用它来更改设置,但我们仍然建议使用系统(System) 偏好设置(Preferences)来执行此操作。
您可以使用 networksetup 工具查看潜在Mac终端网络命令的完整列表,方法是在终端键入networksetup -help。这将显示帮助列表,以及如何使用该工具查看和更改不同网络设置的各种示例。
可用于查看网络信息的networksetup命令示例包括:
- 要查看您的 Mac 计算机名称:networksetup -getcomputername。
- 列出所有Mac网络连接:networksetup -listallhardwareports
- 要显示当前连接的WiFi网络:networksetup -getairportname deviceid。将deviceid替换为(deviceid)networksetup -listallhardwareports命令中的设备 ID 。
使用 ipconfig(Using ipconfig)
ipconfig工具在 Windows 和 macOS 计算机中很常见,但与(Windows)Windows版本(Windows)不同,它不是更改网络设置的最有用工具。但是,它可能有用的地方是列出有关您当前网络配置的信息。
在终端输入ipconfig将列出所有可用的命令,其中包括:
- 要查看您当前的网络 IP 地址:ipconfig getifaddr deviceid。将deviceid替换为正确的网络设备 ID(例如en0)。如果您不知道,请键入networksetup -listallhardwareports 。
- 要查看您当前的网络DNS服务器:ipconfig getoption deviceid domain_name_server(将deviceid替换为您的网络设备 ID)。
使用 ifconfig(Using ifconfig)
ifconfig命令是另一个可供 macOS 和(ifconfig)Linux PC(Linux PCs)用户使用的网络配置工具。然而,与(Unlike)ipconfig 不同的是,ifconfig 是一个更强大的工具,用于查看和修改网络设置。
但是,您只需在终端键入ifconfig即可查看连接或集成到Mac的所有网络设备的详细信息列表。这包括 IP 和MAC地址、当前设备状态等。
您可以通过列出设备 ID(例如ifconfig en0)来查看特定信息。
使用 ping(Using ping)
虽然您无法使用它查看任何网络信息,但您可以使用ping命令测试您是否可以与其他网络设备联系。它可以是您网络上的设备(例如,您的网络路由器)或网站域或互联网 IP 地址,以测试您的互联网连接。
每当您的设备在连接到本地网络上的另一台设备或互联网上的设备或网站时出现问题时,您都需要使用 ping 作为故障排除工具。它将显示发送和返回信息所需的时间,并将持续运行,直到您决定结束它。
要使用它,请输入ping address,将address替换为 IP 地址或域名。一个常见的测试目标是google.com — 如果您无法访问Google,您可能没有互联网连接。
同样,ping 192.168.1.1将测试许多本地网络路由器 (192.168.1.1) 的 IP 地址。
使用网络统计(Using netstat)
netstat工具列出了有关您当前传入和传出网络连接的信息。可以使用此工具列出与您的Mac建立的任何连接。Windows和Linux PC(Linux PCs)也使用 netstat,但存在一些差异,Mac用户可以使用不同的标志。
您可以通过多种方式使用 netstat 查看当前的网络设置或连接。这些包括:
- 有关所有活动 Internet 连接的当前列表:netstat
- 要查看接口的连接数据:netstat -l deviceid,将deviceid替换为您的网络接口名称(例如netstat -l en0)。
- 查看 IP 路由表:netstat -nr或netstat -r
- 显示所有网络统计信息:netstat -s和netstat -i
有关如何使用 netstat 命令以及帮助破译一些复杂技术术语的更多信息,请键入man netstat以查看并通读包含的 netstat 手册页。
使用 lsof(Using lsof)
您可以使用lsof命令来查看Mac上具有活动网络连接的任何正在运行的进程。这取代了您在 Windows 或 Linux PC 上使用netstat命令找到的类似功能。(netstat)
您可以通过多种方式使用 lsof Mac终端命令查看网络数据。这些包括:
- 查看所有打开的网络连接:lsof -i
- 查看哪些软件正在使用哪些端口:lsof -n -i4TCP
有关更多信息,请键入man lsof查看 lsof 命令的手册页。
使用 arp(Using arp)
如果要查看本地网络上所有活动设备的列表,可以使用arp工具。这将根据这些设备所做的ARP(地址解析协议)广播列出(Address Resolution Protocol)Mac在网络上检测到的任何设备的 IP 和MAC地址。(MAC)
在终端输入arp -a将为您提供这些设备的列表。
然后,您可以将此处找到的信息与ping等其他命令结合起来,以确定这些设备是否仍处于活动状态并且可以通过您的Mac进行通信。
配置 Mac 网络设置(Configuring Your Mac Network Settings)
使用这些工具查看您的Mac终端网络设置后,您可以确定您可能希望(或需要)更改的设置。例如,您可能需要在 Mac 上欺骗 MAC 地址(spoof a MAC address)以绕过(bypass MAC address filtering)访客 WiFi 网络上的 MAC 地址过滤。
它还可以帮助您识别问题,尤其是当您的Mac 经常断开其 WiFi 连接(Mac drops its WiFi connection regularly)时。如果您的Mac出现问题,Mac 版 OnyX(OnyX for Mac)等应用程序可以帮助您快速恢复并运行。
How to Use Mac Terminal to Identify Network Settings
While macOS offers an easy-to-use menu for your network settings in the System Preferences menu, the Terminal aрp is wherе you need to be if you want to quickly lookup or test informаtion about your network configuration. Yoυ can use it to find your IP address, find your locаtiоn, check your system firewall, and more.
Your network settings can be accessed using some common terminal commands that, for the most part, require very little configuration to use. We still recommend you use the System Preferences app for configuration (unless you’re happy using the terminal), but identifying your Mac terminal network settings is easy.
Using networksetup
The networksetup tool offers an enormous amount of information on your current Mac network configuration. You can use it to find your computer name, IP address, current WiFi network, and more. As the name suggests, you can also use it to change settings, but we’d still recommend using System Preferences to do this.
You can view a full list of potential Mac terminal network commands using the networksetup tool by typing networksetup -help at the terminal. This will display the help list, with various examples of how to use the tool to view and change different network settings.
Examples of networksetup commands you can use to view network information include:
- To view your Mac computer name: networksetup -getcomputername.
- To list all Mac network connections: networksetup -listallhardwareports
- To display the current, connected WiFi network: networksetup -getairportname deviceid. Replace deviceid with a device ID from the networksetup -listallhardwareports command.
Using ipconfig
The ipconfig tool is common to Windows and macOS computers but, unlike the Windows version, it isn’t the most useful tool for changing network settings. Where it can be useful, however, is listing information on your current network configuration.
Typing ipconfig at the terminal will list all available commands, but these include:
- To view your current network IP address: ipconfig getifaddr deviceid. Replace deviceid with the correct network device id (eg. en0). Type networksetup -listallhardwareports if you don’t know this.
- To view your current network DNS server: ipconfig getoption deviceid domain_name_server (replacing deviceid with your network device id).
Using ifconfig
The ifconfig command is another network configuration tool available to users on macOS and Linux PCs. Unlike ipconfig, however, ifconfig is a much more powerful tool for viewing and modifying your network settings.
However, you only need to type ifconfig at the terminal to view a detailed list of information for all of the network devices connected or integrated into your Mac. That includes IP and MAC addresses, current device status, and more.
You can view specific information by listing the device id (for example, ifconfig en0) instead.
Using ping
While you can’t use it to view any network information, you can use the ping command to test whether or not you can make contact with another network device. It could be a device on your network (for instance, your network router) or a website domain or internet IP address to test your internet connectivity.
You’ll want to use ping as a troubleshooting tool whenever your device seems to be having issues with connecting to another device on your local network, or a device or website on the internet. It will show the time taken for information to be sent and returned and will run continuously until you decide to end it.
To use it, type ping address, replacing address with an IP address or domain name. A common target for testing is google.com—if you can’t hit Google, you probably don’t have internet connectivity.
Likewise, ping 192.168.1.1 will test the IP address for many local network routers (192.168.1.1).
Using netstat
The netstat tool lists information on your current incoming and outgoing network connections. Any connections made to your Mac can be listed using this tool. Windows and Linux PCs also use netstat, but there are some differences, with different available flags to Mac users.
There are several ways you can use netstat to view current network settings or connections. These include:
- For a current list of all active internet connections: netstat
- To view connection data for an interface: netstat -l deviceid, replacing deviceid with your network interface name (eg. netstat -l en0).
- To view the IP routing table: netstat -nr or netstat -r
- To show all network statistics: netstat -s and netstat -i
For more information on how to use the netstat command and to help decipher some of the complex technical terminologies, type man netstat to view and read through the included netstat man page.
Using lsof
You can use the lsof command as a way to view any running processes on your Mac that have active network connections. This replaces similar functionality that you’d find with the netstat command on Windows or Linux PCs.
There are several ways you can use the lsof Mac terminal command to view network data. These include:
- To view all open network connections: lsof -i
- To view what software is using what ports: lsof -n -i4TCP
For more information, type man lsof to view the man page for the lsof command.
Using arp
If you want to view a list of all active devices on a local network, you could use the arp tool. This will list the IP and MAC addresses for any devices that your Mac has detected on your network, based on the ARP (Address Resolution Protocol) broadcasts those devices have made.
Typing arp -a at the terminal will provide you with a list of these devices.
You could then combine the information found here with other commands like ping to determine whether or not those devices are still active and can be communicated with from your Mac.
Configuring Your Mac Network Settings
With your Mac terminal network settings in view using these tools, you can identify the settings you may prefer to (or need to) change. For instance, you may need to spoof a MAC address on your Mac to bypass MAC address filtering on a guest WiFi network.
It can also help you identify problems, especially if your Mac drops its WiFi connection regularly. If your Mac is having issues, apps like OnyX for Mac can help you get back up and running quickly.