Przejdź do treści

🦉 FreeBSD BSD Userland Differences

FreeBSD's userland differs significantly from Linux GNU userland in command syntax, available tools, and system administration approaches. Understanding these differences is crucial for effective system management and cross-platform scripting.


🎯 Core Philosophy Differences

Design Philosophy

FreeBSD Userland: - Integrated system design where tools are developed alongside the kernel - Conservative approach to feature additions - Emphasis on stability and correctness over rapid feature development - Strong focus on documentation and man pages - BSD licensing model

Linux/GNU Userland: - Modular approach with separate tool development - Rapid feature development and frequent updates - GNU Coding Standards compliance - Extensive command-line options and flexibility - GPL licensing model


🔧 Command Syntax Differences

ls - List Directory Contents

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# FreeBSD ls
ls -lahG  # -G for color, no --color support

# Linux ls
ls -lah --color=auto

# Portable approach
if ls --version >/dev/null 2>&1; then
    # GNU version
    LS_COLOR="--color=auto"
else
    # BSD version
    LS_COLOR="-G"
fi

ls -lah $LS_COLOR

grep - Pattern Searching

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# FreeBSD grep
grep -E "pattern" file      # Extended regex
grep -i "pattern" file      # Case insensitive

# Linux grep
grep -E "pattern" file      # Extended regex
grep -P "pattern" file      # Perl-compatible regex (GNU only)

# FreeBSD lacks some GNU extensions
# No --exclude-dir, --include, --color options

# Portable approach
if grep --help 2>&1 | grep -q "perl-regexp"; then
    # GNU grep
    GREP_EXTENDED="-P"
else
    # BSD grep
    GREP_EXTENDED="-E"
fi
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# FreeBSD find
find . -name "*.txt" -mtime -7

# FreeBSD lacks GNU-specific features:
# No -regextype, --daystart, -printf options

# FreeBSD specific features:
find . -name "*.txt" -newerct "2023-01-01"  # Creation time comparison

# Portable approach for common operations
find_files_by_extension() {
    local ext="$1"
    local days="${2:-7}"

    find . -name "*.${ext}" -mtime -${days} -type f
}

📋 System Administration Tools

Package Management

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# FreeBSD package management
pkg info                    # List installed packages
pkg search package-name     # Search for packages
pkg install package-name    # Install package
pkg delete package-name     # Remove package
pkg upgrade                 # Upgrade all packages
pkg audit                   # Security audit

# FreeBSD ports system
cd /usr/ports/category/portname
make install clean          # Compile and install from ports

# Linux equivalents (varies by distribution)
# Debian/Ubuntu: apt-get, aptitude
# Red Hat/CentOS: yum, dnf
# Arch Linux: pacman

Service Management

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# FreeBSD service management (rc.d system)
service sshd start          # Start SSH service
service sshd stop           # Stop SSH service
service sshd restart        # Restart SSH service
service sshd status         # Check service status

# Enable/disable services
sysrc sshd_enable=YES       # Enable SSH at boot
sysrc sshd_enable=NO        # Disable SSH at boot

# List all services
service -l

# Linux systemd equivalents
# systemctl start sshd
# systemctl stop sshd
# systemctl restart sshd
# systemctl status sshd
# systemctl enable sshd

System Information

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# FreeBSD system information
uname -a                    # System information
freebsd-version             # FreeBSD version
sysctl hw.physmem           # Physical memory
sysctl kern.hostname        # Hostname
dmesg                       # Boot messages
pciconf -lv                 # PCI devices
usbconfig                   # USB devices

# Linux equivalents
# uname -a
# cat /etc/os-release
# free
# hostname
# dmesg
# lspci
# lsusb

🛠️ Network Tools

Network Configuration

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# FreeBSD network configuration
ifconfig                    # Show all interfaces
ifconfig em0                # Show specific interface
ifconfig em0 inet 192.168.1.100 netmask 255.255.255.0  # Configure interface

# FreeBSD-specific tools
netstat -rn                 # Routing table
sockstat                    # Socket statistics
arp -a                      # ARP table

# Linux equivalents
# ip addr show
# ip link set eth0 up
# route -n
# ss -tuln
# arp -a

Firewall

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# FreeBSD PF (Packet Filter)
# Configuration: /etc/pf.conf
pfctl -f /etc/pf.conf       # Load rules
pfctl -sr                   # Show rules
pfctl -sn                   # Show NAT rules
pfctl -si                   # Show statistics

