FreeBSD: Difference between revisions

From Leo's Notes
This page was last edited on 5 August 2017, at 00:15.
Initial Post
 
updated info and restructed
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{{code|/etc/rc.conf}}.


For a dynamic network configuration (read: {{WP|DHCP|DHCP}}), we'd use something like this in <tt>/etc/rc.conf</tt>:
=== Network ===
Similar to Linux, network adapters and their settings can be displayed using {{code|ifconfig}}.


hostname="len"
The machine hostname can be set by defining the hostname value in {{code|/etc/rc.conf}}. Eg:
ifconfig_bge0="DHCP"
{{highlight|lang=text|code=
ifconfig_bge0_ipv6="inet6 accept_rtadv"
hostname="freebsd"
}}


If we need a ''static'' network configuration, use:
==== DHCP ====
To use DHCP, add the following lines to {{code|/etc/rc.conf}}:
{{highlight|lang=text|code=
ifconfig_bge0="DHCP"
ifconfig_bge0_ipv6="inet6 accept_rtadv"
}}


ifconfig_bge0="inet 10.0.0.3 netmask 255.255.255.0"
Where {{code|bge0}} is the interface name.
defaultrouter=10.0.0.1
hostname=len.example.net


Restart the network configuration:
==== Static ====
For static IP assignment, edit {{code|/etc/rc.conf}}:
/etc/rc.d/netif restart
{{highlight|lang=text|code=
/etc/rc.d/routing restart
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
}}
 
 
== 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.
 
=== 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.
 
Bootstrap the system by running {{code|/usr/sbin/pkg}}, or install it through ports under {{code|/usr/ports/ports-mgmt/pkg}}.
 
If you are familiar with {{code|yum}} or {{code|dnf}}, this table may help:
{|
! 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 {{code|pkg audit -F}} which lists all known vulnerabilities for software that is installed on the system.
 
=== 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.
 
To get started, updates the ports collection on your system:
{{highlight|lang=terminal|code=
## Download the ports collection to /var/db/portsnap
# portsnap fetch
## ... and extract it to /usr/ports
# portsnap extract
}}
 
You may want to update the ports collection to get software updates periodically in the future. To do so, run:
{{highlight|lang=terminal|code=
# 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}}.
 
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}}.
 
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 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:
{|
! Command
! Description
|-
| {{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 the port package
|}
 
=== Checking for Updates ===
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:
 
{{highlight|lang=terminal|code=
# pkg version -1 "<"
}}


= Packages =
<!--


Install missing packages:
Install missing packages:
Line 36: Line 148:
  pkg update
  pkg update
  pkg upgrade
  pkg upgrade
-->


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


$ freebsd-update fetch  
{|
Looking up update.FreeBSD.org mirrors... 5 mirrors found.
! Command
Fetching metadata signature for 9.3-RELEASE from update5.freebsd.org... done.
! Description
Fetching metadata index... done.
|-
Inspecting system...
| {{highlight|lang=terminal|code=
Preparing to download files... done.
$ freebsd-update fetch  
[...]
}}
The following files will be added as part of updating to 9.3-RELEASE-p16:
| Downloads outstanding patches. Files will be stored in {{code|/var/db/freebsd-update/}}
The following files will be updated as part of updating to 9.3-RELEASE-p16:
|-
| {{highlight|lang=terminal|code=
$ freebsd-update install
$ freebsd-update cron
Installing updates....done.
}}
| 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
|}
 
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:
{{highlight|lang=terminal|code=
# 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 {{code|freebsd-update rollback}}.
 
{{highlight|lang=terminal|code=
# find /var/db/freebsd-update/files/ -type f -exec rm -v '{}' +
}}


<!--
= Upgrade =
= Upgrade =


Line 108: Line 255:
   29428
   29428
  $ find /var/db/freebsd-update/files/ -type f -exec rm -v '{}' +
  $ find /var/db/freebsd-update/files/ -type f -exec rm -v '{}' +
-->


= Postinstall =
=== Build a Custom Kernel ===
== Encrypted swap ==


&rarr; [[Hardening/FreeBSD#Encrypted_swap|Hardening/FreeBSD]]
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]]:


== Linux compatibility ==
{{highlight|lang=terminal|code=
# svn checkout https://svn.freebsd.org/base/stable/ /usr/src
# ARCH=$(uname -m)
# cd /usr/src/sys/$ARCH/conf/
# cp GENERIC MYCONF
}}
 
Edit {{code|MYCONF}} if needed, and compile:
 
{{highlight|lang=terminal|code=
# 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:
{{highlight|lang=terminal|code=
$ sysctl kern.conftxt | grep ident
ident  MYCONF
}}
 
Note: compiling the kernel needs quite a bit of disk space. A FreeBSD-10.2 installation took over 2 GB:
{{highlight|lang=terminal|code=
1.1 GB  /usr/obj/usr/src/sys/MYCONF
1.5 GB  /usr/src/
}}
 
 
== Configuration ==
=== Encrypted swap ===
* [[Hardening/FreeBSD#Encrypted_swap|Hardening/FreeBSD]]
 
=== Linux compatibility ===


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>:
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>:
Line 129: Line 309:
Set <tt>linux_enable="YES"</tt> in <tt>/etc/rc.conf</tt>.
Set <tt>linux_enable="YES"</tt> in <tt>/etc/rc.conf</tt>.


== Screensaver ==
=== Screensaver ===


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>
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>
Line 144: Line 324:
  hint.apm.0.disabled="0"
  hint.apm.0.disabled="0"


== SMART ==
=== SMART ===


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>
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>
Line 158: Line 338:
The installation package should have created <tt>/usr/local/etc/periodic/daily/smart</tt> to check on monitored devices periodically.
The installation package should have created <tt>/usr/local/etc/periodic/daily/smart</tt> to check on monitored devices periodically.


== Memory ==
=== Memory ===


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>
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>
Line 173: Line 353:
* Free: Memory that is completely free and ready to use.
* Free: Memory that is completely free and ready to use.


= Build a Kernel =
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]]:
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]]

Revision as of 00:15, 5 August 2017

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/etc/rc.conf.

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


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.

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.

Bootstrap the system by running /usr/sbin/pkg, or install it through ports under /usr/ports/ports-mgmt/pkg.

If you are familiar with yum or dnf, this table may help:

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 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.

To get started, updates the ports collection on your system:

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

You may want to update the ports collection to get software updates periodically in the future. To do so, run:

# portsnap fetch update

To compile and install a port, go to the directory of the port to be installed and then run 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 make config-recursive prior to make install; the other is to use the default options by setting the environment variable BATCH=yes.

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.

There are utilities that make using ports more convenient. portmaster works like a package manager by automating upgrades and installs on the system. Eg:

Command Description
# portmaster -a
Automatically upgrade all outdated ports
# portmaster -af
Automatically upgrade and rebuild all ports
# portmaster shells/bash
Automatically builds and installs the port package

Checking for Updates

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:

# pkg version -1 "<"


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/


Configuration

Encrypted swap

Linux compatibility

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

On this laptop I wanted to have the LCD shut off when the (text) console was idle[5][6]

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

Enable APM[7] in device.hints[8]

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

SMART

To enable Template:WP, use 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 and

/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, let's explain it in short:[11]

$ 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
  • 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.


Links

References