2026年5月10日 星期日

FreeBSD 基本操作

環境:FreeBSD 15.0

  • 查看版本,使用 freebsd-version
    參數:
    -k:已安裝核心的版本和補丁等級
    -r:正在運行核心的版本和補丁層級
    -u:已安裝使用者空間(userland)的版本和補丁層級
    (https://en.wikipedia.org/wiki/User_space_and_kernel_space)
    -j jail:指定 jail(由 jid 或 name 指定)中已安裝使用者空間的版本和修補程式等級
    (https://docs.freebsd.org/zh-tw/books/handbook/jails/)
    $ freebsd-version -kru
    15.0-RELEASE
    15.0-RELEASE
    15.0-RELEASE
  • 預設沒裝 sudo
    使用 su - 會出現「su: Sorry」,須將使用者加入 wheel 群組
    $ pw groupmod wheel -m 使用者帳號
  • 預設 SSH 不允許 root 遠端登入,SSH 設定檔在 /etc/ssh/sshd_config
    若要可 SSH 登入,修改定檔
    $ vi /etc/ssh/sshd_config
    PermitRootLogin yes
    重新啟動 SSH
    $ service sshd restart
  • 新增設定網卡,設定檔在 /etc/rc.conf 
    例如有兩網卡 em0、em1,設定檔只有 em0,則 em1 會沒 IP
    $ cat /etc/rc.conf
    hostname="test.example.com"
    ifconfig_em0="DHCP"
    ifconfig_em0_ipv6="inet6 accept_rtadv"
    sshd_enable="YES"
    ntpd_enable="YES"
    ntpd_sync_on_start="YES"
    moused_nondefault_enable="NO"
    # Set dumpdev to "AUTO" to enable crash dumps, "NO" to disable
    dumpdev="AUTO"
    zfs_enable="YES"
    $ ifconfig
    em0: flags=1008843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST,LOWER_UP> metric 0 mtu 1500
            options=48505bb<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,JUMBO_MTU,VLAN_HWCSUM,TSO4,LRO,VLAN_HWFILTER,VLAN_HWTSO,HWSTATS,MEXTPG>
            ether 08:00:27:f6:65:db
            inet 192.168.100.4 netmask 0xffffff00 broadcast 192.168.100.255
            inet6 fe80::a00:27ff:fef6:65db%em0 prefixlen 64 scopeid 0x1
            media: Ethernet autoselect (1000baseT <full-duplex>)
            status: active
            nd6 options=23<PERFORMNUD,ACCEPT_RTADV,AUTO_LINKLOCAL>
    em1: flags=1008802<BROADCAST,SIMPLEX,MULTICAST,LOWER_UP> metric 0 mtu 1500
            options=48505bb<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,JUMBO_MTU,VLAN_HWCSUM,TSO4,LRO,VLAN_HWFILTER,VLAN_HWTSO,HWSTATS,MEXTPG>
            ether 08:00:27:c5:be:b9
            media: Ethernet autoselect (1000baseT <full-duplex>)
            status: active
            nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
    lo0: flags=1008049<UP,LOOPBACK,RUNNING,MULTICAST,LOWER_UP> metric 0 mtu 16384
            options=680003<RXCSUM,TXCSUM,LINKSTATE,RXCSUM_IPV6,TXCSUM_IPV6>
            inet 127.0.0.1 netmask 0xff000000
            inet6 ::1 prefixlen 128
            inet6 fe80::1%lo0 prefixlen 64 scopeid 0x3
            groups: lo
            nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>
    新增 em1 網卡,設為 DHCP
    $ vi /etc/rc.conf
    $ cat /etc/rc.conf
    hostname="test.example.com"
    ifconfig_em0="DHCP"
    ifconfig_em0_ipv6="inet6 accept_rtadv"
    sshd_enable="YES"
    ntpd_enable="YES"
    ntpd_sync_on_start="YES"
    moused_nondefault_enable="NO"
    # Set dumpdev to "AUTO" to enable crash dumps, "NO" to disable
    dumpdev="AUTO"
    zfs_enable="YES"
    
    # 新增 em1
    ifconfig_em1="DHCP"
    重啟 em1 網卡
    $ service netif restart em1
    $ ifconfig
    em0: flags=1008843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST,LOWER_UP> metric 0 mtu 1500
            options=48505bb<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,JUMBO_MTU,VLAN_HWCSUM,TSO4,LRO,VLAN_HWFILTER,VLAN_HWTSO,HWSTATS,MEXTPG>
            ether 08:00:27:f6:65:db
            inet 192.168.100.4 netmask 0xffffff00 broadcast 192.168.100.255
            inet6 fe80::a00:27ff:fef6:65db%em0 prefixlen 64 scopeid 0x1
            media: Ethernet autoselect (1000baseT <full-duplex>)
            status: active
            nd6 options=23<PERFORMNUD,ACCEPT_RTADV,AUTO_LINKLOCAL>
    em1: flags=1008843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST,LOWER_UP> metric 0 mtu 1500
            options=48505bb<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,JUMBO_MTU,VLAN_HWCSUM,TSO4,LRO,VLAN_HWFILTER,VLAN_HWTSO,HWSTATS,MEXTPG>
            ether 08:00:27:c5:be:b9
            inet 192.168.10.142 netmask 0xffffff00 broadcast 192.168.10.255
            media: Ethernet autoselect (1000baseT <full-duplex>)
            status: active
            nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
    lo0: flags=1008049<UP,LOOPBACK,RUNNING,MULTICAST,LOWER_UP> metric 0 mtu 16384
            options=680003<RXCSUM,TXCSUM,LINKSTATE,RXCSUM_IPV6,TXCSUM_IPV6>
            inet 127.0.0.1 netmask 0xff000000
            inet6 ::1 prefixlen 128
            inet6 fe80::1%lo0 prefixlen 64 scopeid 0x3
            groups: lo
            nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>

    也可重啟 em1 DHCP
    $ service dhclient restart em1
    Stopping dhclient.
    Starting dhclient.
    DHCPREQUEST on em1 to 255.255.255.255 port 67
    DHCPACK from 192.168.10.10
    bound to 192.168.10.142 -- renewal in 21600 seconds.

    查看 DHCP 取得 IP 的租約資料
    $ cat /var/db/dhclient.leases.em1
    lease {
      interface "em1";
      fixed-address 192.168.10.142;
      next-server 192.168.10.10;
      option subnet-mask 255.255.255.0;
      option routers 192.168.10.1;
      option domain-name-servers 8.8.8.8;
      option host-name "test";
      option broadcast-address 192.168.10.255;
      option dhcp-lease-time 43200;
      option dhcp-message-type 5;
      option dhcp-server-identifier 192.168.10.10;
      option dhcp-renewal-time 21600;
      option dhcp-rebinding-time 37800;
      renew 0 2026/5/10 18:14:29;
      rebind 0 2026/5/10 22:44:29;
      expire 1 2026/5/11 00:14:29;
    }
    lease {
      interface "em1";
      fixed-address 192.168.10.142;
      next-server 192.168.10.10;
      option subnet-mask 255.255.255.0;
      option routers 192.168.10.1;
      option domain-name-servers 8.8.8.8;
      option host-name "test";
      option broadcast-address 192.168.10.255;
      option dhcp-lease-time 43200;
      option dhcp-message-type 5;
      option dhcp-server-identifier 192.168.10.10;
      option dhcp-renewal-time 21600;
      option dhcp-rebinding-time 37800;
      renew 0 2026/5/10 18:16:12;
      rebind 0 2026/5/10 22:46:12;
      expire 1 2026/5/11 00:16:12;
    }



沒有留言:

張貼留言