FreeBSD: Difference between revisions

From Leo's Notes
This page was last edited on 1 September 2019, at 01:36.
No edit summary
m Text replacement - "{\| " to "{| class="wikitable" "
 
(3 intermediate revisions by the same user not shown)
Line 5: Line 5:


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


=== Network ===
=== Network ===
Line 38: Line 37:
# /etc/rc.d/routing restart
# /etc/rc.d/routing restart
}}
}}


=== Kernel Modules ===
=== Kernel Modules ===
Kernel modules can be loaded/unloaded or listed like you would on linux.
Kernel modules can be loaded/unloaded or listed like you would on linux.
{|
{| class="wikitable"
! FreeBSD
! FreeBSD
! Linux
! Linux
Line 74: Line 71:


Most modules should have a startup script which gets triggered by an entry in {{code|/etc/rc.conf}}. Alternatively, it can also be loaded by adding a line to {{code|/etc/loader.conf}}.  
Most modules should have a startup script which gets triggered by an entry in {{code|/etc/rc.conf}}. Alternatively, it can also be loaded by adding a line to {{code|/etc/loader.conf}}.  
=== Time ===
Timezones are configured by copying or symlinking the proper tzdata file from {{code|/usr/share/zoneinfo}} to {{code|/etc/localtime}}. A symbolic link might not be preferable if {{code|/usr}} isn't mounted on boot time.
Configure NTP by editing {{code|/etc/rc.conf}}:
{{highlight|lang=text|code=
ntpdate_enable="YES"
ntpdate_flags="-b ntp.cpsc.ucalgary.ca"
}}
Time can be synchronized immediately via NTP by running {{code|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 {{code|/boot/loader.conf}}.
{{highlight|lang=text|code=
autoboot_delay="5"
}}
Single user mode can be entered by booting with {{code|-s}}. Single user mode can be protected by adding {{code|insecure}} to the console in {{code|/etc/ttys}}.


== Package Management ==
== Package Management ==
There are two ways to obtain software on your FreeBSD machine: Through precompiled packages, or using FreeBSD Ports. Both will be covered in the sections below.
There are two ways to obtain software on your FreeBSD machine: Through precompiled packages or using FreeBSD's port collection.


=== Pkg Package Management ===
=== Pkg Package Management ===
{{code|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.
{{code|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.


Bootstrap the system by running {{code|/usr/sbin/pkg}}, or install it through ports under {{code|/usr/ports/ports-mgmt/pkg}}.
The {{code|/usr/sbin/pkg}} should already be installed as part of the system.


If you are familiar with {{code|yum}} or {{code|dnf}}, this table may help:
Refer to the following table for a quick usage guide and its equivalent {{code|yum}} or {{code|dnf}} commands.
{|
{| class="wikitable"
! RedHat / Fedora
! RedHat / Fedora
! FreeBSD
! FreeBSD
Line 119: Line 137:


=== FreeBSD Ports ===
=== FreeBSD Ports ===
The port system automates building and installing packages on your system from source. Rather than trusting pre-compiled 3rd party packages, the ports system allows you to customize and compile a software package along with its dependencies.
The ports collection is a set of makefiles, patches, and description files stored in {{code|/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 {{code|/usr/ports}} using {{code|portsnap}}.


To get started, updates the ports collection on your system:
{{highlight|lang=terminal|code=
{{highlight|lang=terminal|code=
## Download the ports collection to /var/db/portsnap
## Download the ports collection to /var/db/portsnap
Line 129: Line 148:
}}
}}


You may want to update the ports collection to get software updates periodically in the future. To do so, run:
Since the port collection will change periodically with new applications and software updates, an update can be pulled by running:
{{highlight|lang=terminal|code=
{{highlight|lang=terminal|code=
# portsnap fetch update
# portsnap fetch update
}}
}}


To compile and install a port, go to the directory of the port to be installed and then run {{code|make install}}.
==== Using Ports ====
To compile and install a port, go to {{code|/usr/ports}} and navigate to a port you wish to install and run {{code|make install}}.


Most packages have configurations that can be customized. For larger packages with many dependencies, this may become unwieldy. There are two ways around this: One is to ask for all the customization options up front by running {{code|make config-recursive}} prior to {{code|make install}}; the other is to use the default options by setting the environment variable {{code|1=BATCH=yes}}.
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 {{code|make config-recursive}} prior to {{code|make install}}. To accept the default configurations set by the package, define an environment variable {{code|1=BATCH=yes}} when running {{code|make install}}.


After building a port, it is a good idea to clean the port's working directory to reduce wasted space by running {{code|make clean}}.
After building a port, it is a good idea to clean the port's working directory to reduce wasted space by running {{code|make clean}}.
Line 142: Line 162:
To remove an installed port, use either {{code|pkg delete}} or run {{code|make deinstall}} in the port's directory.
To remove an installed port, use either {{code|pkg delete}} or run {{code|make deinstall}} in the port's directory.


There are utilities that make using ports more convenient. {{code|portmaster}} works like a package manager by automating upgrades and installs on the system. Eg:
==== Portmaster ====
{|
{{code|portmaster}} is a shell script used for installing and updating installed ports on the system. It is capable of reading package information from {{code|/var/db/pkg}} to determine which packages need to be updated.
 
{{code|portmaster}} can be installed via ports:
{{highlight|lang=terminal|code=
# portsnap fetch update
# cd /usr/ports/ports-mgmt/portmaster
# make install clean
}}
 
Portmaster usage in a nutshell:
{| class="wikitable"
! Command
! Command
! Description
! Description
Line 160: Line 190:
# portmaster shells/bash
# portmaster shells/bash
}}
}}
| Automatically builds and installs the port package
| Automatically builds and installs/upgrades the port package
|-
| {{highlight|lang=terminal|code=
# portmaster -dw shells/bash
}}
| Automatically builds and installs/upgrades the port package, but with:
* {{code|-d}} - clean up installation files in {{code|/usr/ports/distfiles}}.
* {{code|-w}} - save old shared libraries (in {{code|/usr/local/lib/compat/pkg/}}) before uninstalling the existing port, allowing those libraries to potentially be restored.
|}
|}


=== Checking for Updates ===
Before updating any ports, it's a good idea to review any changes by viewing {{code|/usr/ports/UPDATING}}.
Regardless of which method you used to install packages, you can check for newer versions of packages or ports by updating ports or the package cache and then running:
 
To clean up afterwards:


{{highlight|lang=terminal|code=
{{highlight|lang=terminal|code=
# pkg version -1 "<"
# portmaster --check-depends
# portmaster --check-port-dbdir
# portmaster -s
# portmaster -y --clean-distfiles
}}
}}


Line 173: Line 214:
Security updates can be applied to the FreeBSD system by using the {{code|freebsd-update}} utility <ref>[https://www.freebsd.org/doc/handbook/updating-upgrading-freebsdupdate.html#freebsdupdate-security-patches Applying Security Patches]</ref>. This utility can also be used to upgrade a system to a newer major or minor release.
Security updates can be applied to the FreeBSD system by using the {{code|freebsd-update}} utility <ref>[https://www.freebsd.org/doc/handbook/updating-upgrading-freebsdupdate.html#freebsdupdate-security-patches Applying Security Patches]</ref>. This utility can also be used to upgrade a system to a newer major or minor release.


{|
{| class="wikitable"
! Command
! Command
! Description
! Description
Line 254: Line 295:


=== Linux Util Equivalents ===
=== Linux Util Equivalents ===
{|
{| class="wikitable"
! Description
! Description
! Linux
! Linux

Latest revision as of 01:36, 1 September 2019

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