Table of contents
1.
Introduction
2.
Configuration
2.1.
Profile Support since Chef 13.7
3.
Knife Config 
4.
config.rb Configuration File
4.1.
Load Path Priority
4.2.
config.rb Configuration Within a Chef Repository 
5.
Setting Your Text Editor
5.1.
Using Quotes
6.
Frequently Asked Questions
6.1.
What is a Chef client?
6.2.
Which text editors can be used to edit the config files?
6.3.
In which file the Knife installed plugins are present?
7.
Conclusion
Last Updated: Aug 13, 2025

Setting up Knife

Author Nagendra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Knife is a command-line program that acts as a conduit between a local chef repository and the Chef Infra Server. The knife command line tool needs to be set up to communicate with the Chef Infra Server and any other infrastructure within your company. It is used for managing other Chef features as well as uploading cookbooks. It offers a connection between the local machine's chefDK (Repo) and the Chef server. 

This blog explains the details of Setting up Knife along with the details of Knife configuration, configuration file, and setting your text editor.

Without further ado, let's get started.

Chef Image

Configuration

A config.rb file was suggested for setting up knives in previous Chef Infra configurations. Run the Knife configure to create a Chef Infra credentials file at  ~/.chef/credentials to set up a knife to communicate with Chef Infra Server for the first time. In order to work with a single Chef Infra Server and a single Chef Infra Server organisation, the configuration of knives using config.rb is still valid.

Command:

New-Item -Path "c:\" -Name ".chef" -ItemType "directory"
New-Item -ItemType "file" -Path "c:\.chef\config.rb"

 

Any Ruby code can be included in the configuration file's config.rb file to go beyond static values. Environmental variables from the workstation can be loaded using this. As a result, you may create a single config.rb file that all users in your company can use. Additionally, you may check in this single file to your chef-repo, which will enable users to load various config.rb files depending on which chef-repo they run commands from. When each chef-repo leads to a distinct chef server or company, this can be extremely helpful.

Example:

current_dir = File.dirname(__FILE__)
  user = ENV['CHEF_USER'] || ENV['USER']
  node_name user
  client_key "#{ENV['HOME']}/chef-repo/.chef/#{user}.pem"
  chef_server_url "https://api.opscode.com/organizations/#{ENV['ORGNAME']}"
  syntax_check_cache_path "#{ENV['HOME']}/chef-repo/.chef/syntax_check_cache"
  cookbook_path ["#{current_dir}/../cookbooks"]
  cookbook_copyright "Your Company, Inc."
  cookbook_license "Apache-2.0"
  cookbook_email "cookbooks@yourcompany.com"

  # Amazon AWS
  knife[:aws_access_key_id] = ENV['AWS_ACCESS_KEY_ID']
  knife[:aws_secret_access_key] = ENV['AWS_SECRET_ACCESS_KEY']
You can also try this code with Online Ruby Compiler
Run Code

Profile Support since Chef 13.7

Knife profiles make it simpler to switch knives between organisations on the same Chef Infra Server or between Chef Infra Servers. There is an alternative to config.rb called knife profiles; you cannot use both. Create your knife profiles by including them in the .chef/credentials file located in your home directory on your workstation. There is a TOML format in the credentials file. Your choice of "table" name is listed after each profile, followed by key=value pairs. The keys match any setting that is allowed in the config.rb file.
Unless you specify an absolute path, file names like client_key or validator_key are relative to ~/.chef. Use client name (recommended) or node name to identify clients. Keep a distinct profile of credentials in the credentials file for use with target mode (chef-client —target switch.example.org). When the name contains a period, use the DNS name of the target as the profile name and enclose it in single quotes. Keys that are legitimate configuration choices, like port, are sent to the train.

Code:

[default]
client_name = "barney"
client_key = "barney_rubble.pem"
chef_server_url = "https://api.chef.io/organizations/bedrock"


[default.knife]
ssh_user = 'ubuntu' 
aws_profile = 'engineering'
use_sudo = true


# client key
[dev]
client_name = "admin"
client_key = """
-----BEGIN RSA PRIVATE KEY-----
#RSA private key
MIICXAIBAAKBgQCqGKukO1De7zhZj6EXAMPLEKEY
...ABC123=
-----END RSA PRIVATE KEY-----
"""
#validator text
validator_key = "test-validator.pem"
#server url
chef_server_url = "https://api.chef-server.dev/organizations/test" 


#Web Production
['web.preprod']
client_name = "ninja"
client_key = "preprod-brubble.pem"
chef_server_url = "https://preprod.chef-server.dev/organizations/preprod"


['switch.example.org']
user = "cisco"
password = "cisco"
enable_password = "cisco"
You can also try this code with Online Ruby Compiler
Run Code


In order of priority, there are four options to choose which profile to use:

  • Knife should be given the —profile option, for example, knife node list —profile dev.
     
  • In the CHEF PROFILE environment variable, enter the name of the profile.
     
  • To the /.chef/context file, add the profile name.
     
  • If not, Knife use the "default" profile.
     

Let's look at the details of knife configuration.

Knife Config 

To manage your knife profiles, use the knife configuration command.

Use the knife config list-profiles command to list all of your profiles.

Command:

knife config list-profiles

 

The currently chosen profile is the line that starts with the asterisk. Run the knife config use-profile NAME command to modify the current profile. This will add the new profile name to the /.chef/context file. The name of the currently chosen profile is printed out when knife config get-profile is run.

If you need to troubleshoot any of the settings, you may use the knife config get KEY command to see what value is being used, for instance:

Command:

knife config get chef_server_url


Loading from credentials file /home/barney/.chef/credentials

chef_server_url: https://api.chef-server.dev/organizations/test

 

Let's look at the details of the config.rb file.

config.rb Configuration File

The knife command-line tool's configurations and those of any installed knife plugins are found in the config.rb file.

Load Path Priority

Every time the knife command is used, the config.rb file loads using the following load order:

  • Using a specific location when the —config flag is present
     
  • If the $KNIFE_HOME environment variable is set, from a specific location.
     
  • Using the ./config.rb file located in the current working directory.
     
  • From a config.rb file located in the current working directory .chef directory, for example,./.chef/config.rb
     
  • From a config.rb file located one directory up from the current working directory in the.chef directory, for example, ../.chef/config.rb
     
  • From c:\Users\<username>\.chef (Windows platform) or ~/.chef/config.rb (Mac OS X and Linux platforms)

config.rb Configuration Within a Chef Repository 

Create your initial config.rb file in your home directory using the Knife configure command.

It's time to dive into the details of setting up the text editor.

Setting Your Text Editor

A text editor must be used to edit data as JSON for some knife operations, such as knife data bag edit. For illustration, the command:

Command:

knife data bag edit admins admin_name

Output:

{
  "id": "admin_name"
}

 

Let's make some changes to the file:

{
  "id": "Coding Ninjas"
  "description": "Text Message"
}

 

You can modify Knife's default text editor by adding a line to your config.rb file or by using the EDITOR environment variable. For instance, include the following code in your config.rb file to instruct Knife to launch the vim text editor:

Command:

C:\\Program Files (x86)\EditPad Pro\EditPad.exe

 

The text would be similar as :

knife[:editor] = "C:\\Progra~1\\EditPa~1\\EditPad.exe"

Put the single-quotes outside of the double-quotes as a way to get around the double-quote vs. single-quote conundrum. For instance, in Notepad++:

Command:

knife[:editor] = '"C:\Program Files (x86)\Notepad++\notepad++.exe" -nosession -multiInst'

 

Sublime Text:

Command:

knife[:editor] = '"C:\Program Files\Sublime Text 2\sublime_text.exe" --wait'

 

TextPad:

Command:

knife[:editor] = '"C:\Program Files (x86)\TextPad 7\TextPad.exe"'


Vim:

Command:

knife[:editor] = '"C:\Program Files (x86)\vim\vim74\gvim.exe"'

Using Quotes

Spaces that are not correctly enclosed in quotes cannot be used in text editor commands. Single quotations (' ') or double quotes (" ") can be used to enclose the command. However, this should be done consistently, as illustrated in the examples above.

Check out this problem - Smallest Distinct Window.

Frequently Asked Questions

What is a Chef client?

Every node that Chef manages has an agent running locally called a chef-client. When a chef-client is launched, it will carry out every action necessary to put the node in the desired state, such as registering and authenticating the node with the Chef server.

Which text editors can be used to edit the config files?

Text editors like Notepad++, TextPad, and vim text editor can be used to edit the config file.

In which file the Knife installed plugins are present?

All the Knife installed plugins can be found in the config.rb file.

Conclusion

In this article, we have extensively discussed the details of Setting up Knife along with the details of Knife configuration, configuration file, and setting your text editor.

Recommended Reading: 

wc command in linux

We hope that this blog has helped you enhance your knowledge regarding Setting up Knife, and if you would like to learn more, you can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews. Do upvote our blog to help other ninjas grow. Happy Coding!!

Thank You image
Live masterclass