Install Sudo for FreeBSD

FreeBSD doesn’t come with sudo pre-installed. Instead you are required to log in as root to make changes to the system. This is not best practise as you can do plenty of damage to your system when running as root.

With that said, for this task, you will need to be running as root.

You can easily install sudo for FreeBSD. We will go through this now. Before we start, make sure the ports tree is up to date.

portsnap fetch update

Type out the following command to change directory to the sudo port and commence the installation.

cd /usr/ports/security/sudo/ && make install clean

Leave the defaults selected, but be sure to also select insults so you get a witty message when you incorrectly enter your sudo password – then select ok.

The port will now build. Once it has completed, you will be required to add a user to the sudoers. To edit the sudoers file it is recommended to use the visudo tool. It can be ran by entering the following:

visudo

On FreeBSD, visudo uses vim as the default editor. If you haven’t used vim before it can be a little daunting. First of all we need to navigate down to find the line root ALL=(ALL) ALL. This can be done using either the J or the down arrow keys.

Once you have found that line, enter the letter twice to create a new line and activate insert mode. Add the line below, substituting username with your actual username. If you haven’t add a user yet, you can do this with the adduser command.

username ALL=(ALL) ALL

We now need to save the file and quit vim. You can do this with the following command:

:wq

You can now log in without root and superuser do, the way Todd. C. Miller intended.

Resizing MBR Partition FreeBSD

Recently, the FreeBSD virtual machine that hosts my internal wiki got low on space. I had not provisioned a large enough virtual hard drive when creating the VM. When you want to store all the things in the wiki, you will need all the space. So, I will guide you through the steps for increasing the MBR partition size in FreeBSD.

First things first: Backup the important data

Second: Backup the important data

Now that you’ve got your data backed up, lets have a look at our current drive usage. This can be done using the following command:

df -Ph
Filesystem     Size    Used   Avail Capacity  Mounted on
/dev/da0s1a 120G 27G 84G 24% /
devfs 1.0K 1.0K 0B 100% /dev

Because I didn’t know I was going to blog this, my drive already has enough space. But because I’m greedy, and want to educate others, I will resize the drive to 256GB.

Let have a look at our current hard drive partitions. You can do this using gpart:

gpart show
=>       63  268435393  da0  MBR  (128G)
63 1985 - free - (993K)
2048 268433408 1 freebsd [active] (128G)

=> 0 268433408 da0s1 BSD (128G)
0 260046848 1 freebsd-ufs (124G)
260046848 8386560 2 freebsd-swap (4.0G)

I powered off my VM and provisioned more drive storage. Powered it up again and ran the above command a second time. You can see the newly acquired free space.

=>       63  536870849  da0  MBR  (256G)
63 1985 - free - (993K)
2048 268433408 1 freebsd [active] (128G)
268435456 268435456 - free - (128G)

=> 0 268433408 da0s1 BSD (128G)
0 260046848 1 freebsd-ufs (124G)
260046848 8386560 2 freebsd-swap (4.0G)

Before resizing a partition we are required to get rid of the swap partition. First of all disable swap with the following command:

sudo swapoff -all
swapoff: removing
/dev/da0s1b as swap device

We can now delete the swap partition. Ensure the index matches the index of the swap-partiton and the drive identifier corresponds with the correct drive in the BSD section.

sudo gpart delete -i 2 da0s1

Before we can use the free space we need resize the MBR section.

sudo gpart resize -i 1 da0

Confirm the freespace has moved to BSD using gpart.

gpart show
=>       63  536870849  da0  MBR  (256G)
63 1985 - free - (993K)
2048 536868864 1 freebsd [active] (256G)

=> 0 536868864 da0s1 BSD (256G)
0 260046848 1 freebsd-ufs (124G)
260046848 276822016 - free - (132G)

You will also see the swap partition is gone.

Now we want to resize the hard drive – be sure to leave enough create a new swap partition.

sudo gpart resize -i 1 -s 252G -a 4k da0s1

That will leave me with 4GB to create a swap drive which can be done with the following command:

sudo gpart add -t freebsd-swap -a 4k da0s1

Check that the partitions have been resized and created using gpart:

gpart show da0s1
=>        0  536868864  da0s1  BSD  (256G)
0 528482304 1 freebsd-ufs (252G)
528482304 8386560 2 freebsd-swap (4.0G)

Now we need to enable the swap drive and grow the file system to fit the new partition. This can be done with the following commands:

sudo swapon -a
sudo growfs /

You will need to confirm that you wish the resize the partition. Did you remember to make a backup?

Device is mounted read-write; resizing will result in temporary write suspension for /. It's strongly recommended to make a backup before growing the file system. OK to grow filesystem on /dev/da0s1a, mounted on /, from 124GB to 252GB? [yes/no]

We can check our partitions again with gpart:

gparts show
=>       63  536870849  da0  MBR  (256G)
63 1985 - free - (993K)
2048 536868864 1 freebsd [active] (256G)

=> 0 536868864 da0s1 BSD (256G)
0 528482304 1 freebsd-ufs (252G)
528482304 8386560 2 freebsd-swap (4.0G)

If everything looks good, we can then check our hard drive usage with the following command.

df -Ph
Filesystem     Size    Used   Avail Capacity  Mounted on
/dev/da0s1a 244G 27G 198G 12% /
devfs 1.0K 1.0K 0B 100% /dev

And there is it – You have now resized your partitions and can store all the files the way data hoarders intended.

Installing Screen for FreeBSD

Screen is an application that allows a user to run multiple sessions within a single terminal window. This is handy if multiple programs are required to be run simultaneously by allowing the user to swap between different ‘screens’ to view the output. Another feature of screen is the ability to restore a remote session if a connection is lost. Using screen is a great habit to get into, especially when running tasks that require a longer running time, such as when installing updates or compiling software.

To install screen ensure your ports are up-to-date by running:
portsnap fetch update

After this completes you will need to change into the directory that the screen port is located:
cd /usr/ports/sysutils/screen

And then to install screen type:
make install clean

Screen will now be installed.

One example for using screen is so that you can restore a remote session if a connection is lost. To do this type in screen after logging in via ssh.

If the connection is lost, log back in via ssh and type screen -RD the session will be restore where you left off.

I hope you find this information useful!