FreeBSD: Difference between revisions

From Leo's Notes
This page was last edited on 1 September 2019, at 01:36.
Initial Post
 
m Text replacement - "{\| " to "{| class="wikitable" "
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
On a freshly installed FreeBSD 10.1 system, we have to configure a few things first.
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.


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


For a dynamic network configuration (read: {{WP|DHCP|DHCP}}), we'd use something like this in <tt>/etc/rc.conf</tt>:
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}}.


hostname="len"
=== Network ===
ifconfig_bge0="DHCP"
Similar to Linux, network adapters and their settings can be displayed using {{code|ifconfig}}.
ifconfig_bge0_ipv6="inet6 accept_rtadv"


If we need a ''static'' network configuration, use:
The machine hostname can be set by defining the hostname value in {{code|/etc/rc.conf}}. Eg:
{{highlight|lang=text|code=
hostname="freebsd"
}}


ifconfig_bge0="inet 10.0.0.3 netmask 255.255.255.0"
==== DHCP ====
defaultrouter=10.0.0.1
To use DHCP, add the following lines to {{code|/etc/rc.conf}}:
hostname=len.example.net
{{highlight|lang=text|code=
ifconfig_bge0="DHCP"
ifconfig_bge0_ipv6="inet6 accept_rtadv"
}}


Restart the network configuration:
Where {{code|bge0}} is the interface name.
 
/etc/rc.d/netif restart
==== Static ====
/etc/rc.d/routing restart
For static IP assignment, edit {{code|/etc/rc.conf}}:
{{highlight|lang=text|code=
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:
 
{{highlight|lang=terminal|code=
# /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.
{| class="wikitable"
! FreeBSD
! Linux
! Description
|-
| {{highlight|lang=terminal|code=
# kldload
}}
| {{highlight|lang=terminal|code=
# modprobe
}}
| Loads a kernel module
|-
| {{highlight|lang=terminal|code=
# kldunload
}}
| {{highlight|lang=terminal|code=
# rmmod
}}
| Removes a module
|-
| {{highlight|lang=terminal|code=
# kldstat
}}
| {{highlight|lang=terminal|code=
# lsmod
}}
| Lists loaded modules
|}
 
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 ==
There are two ways to obtain software on your FreeBSD machine: Through precompiled packages or using FreeBSD's port collection.
 
=== 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.


= Packages =
The {{code|/usr/sbin/pkg}} should already be installed as part of the system.


Install missing packages:
Refer to the following table for a quick usage guide and its equivalent {{code|yum}} or {{code|dnf}} commands.
{| class="wikitable"
! 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
|}


pkg update
A neat feature for the security conscious is {{code|pkg audit -F}} which lists all known vulnerabilities for software that is installed on the system.
pkg install bash bash-completion curl gnupg1 lsof pv rsync screen sudo vnstat


* Change a user's shell via <tt>chsh</tt><ref>[https://www.freebsd.org/cgi/man.cgi?query=chpass chpass(1)]</ref>
=== FreeBSD Ports ===
* Add <tt>vnstat_enable="YES"</tt> in <tt>/etc/rc.conf</tt>.
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.


= Update =
The ports collection can be obtained by downloading and extracting it to {{code|/usr/ports}} using {{code|portsnap}}.


Update package repository catalogues, then update the ''installed packages'':
{{highlight|lang=terminal|code=
## Download the ports collection to /var/db/portsnap
# portsnap fetch
## ... and extract it to /usr/ports
# portsnap extract
}}


pkg update
Since the port collection will change periodically with new applications and software updates, an update can be pulled by running:
pkg upgrade
{{highlight|lang=terminal|code=
# portsnap fetch update
}}


To update<ref>[https://www.freebsd.org/doc/handbook/updating-upgrading-freebsdupdate.html#freebsdupdate-security-patches Applying Security Patches]</ref> the base system:
==== 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}}.


$ freebsd-update fetch
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}}.
Looking up update.FreeBSD.org mirrors... 5 mirrors found.
Fetching metadata signature for 9.3-RELEASE from update5.freebsd.org... done.
Fetching metadata index... done.
Inspecting system...
Preparing to download files... done.
[...]
The following files will be added as part of updating to 9.3-RELEASE-p16:
The following files will be updated as part of updating to 9.3-RELEASE-p16:
$ freebsd-update install
Installing updates....done.


= Upgrade =
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}}.


