From 17eba055301ef659bce8835c83ea8cb315cf0b68 Mon Sep 17 00:00:00 2001 From: Michele Cereda Date: Sat, 13 May 2023 16:44:07 +0200 Subject: [PATCH] feat: freebsd examples and improved knowledge --- .vscode/settings.json | 3 + examples/freebsd.initial-config.sh | 36 ++++ .../freebsd.virtualbox.guest-additions.sh | 24 +++ knowledge base/freebsd.md | 190 +++++++++++++++++- 4 files changed, 243 insertions(+), 10 deletions(-) create mode 100644 examples/freebsd.initial-config.sh create mode 100644 examples/freebsd.virtualbox.guest-additions.sh diff --git a/.vscode/settings.json b/.vscode/settings.json index 0f5a680..0ad21a7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -16,6 +16,7 @@ "cfengine", "chattr", "chezmoi", + "chpass", "chsh", "clamav", "clamd", @@ -76,6 +77,7 @@ "pinentry", "pkexec", "polkit", + "portsnap", "pvresize", "radeon", "replicatedctl", @@ -91,6 +93,7 @@ "subvolume", "swapfile", "swapon", + "sysrc", "tclsh", "templating", "tfvars", diff --git a/examples/freebsd.initial-config.sh b/examples/freebsd.initial-config.sh new file mode 100644 index 0000000..9a37657 --- /dev/null +++ b/examples/freebsd.initial-config.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env sh + +if [ "$(id -ru)" -eq 0 ] +then + echo "Run this again as 'root'" + exit 1 +fi + +# Package management + +pkg bootstrap +pkg update +pkg install -y \ + 'vim' \ + 'zsh' 'zsh-autosuggestions' 'zsh-completions' 'zsh-navigation-tools' 'zsh-syntax-highlighting' + +# Non-'root' user management + +pw groupmod 'wheel' -m 'username' +cat > '/home/username/.zshrc' <<-EOF + source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh + source /usr/local/share/zsh-autosuggestions/zsh-autosuggestions.zsh + source /usr/local/share/zsh-navigation-tools/zsh-navigation-tools.plugin.zsh + + HISTFILE=~/.histfile + HISTSIZE=100000 + SAVEHIST=100000 + bindkey -e + + zstyle :compinstall filename ~/.zshrc + autoload -Uz compinit + compinit +EOF +chown 'username':'usergroup' '/home/username/.zshrc' +chmod 'u=rw,go=r' '/home/username/.zshrc' +chpass -s "$(grep 'bin/zsh' '/etc/shells')" 'username' diff --git a/examples/freebsd.virtualbox.guest-additions.sh b/examples/freebsd.virtualbox.guest-additions.sh new file mode 100644 index 0000000..5d855fb --- /dev/null +++ b/examples/freebsd.virtualbox.guest-additions.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env sh + +if [ "$(id -ru)" -eq 0 ] +then + echo "Run this again as 'root'" + exit 1 +fi + +# Package management +# Use 'virtualbox-ose-additions-nox11' for console-only systems. + +pkg bootstrap +pkg update +pkg install -y 'virtualbox-ose-additions' + +# Start VirtualBox services at boot + +sysrc vboxguest_enable="YES" +sysrc vboxservice_enable="YES" + +# NTP workaround +# Needed if NTP or NTPDate are used + +sysrc vboxservice_flags="--disable-timesync" diff --git a/knowledge base/freebsd.md b/knowledge base/freebsd.md index 1d62fa4..73f05ce 100644 --- a/knowledge base/freebsd.md +++ b/knowledge base/freebsd.md @@ -1,34 +1,204 @@ # FreeBSD +## Table of contents + +1. [TL;DR](#tldr) +1. [Utilities worth noting](#utilities-worth-noting) +1. [The `rc.conf` files](#the-rcconf-files) +1. [Package management](#package-management) + 1. [Manage binary packages with `pkg`](#manage-binary-packages-with-pkg) + 1. [Manage ports from the Ports collection](#manage-ports-from-the-ports-collection) +1. [Enable time sync for the NTP server](#enable-time-sync-for-the-ntp-server) +1. [VirtualBox Guest Additions](#virtualbox-guest-additions) +1. [Further readings](#further-readings) +1. [Sources](#sources) + ## TL;DR ```sh -# Initialize package managers. -portsnap auto +# Read manual pages. +man 5 'rc.conf' + +# Search for keywords in the manual page descriptions. +man -k 'mail' + +# Edit files. +edit 'path/to/file' + +# Become 'root' from user sessions. +# The user must know root's password *and* be member of the 'wheel' group. +# Use '-' at the end to also load root's environment. +su +su - + +# Add new members to groups. +pw groupmod 'group_name' -m 'username' +pw groupmod 'group_name' -m 'username_1','username_N' + +# Replace all members in groups. +pw groupmod 'group_name' -M 'username' +pw groupmod 'group_name' -M 'username_1','username_N' + +# Change users' default shell. +chpass -s 'path/to/shell' 'username' +chpass -s "$(grep 'bin/zsh' '/etc/shells')" 'username' + +# Start services at boot. +sysrc ntpd_enable="YES" +sysrc vboxguest_enable="YES" + +# Get the current system's version. +freebsd-version + +# Upgrade the system. +# Maintains the current version. +freebsd-update fetch && \ +freebsd-update install + +# Upgrade the system to a newer version. +freebsd-update upgrade -r '13.2-RELEASE' && \ +freebsd-update install + +# Initialize the package managers. pkg bootstrap +portsnap auto + +# Update the package cache. +pkg update + +# Search for packages. +pkg search 'bash' + +# Install packages. +pkg install 'vim' +pkg install -y 'zsh' 'zsh-autosuggestions' + +# Upgrade packages. +pkg upgrade +pkg install -y 'zsh' 'zsh-autosuggestions' + +# Check for known vulnerabilities in *installed* applications. +pkg audit -F +pkg audit -Fr 'sqlite' ``` -## Utilities worth noticing +## Utilities worth noting - `bsdinstall` - `bsdconfig` -## NTP time sync +## The `rc.conf` files -```conf -# file /etc/rc.conf -ntpd_enable="YES" -ntpd_sync_on_start="YES" +The `rc.conf` files contain information about the local host name, configuration details for any network interfaces and which services should be started up at system boot.
+Options are set with `name=value` assignments using the `sh(1)` syntax, and the files are included by the various generic startup scripts in `/etc` which than make decision about their internal actions according to their contents. + +The `sysrc(8)` command provides a scripting interface to programmatically modify system configuration files. + +The `/etc/defaults/rc.conf` file specifies the **default** settings for all the available options. At its very end, it sources, in order: + +- the `/etc/rc.conf` file, to allow system administrators to override such default values for the local system, and +- the `/etc/defaults/vendor.conf` file, to allow vendors to override system defaults. + +In the very same way, the `/etc/rc.conf.local` file is used to override settings in `/etc/rc.conf` for historical reasons. + +In addition to `/etc/rc.conf.local`, one can also place smaller configuration files for each `rc(8)` script in the `/etc/rc.conf.d` or `⟨dir⟩/rc.conf.d` directories specified in `local_startup`, all of which will then be included by the `load_rc_config` function. + +For jail configurations, one could use the `/etc/rc.conf.d/jail` file to store configuration options specific to jails only.
+If `local_startup` contains `/usr/local/etc/rc.d` and `/opt/conf`, `/usr/local/rc.conf.d/jail` and `/opt/conf/rc.conf.d/jail` will be loaded too. If `⟨dir⟩/rc.conf.d/⟨name⟩` is a directory, all the files in it will be loaded too. + +See the contents of `man 5 rc.conf` for more information. + +## Package management + +Requires: + +- [`pkg`][manage binary packages with pkg] if one wants to deal with binary packages; +- the [Ports collection][manage ports from the ports collection] if one wants to compile and install source code in an automated way. + +See [Installing applications] for more information. + +### Manage binary packages with `pkg` + +```sh +# Bootstrap `pkg`. +# Need to be run as 'root'. +pkg bootstrap + +# Update the package cache. +pkg update + +# Get help on the command. +pkg help +pkg help 'search' + +# Search for packages. +pkg search 'bash' + +# Install packages. +pkg install 'vim' +pkg install -y 'zsh' 'zsh-autosuggestions' ``` +### Manage ports from the Ports collection + +TODO + +## Enable time sync for the NTP server + +```sh +sysrc ntpd_enable="YES" +sysrc ntpd_sync_on_start="YES" +``` + +## VirtualBox Guest Additions + +1. Install the additions.
+ Use the `-nox11` package for console-only guests. + + ```sh + pkg update + pkg install -y 'virtualbox-ose-additions' + ``` + +1. Enable the services at boot: + + ```sh + sysrc vboxguest_enable="YES" + sysrc vboxservice_enable="YES" + ``` + +1. If `ntp` or `ntpdate` are used, disable the additions' time sync: + + ```sh + sysrc vboxservice_flags="--disable-timesync" + ``` + +## Further readings + +- The [FreeBSD Handbook] +- [`rc.conf`'s man page][rc.conf man page] +- [Installing applications] +- [Using the Ports collection] + ## Sources -- [Ports] +All the references in the [further readings] section, plus the following: + - [NTPdate - not updating to current time] - [Boinc] - [sbz's FreeBSD commands cheat-sheet] + +[freebsd handbook]: https://docs.freebsd.org/en/books/handbook/ +[Installing applications]: https://docs.freebsd.org/en/books/handbook/ports/ +[rc.conf man page]: https://man.freebsd.org/cgi/man.cgi?rc.conf(5) +[using the ports collection]: https://docs.freebsd.org/en/books/handbook/ports/#ports-using + + +[manage binary packages with pkg]: #manage-binary-packages-with-pkg +[manage ports from the ports collection]: #manage-ports-from-the-ports-collection + + [boinc]: https://people.freebsd.org/~pav/boinc.html [ntpdate - not updating to current time]: https://forums.freebsd.org/threads/ntpdate-not-updating-to-current-time.72847/ -[ports]: https://docs.freebsd.org/en/books/handbook/ports/ [sbz's freebsd commands cheat-sheet]: https://github.com/sbz/freebsd-commands