FreeBSD: Install sudo Command To Execute A Command As The Root

in Categories , , last updated October 24, 2017

I have recently started to managing FreeBSD server along with Ubuntu Linux. However, sudo command, always shows me the following error:

zsh: command not found: sudo

How do I solve this problem and use sudo under a FreeBSD 9.x/10.x/11.x system?

Sudo is a program designed to allow a sysadmin to give limited root privileges to users and log root activity. Unlike su, sudo authenticates users against their own password rather than that of the target user. Sudo allows a system administrator to delegate authority to give certain users (or groups of users) the ability to run some (or all) commands as root or another user while providing an audit trail of the commands and their arguments. This allow the delegation of specific commands to specific users on specific hosts without sharing passwords among them.
Let us see how to install and use sudo on a FreeBSD system.

Install sudo using FreeBSD port system

To install the port, enter:
# cd /usr/ports/security/sudo/ && make install clean

Install sudo using pkg_add or pkg command

To add the binary package use the pkg_add on older version of FreeBSD such as FreeBSD 9.x, run:
# pkg_add -rv sudo
# pkg_add -r sudo
Sample outputs:

Fetching Done.

For the latest version of FreeBSD such as FreeBSD 11.x+ use the pkg command:
# pkg install sudo
Sample outputs:

Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
The following 1 package(s) will be affected (of 0 checked):
New packages to be INSTALLED:
	sudo: 1.8.21p2
Number of packages to be installed: 1
The process will require 4 MiB more space.
909 KiB to be downloaded.
Proceed with this action? [y/N]: y
[1/1] Fetching sudo-1.8.21p2.txz: 100%  909 KiB 930.8kB/s    00:01    
Checking integrity... done (0 conflicting)
[1/1] Installing sudo-1.8.21p2...
Extracting sudo-1.8.21p2: 100%

How do I configure sudo?

The default configuration file is located at /usr/local/etc/sudoers. This file MUST be edited with the visudo command as root. Failure to use visudo may result in syntax or file permission errors that prevent sudo from running.
% su -
# visudo

Allow user vivek to run all admin tasks via sudo:

vivek ALL=(ALL) ALL

Allow members of group wheel to execute any command:

%wheel ALL=(ALL) ALL

Save and close the file.

Task: Become a root user with sudo

Type the following command
% sudo -s
Sample outputs:

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.
Password: <Enter Your Own Password Here>

Task: Run any command as root

The syntax is:

sudo /path/to/command
sudo /path/to/command arg1

In this example, restart ftpd service:
% sudo /etc/rc.d/ftpd restart
Sample outputs:

Password: <Enter Your Own Password Here>
Stopping ftpd.
Starting ftpd.
See also

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Share this on (or read 3 comments/add one below):

3 comment

  1. Nice guide, I have configured sudo on Arch (linux) a few times before, so this was quite straightforward for me.
    However under ‘How do I configure sudo?’
    # visdo
    should surely be
    # visudo

    Have a question? Post it on our forum!