To ''upgrade''<ref>[https://www.freebsd.org/doc/handbook/updating-upgrading-freebsdupdate.html#freebsdupdate-upgrade Performing Major and Minor Version Upgrades]</ref> to another [http://ftp.freebsd.org/pub/FreeBSD/releases/amd64/ release], use:
To remove an installed port, use either {{code|pkg delete}} or run {{code|make deinstall}} in the port's directory.


$ uname -r
==== Portmaster ====
{{color|blue|100|9.3-STABLE}}
{{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.
$ freebsd-update -r {{color|red|100|10.1-RELEASE}} upgrade
Looking up update.FreeBSD.org mirrors... 5 mirrors found.
Fetching metadata signature for 9.3-RELEASE from update2.freebsd.org... done.
Fetching metadata index... done.
Inspecting system... done.
[...]
Fetching metadata signature for 10.1-RELEASE from update2.freebsd.org... done.
Fetching metadata index... done.
Fetching 1 metadata patches. done.
Applying metadata patches... done.
Fetching 1 metadata files... done.
Inspecting system...
Fetching files from 9.3-RELEASE for merging... done.
Preparing to download files...
To install the downloaded upgrades, run "/usr/sbin/freebsd-update install".
$ freebsd-update install
Installing updates...
Kernel updates have been installed.  Please reboot and run
"/usr/sbin/freebsd-update install" again to finish installing updates.


Note: since the kernel has already been updated, we may have to boot the ''old'' (9.3-RELEASE) kernel and continue with:
{{code|portmaster}} can be installed via ports:
{{highlight|lang=terminal|code=
# portsnap fetch update
# cd /usr/ports/ports-mgmt/portmaster
# make install clean
}}


$ freebsd-update install
Portmaster usage in a nutshell:
Installing updates...
{| class="wikitable"
Completing this upgrade requires removing old shared object files.
! Command
Please rebuild all installed 3rd party software (e.g., programs
! Description
installed from the ports tree) and then run "/usr/sbin/freebsd-update install"
|-
again to finish installing updates.
| {{highlight|lang=terminal|code=
# portmaster -a
}}
| Automatically upgrade all outdated ports
|-
| {{highlight|lang=terminal|code=
# portmaster -af
}}
| Automatically upgrade and rebuild all ports
|-
| {{highlight|lang=terminal|code=
# portmaster shells/bash
}}
| 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.
|}


[[#Update|Update]] userland tools:
Before updating any ports, it's a good idea to review any changes by viewing {{code|/usr/ports/UPDATING}}.


pkg update
To clean up afterwards:
pkg upgrade


Continue with:
{{highlight|lang=terminal|code=
# portmaster --check-depends
# portmaster --check-port-dbdir
# portmaster -s
# portmaster -y --clean-distfiles
}}


$ freebsd-update install
== System Update ==
Installing updates... done.
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.


Now the ''updated'' kernel should be bootable and the upgrade should now be completed.
{| class="wikitable"
! Command
! Description
|-
| {{highlight|lang=terminal|code=
$ freebsd-update fetch
}}
| Downloads outstanding patches. Files will be stored in {{code|/var/db/freebsd-update/}}
|-
| {{highlight|lang=terminal|code=
$ freebsd-update cron
}}
| Downloads outstanding patches (scheduled as a cronjob)
|-
| {{highlight|lang=terminal|code=
$ freebsd-update install
}}
| Installs any downloaded packages
|-
| {{highlight|lang=terminal|code=
$ freebsd-update rollback
}}
| Rolls back the last set of changes.
|-
| {{highlight|lang=terminal|code=
$ freebsd-update -r 10.1-RELEASE upgrade
}}
| Upgrades the system to the specified release
|}


If all is well, and '''no''' <tt>freebsd-update rollback</tt> is required, <tt>/var/db/freebsd-update/files</tt> can be emptied:<ref>[https://forums.freebsd.org/threads/can-var-db-freebsd-update-be-safely-deleted.25875/#post-145267 can /var/db/freebsd-update/ be safely deleted?]</ref>
Depending on the type of updates performed, a restart may be necessary (ie. kernel and kernel modules).


$ du -sh /var/db/freebsd-update/files; find /var/db/freebsd-update/files | wc -l
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:
781M    /var/db/freebsd-update/files
{{highlight|lang=terminal|code=
  29428
# pkg-static upgrade -f
$ find /var/db/freebsd-update/files/ -type f -exec rm -v '{}' +
## or ports (pass -G to use generic options)
# portmaster -af
}}


= Postinstall =
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 {{code|freebsd-update rollback}}.
== Encrypted swap ==


&rarr; [[Hardening/FreeBSD#Encrypted_swap|Hardening/FreeBSD]]
{{highlight|lang=terminal|code=
# find /var/db/freebsd-update/files/ -type f -exec rm -v '{}' +
}}


== Linux compatibility ==
=== Build a Custom Kernel ===


Mount <tt>linuxprocfs</tt> and install <tt>linux-f10-procps</tt><ref>[https://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/linuxemu.html Linux® Binary Compatibility]</ref>:
Checkout the kernel source<ref>[https://www.freebsd.org/doc/handbook/svn.html#svn-usage Appendix A. Obtaining FreeBSD: Running Subversion]</ref><ref>[https://www.digitalocean.com/community/tutorials/how-to-customize-and-recompile-your-kernel-on-freebsd-10-1 How To Customize and Recompile Your Kernel on FreeBSD 10.1]</ref> to <tt>/usr/src</tt> via [[SCM#Subversion|SVN]]:


$ grep ^lin /etc/fstab
{{highlight|lang=terminal|code=
linprocfs      /compat/linux/proc linprocfs rw 0      0
# svn checkout https://svn.freebsd.org/base/stable/ /usr/src
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
# ARCH=$(uname -m)
# cd /usr/src/sys/$ARCH/conf/
# cp GENERIC MYCONF
}}


Set <tt>linux_enable="YES"</tt> in <tt>/etc/rc.conf</tt>.
Edit {{code|MYCONF}} if needed, and compile:


== Screensaver ==
{{highlight|lang=terminal|code=
# cd /usr/src/
# make -j4  buildkernel  KERNCONF=''MYCONF''
# sudo make installkernel KERNCONF=''MYCONF''
}}


On this laptop I wanted to have the LCD shut off when the (text) console was idle<ref>[https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=114928 green_saver does not switch DVI monitor power off]</ref><ref>[http://lists.freebsd.org/pipermail/freebsd-current/2010-November/021589.html Should green_saver.ko shut off a laptop's backlight?]</ref>
The new kernel should now be installed and should run on the next rebot:
{{highlight|lang=terminal|code=
$ sysctl kern.conftxt | grep ident
ident  MYCONF
}}


  $ cat /etc/rc.conf
Note: compiling the kernel needs quite a bit of disk space. A FreeBSD-10.2 installation took over 2 GB:
[...]
{{highlight|lang=terminal|code=
apm_enable="YES"
1.1 GB /usr/obj/usr/src/sys/MYCONF
  blanktime="60"
1.5 GB  /usr/src/
  saver="blank"
}}
 
 
== Miscellaneous ==
 
=== Linux Util Equivalents ===
{| class="wikitable"
! Description
! Linux
! FreeBSD
|-
| Listing all disks
| lsblk
| geom disk list, geom part list
|}
 
 
=== Linux Binary Emulation ===
You can run linux binaries under FreeBSD.
 
Mount {{code|linuxprocfs}} and install {{code|linux-f10-procps}}<ref>[https://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/linuxemu.html Linux® Binary Compatibility]</ref>:
 
{{highlight|lang=terminal|code=
$ 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
}}


Enable APM<ref>[https://www.freebsd.org/doc/en/articles/laptop/article.html#power-management FreeBSD on Laptops: APM]</ref> in <tt>device.hints</tt><ref>[https://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/device-hints.html 13.4. Device Hints]</ref>
Set {{code|1=linux_enable="YES"}} in {{code|/etc/rc.conf}}.


$ grep apm.0.disabled /boot/device.hints
=== Screensaver ===
hint.apm.0.disabled="0"
To have the LCD shut off when the text console is idle<ref>[https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=114928 green_saver does not switch DVI monitor power off]</ref><ref>[http://lists.freebsd.org/pipermail/freebsd-current/2010-November/021589.html Should green_saver.ko shut off a laptop's backlight?]</ref>
{{highlight|lang=terminal|code=
$ cat /etc/rc.conf
[...]
apm_enable="YES"
blanktime="60"
saver="blank"
}}


== SMART ==
Enable Advanced Power Management (APM) <ref>[https://www.freebsd.org/doc/en/articles/laptop/article.html#power-management FreeBSD on Laptops: APM]</ref> in {{code|device.hints}} <ref>[https://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/device-hints.html 13.4. Device Hints]</ref>


To enable {{WP|S.M.A.R.T.|S.M.A.R.T.}}, use <tt>smartmontools</tt>:<ref>[http://www.smartmontools.org/ smartmontools]</ref><ref>[http://www.freebsddiary.org/smart.php Monitoring your HDD using SMART and Nagios]</ref>
{{highlight|lang=terminal|code=
$ grep apm.0.disabled /boot/device.hints
hint.apm.0.disabled="0"
}}


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


Start <tt>smartd</tt> and  
To enable SMART, install {{code|smartmontools}}: <ref>[http://www.smartmontools.org/ smartmontools]</ref><ref>[http://www.freebsddiary.org/smart.php Monitoring your HDD using SMART and Nagios]</ref>


/usr/local/etc/rc.d/smartd start
{{highlight|lang=terminal|code=
# pkg install smartmontools
# echo 'smartd_enable="YES"' >> /etc/rc.conf
# cp -i /usr/local/etc/smartd.conf.sample /usr/local/etc/smartd.conf
}}


The installation package should have created <tt>/usr/local/etc/periodic/daily/smart</tt> to check on monitored devices periodically.
Start {{code|smartd}} manually:
{{highlight|lang=terminal|code=
# /usr/local/etc/rc.d/smartd start
}}


== Memory ==
The installation package should have created {{code|/usr/local/etc/periodic/daily/smart}} to check on monitored devices periodically.


FreeBSD memory notation is quite different, let's explain it in short:<ref>[http://unix.stackexchange.com/questions/134862/what-do-the-different-memory-counters-in-freebsd-mean What do the different memory counters in FreeBSD mean?]</ref>
=== Memory ===


$ top -b -d 1 | grep -A1 ^Mem
FreeBSD memory notation is quite different <ref>[http://unix.stackexchange.com/questions/134862/what-do-the-different-memory-counters-in-freebsd-mean What do the different memory counters in FreeBSD mean?]</ref>. Running top shows various numbers:
Mem: 112M Active, 687M Inact, 158M Wired, 8268K Cache, 87M Buf, 12M Free
{{highlight|lang=terminal|code=
Swap: 1024M Total, 41M Used, 983M Free, 3% Inuse
$ 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
* Active: Memory currently being used by a process
* Inactive: Memory that has been freed but is still cached since it may be used again.
* Inactive: Memory that has been freed but is still cached since it may be used again.
Line 173: Line 375:
* Free: Memory that is completely free and ready to use.
* Free: Memory that is completely free and ready to use.


= Build a Kernel =
== Questions ==
 
What are the equivalents for:
Checkout the kernel source<ref>[https://www.freebsd.org/doc/handbook/svn.html#svn-usage Appendix A. Obtaining FreeBSD: Running Subversion]</ref><ref>[https://www.digitalocean.com/community/tutorials/how-to-customize-and-recompile-your-kernel-on-freebsd-10-1 How To Customize and Recompile Your Kernel on FreeBSD 10.1]</ref> to <tt>/usr/src</tt> via [[SCM#Subversion|SVN]]:
* cat /proc/cpuinfo, lscpu
 
svn checkout <nowiki>https://svn.freebsd.org/</nowiki>{{color|blue|100|base}}/{{color|red|100|stable/10}} /usr/src
ARCH=$(uname -m)
cd /usr/src/sys/$ARCH/conf/
cp GENERIC MYCONF
 
Edit <tt>MYCONF</tt>, if needed and we're ready to compile:
 
cd /usr/src/
make -j4  buildkernel  KERNCONF=''MYCONF''
sudo make installkernel KERNCONF=''MYCONF''


Reboot - and if all goes well, our new kernel should be running:


$ sysctl kern.conftxt | grep ident
ident  MYCONF
Note: compiling the kernel needs quite a bit of disk space, in our FreeBSD-10.2 installation:
1.1 GB  /usr/obj/usr/src/sys/MYCONF
1.5 GB  /usr/src/


= Links =
== Links ==


* [[/Bugs/]]
* [http://freebsdwiki.net/ FreeBSDwiki]
* [http://freebsdwiki.net/ FreeBSDwiki]


= References =
== References ==
<references/>
<references/>


[[Category:OS]]
[[Category:OS]]

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