FreeBSD

From Leo's Notes
Last edited on 1 September 2019, at 01:36.

FreeBSD is a Unix-like operating system descended from the Berkeley Software Distribution (BSD). Unlike Linux, the FreeBSD project maintains a complete operating system and is licensed under the more permissive BSD license.

Configuration

Most of the configuration for the operating system can be made by editing the system configuration file /etc/rc.conf. The file does not directly trigger commands or actions to happen directly, but is loaded by various scripts in /etc/ that conditionalizes their actions.

Default settings not specified in /etc/rc.conf are included from /etc/defaults/rc.conf. Additional settings can also be placed in separate files in /etc/rc.conf.d.

Network

Similar to Linux, network adapters and their settings can be displayed using ifconfig.

The machine hostname can be set by defining the hostname value in /etc/rc.conf. Eg:

hostname="freebsd"

DHCP

To use DHCP, add the following lines to /etc/rc.conf:

ifconfig_bge0="DHCP"
ifconfig_bge0_ipv6="inet6 accept_rtadv"

Where bge0 is the interface name.

Static

For static IP assignment, edit /etc/rc.conf:

ifconfig_bge0="inet 10.0.0.3 netmask 255.255.255.0"
defaultrouter="10.0.0.1"

Applying Network Changes

Apply any network configuration changes by restarting these services:

# /etc/rc.d/netif restart
# /etc/rc.d/routing restart

Kernel Modules

Kernel modules can be loaded/unloaded or listed like you would on linux.

FreeBSD Linux Description
# kldload
# modprobe
Loads a kernel module
# kldunload
# rmmod
Removes a module
# kldstat
# lsmod
Lists loaded modules

Most modules should have a startup script which gets triggered by an entry in /etc/rc.conf. Alternatively, it can also be loaded by adding a line to /etc/loader.conf.

Time

Timezones are configured by copying or symlinking the proper tzdata file from /usr/share/zoneinfo to /etc/localtime. A symbolic link might not be preferable if /usr isn't mounted on boot time.

Configure NTP by editing /etc/rc.conf:

ntpdate_enable="YES"
ntpdate_flags="-b ntp.cpsc.ucalgary.ca"

Time can be synchronized immediately via NTP by running ntpdate ntp.cpsc.ucalgary.ca.

Bootloader

The FreeBSD loader is a program that provides an interactive boot screen, reads a boot configuration file, and starts the kernel. It can be configured by editing /boot/loader.conf.

autoboot_delay="5"

Single user mode can be entered by booting with -s. Single user mode can be protected by adding insecure to the console in /etc/ttys.


Package Management

There are two ways to obtain software on your FreeBSD machine: Through precompiled packages or using FreeBSD's port collection.

Pkg Package Management

pkg is package management utility that downloads and installs packages from FreeBSD's pkg service at http://pkg.freebsd.org/ and is available on FreeBSD versions after 10.x.

The /usr/sbin/pkg should already be installed as part of the system.

Refer to the following table for a quick usage guide and its equivalent yum or dnf commands.

RedHat / Fedora FreeBSD Description
rpm -qa pkg info [pkg name] Returns a list of installed packages
yum install [package] pkg install [package] Installs a package with its dependencies
yum remove [package] pkg delete [package] Removes the package from the system
yum update pkg upgrade Updates installed packages
package-cleanup --leaves

or dnf [list] autoremove

pkg autoremove Removes leaf dependencies
yum clean [all] pkg clean [-a] Cleans package cache information

A neat feature for the security conscious is pkg audit -F which lists all known vulnerabilities for software that is installed on the system.

FreeBSD Ports

The ports collection is a set of makefiles, patches, and description files stored in /usr/ports. These files provide a way to bootstrap the build and install of applications to your system.

The ports collection can be obtained by downloading and extracting it to /usr/ports using portsnap.

## Download the ports collection to /var/db/portsnap
# portsnap fetch
## ... and extract it to /usr/ports
# portsnap extract

Since the port collection will change periodically with new applications and software updates, an update can be pulled by running:

# portsnap fetch update

Using Ports

To compile and install a port, go to /usr/ports and navigate to a port you wish to install and run make install.

Depending on the application, you will be asked to configure build options that are available for the program as well as its dependencies as they're built. You can configure all build options before the compilation process using make config-recursive prior to make install. To accept the default configurations set by the package, define an environment variable BATCH=yes when running make install.

After building a port, it is a good idea to clean the port's working directory to reduce wasted space by running make clean.

To remove an installed port, use either pkg delete or run make deinstall in the port's directory.

Portmaster

portmaster is a shell script used for installing and updating installed ports on the system. It is capable of reading package information from /var/db/pkg to determine which packages need to be updated.

portmaster can be installed via ports:

# portsnap fetch update
# cd /usr/ports/ports-mgmt/portmaster
# make install clean

Portmaster usage in a nutshell:

Command Description
# portmaster -a
Automatically upgrade all outdated ports
# portmaster -af
Automatically upgrade and rebuild all ports
# portmaster shells/bash
Automatically builds and installs/upgrades the port package
# portmaster -dw shells/bash
Automatically builds and installs/upgrades the port package, but with:
  • -d - clean up installation files in /usr/ports/distfiles.
  • -w - save old shared libraries (in /usr/local/lib/compat/pkg/) before uninstalling the existing port, allowing those libraries to potentially be restored.

Before updating any ports, it's a good idea to review any changes by viewing /usr/ports/UPDATING.

To clean up afterwards:

# portmaster --check-depends 
# portmaster --check-port-dbdir 
# portmaster -s 
# portmaster -y --clean-distfiles

System Update

Security updates can be applied to the FreeBSD system by using the freebsd-update utility [1]. This utility can also be used to upgrade a system to a newer major or minor release.

Command Description
$ freebsd-update fetch
Downloads outstanding patches. Files will be stored in /var/db/freebsd-update/
$ freebsd-update cron
Downloads outstanding patches (scheduled as a cronjob)
$ freebsd-update install
Installs any downloaded packages
$ freebsd-update rollback
Rolls back the last set of changes.
$ freebsd-update -r 10.1-RELEASE upgrade
Upgrades the system to the specified release

Depending on the type of updates performed, a restart may be necessary (ie. kernel and kernel modules).

When upgrading between minor releases, the ABI should remain the same and any installed applications should continue working. For upgrades between major releases, the ABI may change and any installed applications may break. To force an upgrade on all installed applications:

# pkg-static upgrade -f
## or ports (pass -G to use generic options)
# portmaster -af

After an upgrade, you may wish to reduce the amount of space used by deleting old update files. Do this only after you are sure you do not need to run freebsd-update rollback.

# find /var/db/freebsd-update/files/ -type f -exec rm -v '{}' +

Build a Custom Kernel

Checkout the kernel source[2][3] to /usr/src via SVN:

# svn checkout https://svn.freebsd.org/base/stable/ /usr/src
 
# ARCH=$(uname -m)
# cd /usr/src/sys/$ARCH/conf/
# cp GENERIC MYCONF

Edit MYCONF if needed, and compile:

# cd /usr/src/
# make -j4  buildkernel   KERNCONF=''MYCONF''
# sudo make installkernel KERNCONF=''MYCONF''

The new kernel should now be installed and should run on the next rebot:

$ sysctl kern.conftxt

Note: compiling the kernel needs quite a bit of disk space. A FreeBSD-10.2 installation took over 2 GB:

1.1 GB  /usr/obj/usr/src/sys/MYCONF
 1.5 GB  /usr/src/


Miscellaneous

Linux Util Equivalents

Description Linux FreeBSD
Listing all disks lsblk geom disk list, geom part list


Linux Binary Emulation

You can run linux binaries under FreeBSD.

Mount linuxprocfs and install linux-f10-procps[4]:

$ grep ^lin /etc/fstab 
linprocfs       /compat/linux/proc linprocfs rw 0       0
linsys          /compat/linux/sys  linsysfs  rw 0       0
 
$ sudo mkdir -p /compat/linux/{proc,sys}
$ sudo mount /compat/linux/proc && sudo mount /compat/linux/sys

$ sudo pkg install linux-f10-procps

Set linux_enable="YES" in /etc/rc.conf.

Screensaver

To have the LCD shut off when the text console is idle[5][6]

$ cat /etc/rc.conf
[...]
apm_enable="YES"
blanktime="60"
saver="blank"

Enable Advanced Power Management (APM) [7] in device.hints [8]

$ grep apm.0.disabled /boot/device.hints 
hint.apm.0.disabled="0"

SMART

To enable SMART, install smartmontools: [9][10]

# pkg install smartmontools
# echo 'smartd_enable="YES"' >> /etc/rc.conf
# cp -i /usr/local/etc/smartd.conf.sample /usr/local/etc/smartd.conf

Start smartd manually:

# /usr/local/etc/rc.d/smartd start

The installation package should have created /usr/local/etc/periodic/daily/smart to check on monitored devices periodically.

Memory

FreeBSD memory notation is quite different [11]. Running top shows various numbers:

$ top -b -d 1 | grep -A1 ^Mem
Mem: 112M Active, 687M Inact, 158M Wired, 8268K Cache, 87M Buf, 12M Free
Swap: 1024M Total, 41M Used, 983M Free, 3% Inuse

These values are:

  • Active: Memory currently being used by a process
  • Inactive: Memory that has been freed but is still cached since it may be used again.
  • Wired: Memory in use by the Kernel. This memory cannot be swapped out
  • Cache: Memory being used to cache data, can be freed immediately if required
  • Buffers: Disk cache
  • Free: Memory that is completely free and ready to use.

Questions

What are the equivalents for:

  • cat /proc/cpuinfo, lscpu


Links

References