# Linux iptables
# iptables -L
# iptables-save
# iptables-restore

🗃️ File System Tools

Disk Management

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# FreeBSD disk management
gpart show                  # Show disk partitions
gpart create -s GPT ada0    # Create GPT partition table
gpart add -t freebsd-ufs ada0  # Add partition

# File system creation
newfs /dev/ada0p1           # Create UFS filesystem
mount /dev/ada0p1 /mnt      # Mount filesystem

# Linux equivalents
# fdisk /dev/sda
# parted /dev/sda
# mkfs.ext4 /dev/sda1
# mount /dev/sda1 /mnt

ZFS Support

1
2
3
4
5
6
7
8
9
# FreeBSD has native ZFS support
zpool status                # Pool status
zpool list                  # List pools
zfs list                    # List datasets
zfs create tank/dataset     # Create dataset
zfs snapshot tank/dataset@snap  # Create snapshot

# Linux ZFS (requires OpenZFS)
# Same commands generally work

🎨 Advanced FreeBSD Features

Jails

1
2
3
4
5
6
7
8
9
# FreeBSD jail management
# Configuration in /etc/jail.conf
service jail start          # Start all jails
service jail start jailname # Start specific jail
jls                        # List jails
jexec jailname command     # Execute in jail

# Linux containers
# docker, lxc, systemd-nspawn

GEOM Framework

1
2
3
4
5
6
7
8
9
# FreeBSD GEOM (Geometry framework)
geom disk list             # List disks
geom partition list        # List partitions
geom mirror status         # RAID status
geom stripe status         # Stripe status

# Linux equivalents
# mdadm for RAID
# dmsetup for device mapper

🧪 Development Tools

Compiler and Build Tools

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# FreeBSD development tools
cc                         # Default C compiler (clang)
gcc                        # GCC compiler (if installed)
make                       # BSD make
gmake                      # GNU make (if installed)

# FreeBSD ports build system
cd /usr/ports/devel/gcc
make config                # Configure build options
make install clean         # Build and install

# Linux equivalents
# gcc
# make
# Various package managers

Debugging Tools

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# FreeBSD debugging tools
truss                      # System call tracer (like strace)
ktrace/kdump               # Kernel trace
gdb                        # GNU debugger
procstat                   # Process information
fstat                      # File status

# Linux equivalents
# strace
# gdb
# ps, top
# lsof

🧾 Summary of Key Differences

Command Differences Table

FreeBSD Linux Description
ls -G ls --color Color output
sysrc systemctl enable Service enablement
service systemctl Service management
pkg apt/yum/pacman Package management
truss strace System call tracing
ktrace Built-in Kernel tracing
gpart fdisk/parted Partition management
geom mdadm/dmsetup Disk geometry management
jail docker/lxc Containerization

Configuration Locations

FreeBSD: - /etc/rc.conf - System configuration - /etc/jail.conf - Jail configuration - /etc/pf.conf - Firewall rules - /etc/master.passwd - User database - /etc/group - Group database

Linux: - /etc/systemd/ - Systemd configuration - /etc/default/ - Service defaults - /etc/iptables/ - Firewall rules - /etc/passwd - User database - /etc/group - Group database


🧠 Best Practices for Cross-Platform Scripts

Detection and Adaptation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Detect FreeBSD vs Linux
detect_platform() {
    case "$(uname -s)" in
        FreeBSD)
            echo "freebsd"
            ;;
        Linux)
            echo "linux"
            ;;
        *)
            echo "unknown"
            ;;
    esac
}

PLATFORM=$(detect_platform)

# Platform-specific functions
install_package() {
    local package="$1"

    case "$PLATFORM" in
        freebsd)
            pkg install -y "$package"
            ;;
        linux)
            if command -v apt-get >/dev/null 2>&1; then
                apt-get update && apt-get install -y "$package"
            elif command -v yum >/dev/null 2>&1; then
                yum install -y "$package"
            fi
            ;;
    esac
}

# Service management abstraction
manage_service() {
    local service="$1"
    local action="$2"

    case "$PLATFORM" in
        freebsd)
            service "$service" "$action"
            ;;
        linux)
            systemctl "$action" "$service"
            ;;
    esac
}

🧾 See Also