【FreeBSD】网络管理

Posted by 西维蜀黍 on 2021-03-04, Last Modified on 2024-05-07

netstart

SYNOPSIS

$ /etc/netstart	[[-n] interface ...]

DESCRIPTION

netstart is the command script that is invoked by rc(8) during an automatic reboot and after single-user mode is exited; it performs network initialization.

The netstart script can also be used to start newly created bridges or interfaces, or reset existing interfaces to their default state. The behaviour of this script is (or can be) controlled to some extent by variables defined in rc.conf(8), which specifies which daemons and services are to be run.

During the system boot, netstart is executed. netstart performs the following operations, in the sequence given:

After the system is completely initialized, it is possible to start a newly created interface or bridge(4), or reset an existing interface to its default state, by invoking the following, where foo0 is the interface or bridge name:

$ sh /etc/netstart foo0

Using the -n option reports the steps that would be taken, without actually configuring the interface.

ifconfig - View/Mange Network Interfaces

To display the NIC configuration, enter the following command:

$ ifconfig
dc0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
        options=80008<VLAN_MTU,LINKSTATE>
        ether 00:a0:cc:da:da:da
        inet 192.168.1.3 netmask 0xffffff00 broadcast 192.168.1.255
        media: Ethernet autoselect (100baseTX <full-duplex>)
        status: active
dc1: flags=8802<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
        options=80008<VLAN_MTU,LINKSTATE>
        ether 00:a0:cc:da:da:db
        inet 10.0.0.1 netmask 0xffffff00 broadcast 10.0.0.255
        media: Ethernet 10baseT/UTP
        status: no carrier
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384
        options=3<RXCSUM,TXCSUM>
        inet6 fe80::1%lo0 prefixlen 64 scopeid 0x4
        inet6 ::1 prefixlen 128
        inet 127.0.0.1 netmask 0xff000000
        nd6 options=3<PERFORMNUD,ACCEPT_RTADV>

In this example, the following devices were displayed:

  • dc0: The first Ethernet interface.
  • dc1: The second Ethernet interface.
  • lo0: The loopback device.

FreeBSD uses the driver name followed by the order in which the card is detected at boot to name the NIC. For example, sis2 is the third NIC on the system using the sis(4) driver.

In this example, dc0 is up and running. The key indicators are:

  1. UP means that the card is configured and ready.
  2. The card has an Internet (inet) address, 192.168.1.3.
  3. It has a valid subnet mask (netmask), where 0xffffff00 is the same as 255.255.255.0.
  4. It has a valid broadcast address, 192.168.1.255.
  5. The MAC address of the card (ether) is 00:a0:cc:da:da:da.
  6. The physical media selection is on autoselection mode (media: Ethernet autoselect (100baseTX <full-duplex>)). In this example, dc1 is configured to run with 10baseT/UTP media. For more information on available media types for a driver, refer to its manual page.
  7. The status of the link (status) is active, indicating that the carrier signal is detected. For dc1, the status: no carrier status is normal when an Ethernet cable is not plugged into the card.

View Network Interface Info

$ ifconfig [interface_name]

# e.g.,
$ ifconfig em0

Manage Network Interfaces

To stop network card (NIC) on-fly:

$ ifconfig [network-interface] down

To start network card (NIC) on fly:

$ ifconfig [network-interface] up

为 interface 设置 statis IP

# Assign the	IPv4 address 192.0.2.10, with a	network	mask of	255.255.255.0, to	the interface fxp0:
$ ifconfig fxp0 inet 192.0.2.10 netmask 255.255.255.0

# Add the IPv6 address 2001:DB8:DBDB::123/48	to the interface em0:
$ ifconfig em0 inet6 2001:db8:bdbd::123 prefixlen 48 alias

Misc

# Remove the	IPv4 address 192.0.2.45	from the interface ed0:
$ ifconfig ed0 inet 192.0.2.45 -alias
# Enable IPv6 functionality of the interface:
$ ifconfig em0 inet6	-ifdisabled


# Create the	software network interface gif1:
$ ifconfig gif1 create
# Destroy the software network interface gif1:
$ ifconfig gif1 destroy

dhclient - Set DHCP for an Interface

$ dhclient <interface>
# For example:
$ dhclient vmx1

$ service dhclient restart {interface-name-here}
# e.g.,
$ service dhclient restart em0

Check https://www.freebsd.org/cgi/man.cgi?dhclient for details.

route - 路由设置

