FreeBSD Install sudo Command To Execute Commands

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 install sudo command under a FreeBSD 9.x/10.x/11.x/12.x/13.x system?

Tutorial details
Difficulty level Intermediate
Root privileges Yes
Requirements FreeBSD
Est. reading time 5m
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.

FreeBSD install sudo command

  1. Open the terminal application
  2. For remote server log in using the ssh command. For example: ssh ec2-user@ec2-freebsd-server-ip
  3. Update your pkg database, run: pkg update && pkg upgrade.
  4. Install sudo on FreeBSD by typing the pkg install sudo command.
  5. Configure sudo access on FreeBSD as per needs

Let us see how to install and use sudo on a FreeBSD system in details.

Install sudo using FreeBSD port system

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

FreeBSD Install sudo command 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...
Fetching meta.txz: 100%    916 B   0.9kB/s    00:01    
Fetching packagesite.txz: 100%    6 MiB 955.9kB/s    00:07    
Processing entries: 100%
FreeBSD repository update completed. 31972 packages processed.
All repositories are up to date.
The following 1 package(s) will be affected (of 0 checked):
New packages to be INSTALLED:
	sudo: 1.9.3p1
Number of packages to be installed: 1
The process will require 4 MiB more space.
925 KiB to be downloaded.
Proceed with this action? [y/N]: y
[1/1] Fetching sudo-1.9.3p1.txz: 100%  925 KiB 473.4kB/s    00:02    
Checking integrity... done (0 conflicting)
[1/1] Installing sudo-1.9.3p1...
[1/1] Extracting sudo-1.9.3p1: 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 to run shell as the target user:
% 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>

In this following example run login shell as the target user:
$ sudo -i
Sample outputs:

root@examplejail:~ #

Use the id command to verify the user and group names and numeric IDs:
$ id
See "How To Add, Delete, and Grant Sudo Privileges to Users on a FreeBSD Server" for detailed info.

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.

How to list user's privileges or check a specific command

To see what commands you are allowed to run, type:
$ sudo -l
$ sudo -ll

Sample outputs:

User vivek may run the following commands on examplejail:

Sudoers entry:
    RunAsUsers: ALL

Summing up

We leanred how to insall and configure sudo on FreeBSD Unix operating system running in the cloud. See the following for more info:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 5 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
5 comments… add one
  • James May 5, 2013 @ 7:47

    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

  • Praetorianus Nov 8, 2016 @ 16:30

    Congrats Vivek,

    Very simple, direct and help me a lot.

  • Eric Jan 1, 2021 @ 8:18

    I agree very simple end easy to following guide for me. I was looking for “how to install sudo on FreeBSD”. The wheel group did the trick for me like Ubuntu system.

  • vscott Jan 2, 2021 @ 14:23

    I was getting “freebsd insufficient privilege upgrade” error. After wasting so much time I login to server console. Installed sudo and it fix was deployed. Why sudo is not installed on FreeBSD like CentOS or Debian server by default?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum