knife
Manage Chef infrastructure from the command line
TLDR
Bootstrap a new node
List all registered nodes
Show a node
Edit a node
Edit a role
View a data bag
Upload a local cookbook to the Chef server
SYNOPSIS
knife [global_options] subcommand [subcommand_options] [arguments]
PARAMETERS
-c CONFIG, --config CONFIG
The configuration file to use (default: ~/.chef/knife.rb).
-e EDITOR, --editor EDITOR
The text editor to use for creating or editing Chef objects.
-f FORMAT, --format FORMAT
The output format. Common formats include summary, text, json, and yaml.
-k KEY, --key KEY
The API client key file to use for authentication with the Chef server.
-o PATH, --chef-repo-path PATH
The path to the Chef repository. knife often expects to be run from or configured to point to a chef-repo.
-s URL, --server-url URL
The URL of the Chef server.
-u USER, --user USER
The Chef server API client name (e.g., an administrator user or client).
-v, --version
Show knife version.
-V, --verbose
Increase verbosity of output messages.
-y, --yes
Assume 'yes' to all prompts, useful for automation.
--local-mode
Run knife in local mode, operating directly against files in the Chef repository instead of communicating with a Chef server.
DESCRIPTION
knife is the powerful command-line tool for interacting with a Chef server and managing your Chef infrastructure. It provides an interface between a local Chef repository (typically a chef-repo directory) and the Chef server. Used by Chef administrators and developers, knife enables a wide range of operations, including uploading cookbooks, roles, data bags, and environments; managing nodes, users, and clients; bootstrapping new machines; executing remote commands via SSH; and searching Chef server data.
As the primary tool for Chef workflows, knife is essential for deploying, managing, and maintaining your infrastructure as code. Its functionality is heavily subcommand-driven, meaning you invoke knife followed by a specific action (e.g., knife node list, knife cookbook upload), each with its own set of options and arguments. It requires proper configuration, often through a knife.rb file, to connect to the Chef server.
CAVEATS
knife is intrinsically linked to the Chef ecosystem; it requires either a running Chef server or configuration for --local-mode operation. It is not a standalone general-purpose Linux utility. Its extensive subcommand structure means that while global options apply broadly, each subcommand has its own specific set of options and arguments that must be learned independently. Proper configuration of knife.rb and client keys is crucial for successful operation.
COMMON SUBCOMMANDS
knife's power comes from its many subcommands, each performing a specific task. Some of the most frequently used include:
- knife bootstrap: Installs the Chef client on a target node and performs the initial client run.
- knife cookbook upload: Uploads one or more cookbooks from the local Chef repository to the Chef server.
- knife node list: Displays a list of all nodes registered with the Chef server.
- knife ssh: Allows running commands via SSH on nodes identified by Chef search queries.
- knife search: Searches Chef server data (nodes, roles, environments, data bags) using Chef search queries.
- knife data bag create: Creates a new data bag on the Chef server.
- knife role from file: Creates or updates a role from a local JSON or Ruby file to the Chef server.
CONFIGURATION (KNIFE.RB)
The behavior of knife is largely controlled by a configuration file, typically named knife.rb. This file, usually located in ~/.chef/ or at the root of a chef-repo, specifies critical settings such as the Chef server URL, the client name (user), the path to the client key, and default editor. Proper configuration of knife.rb is essential for knife to authenticate and communicate with the Chef server successfully.
HISTORY
knife has been a cornerstone of the Chef ecosystem since its inception by Opscode (now Chef Software, Inc.) around 2009-2010. Designed as the primary command-line interface for Chef, it enabled administrators and developers to manage their infrastructure as code by interacting with the Chef server. Over the years, knife has evolved in parallel with the Chef platform, gaining new subcommands and functionalities to support cloud integrations, containerization, and advanced infrastructure automation patterns. Its foundational design as a subcommand-driven Ruby application has remained consistent, allowing it to adapt to the changing landscape of DevOps and infrastructure management.
SEE ALSO
chef-client(8), ohai(8)