View Route Table

  • -r: When netstat is invoked with the routing table option -r, it llists the available routes and their status. Each route consists ofa destination host or network, and a gateway to use in forwarding packets.
  • -n: Do not resolve numeric addresses and port numbers to names.
$ netstat -rn
Routing tables

Internet:
Destination        Gateway            Flags     Netif Expire
default            10.0.0.1           UGS        nfe0
10.0.0.0/24        link#2             U          nfe0
10.0.0.240         link#2             UHS         lo0
127.0.0.1          link#3             UH          lo0
192.168.0.0/24     link#1             U           re0
192.168.0.1        link#1             UHS         lo0
192.168.48.0/22    link#4             U         wlan0
192.168.49.167     link#4             UHS         lo0

Add

Default Route

#A shorter version of adding a default route can also be written as:
$ route add default 192.168.1.1

# Specify by interface
$ route add default -iface em0

Normal Route

# Add a static route	to the 172.16.10.0/24 network via the 172.16.1.1 gateway
$ route add -net 172.16.10.0/24 172.16.1.1
# Or
$ route add 172.16.10.0/24 192.168.48.1
# Or
$ route add -net 172.16.10.0 -netmask 255.255.255.0 192.168.48.1

# Add a static route to a host 8.8.8.8 via the 192.168.48.1 gateway
$ route add -host 8.8.8.8 192.168.48.1

# Specify by interface
$ route add 172.16.2.0/24 -iface em0

Demo

$ netstat -r
Routing tables

Internet:
Destination        Gateway            Flags     Netif Expire
localhost          link#4             UH          lo0
192.168.18.0/24    link#3             U           em0
192.168.18.67      link#3             UHS         lo0

# without that entity within the route table
$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8): 56 data bytes
ping: sendto: No route to host
ping: sendto: No route to host

$ route add -host 8.8.8.8 192.168.18.1
add host 8.8.8.8: gateway 192.168.18.1
$ netstat -r
Routing tables

Internet:
Destination        Gateway            Flags     Netif Expire
8.8.8.8            192.168.18.1       UGHS        em0
localhost          link#4             UH          lo0
192.168.18.0/24    link#3             U           em0
192.168.18.67      link#3             UHS         lo0

$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: icmp_seq=0 ttl=118 time=4.615 ms
64 bytes from 8.8.8.8: icmp_seq=1 ttl=118 time=4.477 ms
...

$ traceroute 8.8.8.8
traceroute to 8.8.8.8 (8.8.8.8), 64 hops max, 40 byte packets
 1  192.168.18.1 (192.168.18.1)  0.657 ms  0.597 ms  0.418 ms
...

Delete

# Delete a static route from the routing table:
$ route delete -net 172.16.10.0/24 172.16.1.2

$ route delete -host 8.8.8.8 192.168.18.1
# Or
$ route delete 8.8.8.8 192.168.18.1

$ route delete default 192.168.18.1

# Delete by specifying the interface
$ route delete -host 8.8.8.8 -iface ix1
$ route delete 172.16.2.0/24 -iface em0


# Remove all routes from the	routing	table:
$ route flush

Route Test

When without routing for a specific destination

$ netstat -r
Routing tables

Internet:
Destination        Gateway            Flags     Netif Expire
localhost          link#4             UH          lo0
192.168.18.0/24    link#3             U           em0
192.168.18.67      link#3             UHS         lo0

# When without routing for a specific destination
$ route show 8.8.8.8
route: route has not been found

Normal case:

$ route add -host 8.8.8.8 192.168.18.1
add host 8.8.8.8: gateway 192.168.18.1
$ netstat -r
Routing tables

Internet:
Destination        Gateway            Flags     Netif Expire
8.8.8.8            192.168.18.1       UGHS        em0
localhost          link#4             UH          lo0
192.168.18.0/24    link#3             U           em0
192.168.18.67      link#3             UHS         lo0

# Display the route for a destination network
$ route show 8.8.8.8
   route to: 8.8.8.8
destination: default
       mask: default
    gateway: 192.168.18.67
        fib: 0
  interface: em0
      flags: <UP,GATEWAY,DONE,STATIC>
 recvpipe  sendpipe  ssthresh  rtt,msec    mtu        weight    expire
       0         0         0         0      1500         1         0

ARP

//TODO

Bridge

The bridge is created using interface cloning. To create the bridge interface:

$ ifconfig bridge create
bridge2
$ ifconfig bridge2
bridge2: flags=8802<BROADCAST,SIMPLEX,MULTICAST> metric 0 mtu 1500
	ether 02:cf:b7:b1:ab:02
	id 00:00:00:00:00:00 priority 32768 hellotime 2 fwddelay 15
	maxage 20 holdcnt 6 proto stp-rstp maxaddr 2000 timeout 1200
	root id 00:00:00:00:00:00 priority 0 ifcost 0 port 0
	groups: bridge
	nd6 options=1<PERFORMNUD>

When a bridge interface is created, it is automatically assigned a randomly generated Ethernet address. The maxaddr and timeout parameters control how many MAC addresses the bridge will keep in its forwarding table and how many seconds before each entry is removed after it is last seen. The other parameters control how STP operates.

Next, specify which network interfaces to add as members of the bridge. For the bridge to forward packets, all member interfaces and the bridge need to be up:

$ ifconfig bridge0 addm fxp0 addm fxp1 up
$ ifconfig fxp0 up
$ ifconfig fxp1 up
$ ifconfig bridge0
bridge0: flags=8802<BROADCAST,SIMPLEX,MULTICAST> metric 0 mtu 1500
	ether 02:cf:b7:b1:ab:01
	inet 0.0.0.0 netmask 0xff000000 broadcast 255.255.255.255
	id 00:00:00:00:00:00 priority 32768 hellotime 2 fwddelay 15
	maxage 20 holdcnt 6 proto stp-rstp maxaddr 2000 timeout 1200
	root id 00:00:00:00:00:00 priority 32768 ifcost 0 port 0
	member: fxp0 flags=143<LEARNING,DISCOVER,AUTOEDGE,AUTOPTP>
	        ifmaxaddr 0 port 2 priority 128 path cost 2000
	member: fxp1 flags=143<LEARNING,DISCOVER,AUTOEDGE,AUTOPTP>
	        ifmaxaddr 0 port 1 priority 128 path cost 2000
	groups: bridge
	nd6 options=9<PERFORMNUD,IFDISABLED>

If the bridge host needs an IP address, set it on the bridge interface, not on the member interfaces. The address can be set statically or via DHCP. This example sets a static IP address:

$ ifconfig bridge0 inet 192.168.0.1/24

Set DNS

$ vim /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
$ sh /etc/rc   
$ /etc/netstart 

Check the /etc/rc.conf config

If you wanna any changes within /etc/re.conf take effect immediately:

$ service netif restart && service routing restart
$ sh /etc/rc   
$ /etc/netstart 

Once the necessary changes to /etc/rc.conf are saved, a reboot can be used to test the network configuration and to verify that the system restarts without any configuration errors. Alternatively, apply the settings to the networking system with this command:

$ service netif restart

# Or

$ ./etc/netstart

Setting a Static IPv4 Address and Gateway

为 interface 设置 statis IP,重启后生效,且永久生效:

$ vim /etc/rc.conf

Add the following:

ifconfig_em0="inet 192.168.0.254 netmask 255.255.255.0"
defaultrouter="192.168.0.1"

Setting DHCP for an Interface

To set a DHCP address for a network interface on a FreeBSD host, you can edit the system configuration file /etc/rc.conf. For example, to set the network interface em0 to DHCP, you set would do the following.

$ vim /etc/rc.conf

Add the following:

ifconfig_[interface]="DHCP", e.g., ifconfig_em0="DHCP"

Set a Bridge

The bridge can now forward Ethernet frames between fxp0 and fxp1. Add the following lines to /etc/rc.conf so the bridge is created at startup:

cloned_interfaces="bridge0"
ifconfig_bridge0="addm fxp0 addm fxp1 up"
ifconfig_fxp0="up"
ifconfig_fxp1="up"

Related Files

  • /etc/dhclient.conf
    • dhclient requires a configuration file, /etc/dhclient.conf. Typically the file contains only comments, the defaults being reasonably sane. This configuration file is described by the dhclient.conf(5) manual page.
  • /sbin/dhclient
    • dhclient is statically linked and resides in /sbin. The dhclient(8) manual page gives more information about dhclient.
  • /sbin/dhclient-script
    • dhclient-script is the FreeBSD-specific DHCP client configuration script. It is described in dhclient-script(8), but should not need any user modification to function properly.
  • /var/db/dhclient.leases
    • The DHCP client keeps a database of valid leases in this file, which is written as a log. dhclient.leases(5) gives a slightly longer description.

Reference

Official

Route

Misc