如何创建静态永久ifconfig别名?


7

我怎么得到这个 ifconfig loopback别名更改为重启后是否仍然存在?

$ sudo ifconfig lo0 alias 172.16.222.111
$ ifconfig
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
    options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>
    inet 127.0.0.1 netmask 0xff000000 
    inet6 ::1 prefixlen 128 
    inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
    inet 172.16.222.111 netmask 0xffff0000          ### <-- WANTED!!!
    nd6 options=201<PERFORMNUD,DAD>

我发现的唯一类似指令是针对其他* nix系统上不存在的OSX文件 /etc/network/interfaces 要么 /etc/sysconfig/networking-scripts/ifcfg-eth0

Answers:


12

一般来说,您可以在“系统偏好设置”的“网络”中创建持久性别名。

enter image description here enter image description here

然而 lo0 环回设备没有显示在这里。对于这种特殊情况,我们需要利用 launchd 用这个开始一个简短的剧本 ifconfig 你上面使用的命令。

这是一个示例.plist文件,保存为 com.user.lo0-loopback.plist (可以保存在任何地方,因为稍后会将其复制到相应的目录中)。

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd > 
<plist version="1.0"> 
<dict> 
  <key>Label</key> 
  <string>com.user.lo0-loopback</string> 
  <key>ProgramArguments</key> 
  <array> 
    <string>/sbin/ifconfig</string> 
    <string>lo0</string> 
    <string>alias</string> 
    <string>172.16.222.111</string> 
  </array> 
  <key>RunAtLoad</key> <true/> 
  <key>Nice</key> 
  <integer>10</integer> 
  <key>KeepAlive</key> 
  <false/> 
  <key>AbandonProcessGroup</key> 
  <true/> 
  <key>StandardErrorPath</key> 
  <string>/var/log/loopback-alias.log</string> 
  <key>StandardOutPath</key> 
  <string>/var/log/loopback-alias.log</string> 
</dict> 
</plist>

接下来,将其移至 /Library/LaunchDaemons/ 目录所以它在启动时启动(将作为 root )并设置正确的权限

$ cp com.user.lo0-loopback.plist /Library/LaunchDaemons/ 

$ chmod 0644 /Library/LaunchDaemons/com.user.lo0-loopback.plist 
$ chown root:wheel /Library/LaunchDaemons/com.user.lo0-loopback.plist

然后加载它 launchctl

$ launchctl load /Library/LaunchDaemons/com.user.lo0-loopback.plist

重启和你的 lo0 loopback应该有一个分配给它的别名IP,它将在重新启动后保持不变。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.