MacOS如何通过Shell脚本快速修改IP子网掩码网关DNS

文章来源

By Ross:
/*
列出本机所有网络服务:

$ networksetup -listallnetworkservices
An asterisk (*) denotes that a network service is disabled.
USB 10/100/1000 LAN
USB 10/100/1000 LAN 2
Wi-Fi
Bluetooth PAN
Thunderbolt Bridge
$

设置Wi-Fi静态IP和指定DNS

$ sudo networksetup -setmanual "Wi-Fi" 192.168.0.228 255.255.255.0 192.168.0.1
Password: 手动输入MacBook用户密码
$ sudo networksetup -setdnsservers Wi-Fi 223.5.5.5 114.114.114.114

设置Wi-Fi自动获取IP和DNS

$ sudo networksetup -setdhcp "Wi-Fi"
$ sudo networksetup -setdnsservers Wi-Fi empty

*/

MacOS 如何通过shell脚本快速修改IP子网掩码网关DNS等?

每次换IP手动在图形化里设置太麻烦了。。 Windows可以通过bat做快速设置,mac求助?

列出本机所有网络服务

$ networksetup -listallnetworkservices
An asterisk (*) denotes that a network service is disabled.
MT65xx Preloader
Thunderbolt
Wi-Fi
iPhone USB
Bluetooth PAN
Thunderbolt Bridge
VPN (Cisco IPSec)

设置Wi-Fi自动获取IP

sudo networksetup -setdhcp "Wi-Fi"

设置Wi-Fi静态IP

sudo networksetup -setmanual "Wi-Fi” 192.168.0.13 255.255.255.0 192.168.0.1

设置Thunderbolt自动获取IP

sudo networksetup -setdhcp "Thunderbolt"

设置Thunderbolt静态IP

sudo networksetup -setmanual "Thunderbolt" 192.168.0.14 255.255.255.0 192.168.0.1

设置Thunderbolt BootP

sudo networksetup -setbootp "Thunderbolt"

设置Thunderbolt DNS DHCP

sudo networksetup -setdnsservers Thunderbolt empty

设置Thunderbolt DNS 指定DNS

sudo networksetup -setdnsservers Thunderbolt 114.114.114.114 8.8.8.8

设置Wi-Fi DNS DHCP

sudo networksetup -setdnsservers Wi-Fi empty

设置Wi-Fi DNS 指定DNS

sudo networksetup -setdnsservers Wi-Fi 114.114.114.114 8.8.8.8


How To Set a Static IP Address in macOS Using Command Line

Posted on 2021年9月25日
https://dev.to/dunithd/how-to-set-a-static-ip-address-in-macos-using-command-line-4j1b
Imagine you have an Apple device like a Macmini, MacBook, or an iMac in a remote location. You only have access to it via SSH and no Desktop GUI interaction is allowed.
What if you wanted to change the IP address of the device? For example, how do you assign a static IP address instead of DHCP?
Command line is your option in that case.
Let's see how to get it done...

Make sure you are in the sudoers list

To make changes like network interface configurations, you need to be a user with super user privileges.

Follow the below commands

First, get a list of all your network services.
networksetup -listallnetworkservices
That command will result in an output like below.

> networksetup -listallnetworkservices
An asterisk (*) denotes that a network service is disabled.
Apple USB Ethernet Adapter
USB 10/100/1000 LAN
Wi-Fi
Bluetooth PAN
Thunderbolt Bridge

Then use the desired service name in the following command:
networksetup -setmanual SERVICE IP SUBNET ROUTER
For example, to change the IP address of my Wi-Fi interface, I would use a command like this:
networksetup -setmanual Wi-Fi 192.168.1.2 255.255.255.0 192.168.1.1
In the above example, 192.168.1.2 is my new IP address. 192.168.1.1 is the address of the default gateway, which is the router most of the time.

Setting it back to DHCP

To set it back to DHCP, use the following command:
networksetup -setdhcp SERVICE

Keep DHCP with a manual IP

To keep DHCP services but only use a manually designated IP address, then use the following command:
networksetup -setmanualwithdhcprouter SERVICE IP

Confirm settings are permanent

Restart the device sudo shutdown -r now and see if the new address persists.
If everything goes well, it should persist.

你可能感兴趣的:(MacOS如何通过Shell脚本快速修改IP子网掩码网关DNS)