MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Splunk Command Line Reference

This page is a quick reference for the Splunk command line interface (CLI): adding and removing data inputs, sending data to an index, setting up forwarding and receiving, checking configuration with btool, and cleaning or removing indexes. Below the original command list you will find corrected versions of a few commands, the order to run them in, ways to check the results, and fixes for common errors.

How to add monitor in Splunk?
$ sudo ./splunk [add|edit|remove|list] [monitor|exex|tcp|udp|oneshot]
source - file, directory, scripted input, or socket to manage

How to remove monitor?
$ sudo ./splunk remove monitor /var/log/jenkins

How to set hostname?
$ sudo ./splunk add monitor /var/log/dmesg -hostname rajesh -index newindex
$ sudo ./splunk add monitor /opt/lampp/etc -hostname rajhost -index rajesh

How to upload to new index?
$ sudo ./splunk add monitor /var/log/dmesg -hostname rajesh -index newindex

How to upload a file?
$ sudo ./splunk add oneshot /var/log/applog	
$ sudo ./splunk add oneshot C:\Program Files\AppLog\log.txt
$ sudo ./splunk add forward-server <host>:<port> -auth <username>:<password>

Alternatively, if you have many forwarders, you can use an outputs.conf file to specify the receiver. For example:
[tcpout:my_indexers]
server= splunk_indexer.acme.com:9997

This command, <port> is the network port you want the receiver to listen on.
$ sudo ./splunk enable listen <port> -auth <username>:<password>
$ sudo ./splunk enable listen 9997 -auth <username>:<password>

This command below will also show which apps each setting is coming from.
$ sudo ./splunk cmd btool --debug inputs list

Permanently remove event data from an index by typing
$ splunk clean eventdata
$ splunk clean eventdata -index <index_name>
$ splunk stop
$ splunk clean eventdata 	# To permanently remove data from all indexes
$ splunk stop
$ splunk clean eventdata -index _internal -f # To permanently remove data from _internal

Remove all data from one or all indexes
$ splunk help clean

Remove an index entirely
$ splunk stop
$ splunk remove index main # cannot remove idx=main, is internal
$ splunk remove index <index_name>

Disable an index without removing it
$ splunk disable index <index_name>

Code language: HTML, XML (xml)

Before you run any Splunk CLI command

The splunk binary is in $SPLUNK_HOME/bin. The default location is /opt/splunk on a Splunk Enterprise server and /opt/splunkforwarder on a universal forwarder. The example above switches between ./splunk and splunk. Both work: ./splunk means you are in the bin directory, and plain splunk means that directory is on your PATH.

export SPLUNK_HOME=/opt/splunk          # or /opt/splunkforwarder
export PATH=$SPLUNK_HOME/bin:$PATH

splunk status                           # is splunkd running?
splunk version
splunk login                            # authenticate once per sessionCode language: PHP (php)

Most commands that change configuration need splunkd to be running and need you to log in. If you run splunk login first, you don't have to add -auth <username>:<password> to every command, and your password stays out of your shell history.

Note: The example above starts every command with sudo. Splunk should run as a dedicated non-root user, usually splunk. If you run CLI commands as root, files under $SPLUNK_HOME can end up owned by root, and the service then fails to start as the splunk user. Use sudo -u splunk splunk <command> or switch to that user instead.

Corrected commands from the example above

Most of the original list is valid, but a few commands contain typos, run in the wrong order, or run on the wrong host. Here are the corrected versions:

# Generic syntax: input type is exec, not "exex"; oneshot only supports "add"
splunk [add|edit|remove|list] [monitor|exec|tcp|udp] <source> [options]
splunk add oneshot <file> [options]

# Create the index BEFORE sending data to it
splunk add index newindex
splunk add monitor /var/log/dmesg -hostname rajesh -index newindex -sourcetype dmesg

# Windows oneshot: no sudo, quote paths that contain spaces, run from %SPLUNK_HOME%\bin
.\splunk.exe add oneshot "C:\Program Files\AppLog\log.txt" -index main

# Clean event data: stop Splunk FIRST, then clean, then start
splunk stop
splunk clean eventdata -index <index_name> -f
splunk start

# Removing an index does not require a stop
splunk remove index <index_name>Code language: PHP (php)
  • exex is a typo for exec, the input type for scripted inputs.
  • add monitor ... -index newindex does not create the index. If the index doesn't exist, the input is saved anyway, but events sent to the missing index are dropped and splunkd logs a warning. Create the index first with splunk add index.
  • sudo does nothing on Windows, and an unquoted path with a space (C:\Program Files\...) gets split into two arguments.
  • splunk clean eventdata only works when Splunk is stopped. The example above runs it before splunk stop.
  • splunk remove index runs against a live instance, so you don't need the splunk stop that comes before it in the example above.
  • splunk add forward-server goes on the forwarder, not the indexer. It is a forwarding command, not a file-upload command, so it belongs in its own group.

Managing data inputs

monitor watches a file or directory continuously and indexes new data as it is written. oneshot indexes a file once and doesn't watch it afterwards, which is useful for historical logs or a quick test.

# Continuous monitoring with metadata
splunk add monitor /var/log/jenkins -index ci -sourcetype jenkins -hostname build01

# Inspect and edit
splunk list monitor
splunk edit monitor /var/log/jenkins -sourcetype jenkins:log

# Stop monitoring (data that is already indexed is NOT deleted)
splunk remove monitor /var/log/jenkins

# One-time upload
splunk add oneshot /var/log/applog -index main -sourcetype applog

# Network inputs
splunk add tcp 5514 -sourcetype syslog -index network
splunk add udp 514 -sourcetype syslog -index network
splunk list udpCode language: PHP (php)

The CLI writes these inputs to an inputs.conf file, usually in $SPLUNK_HOME/etc/apps/search/local/ or $SPLUNK_HOME/etc/system/local/, depending on the app context. On forwarders that you manage at scale, it's better to put inputs in an app and push it with a deployment server than to run CLI commands on each host.

Note: On Linux, ports below 1024 (such as UDP 514) need root privileges to bind. If Splunk runs as a non-root user, use a high port or put a syslog server in front of Splunk.

Setting up forwarding and receiving

Forwarding needs commands on both ends: the indexer listens, and the forwarder sends. Configure the receiver first.

  1. On the indexer, open a receiving port: splunk enable listen 9997.
  2. Make sure the host firewall allows inbound TCP 9997 from your forwarders.
  3. On the universal forwarder, point it at the indexer: splunk add forward-server splunk_indexer.acme.com:9997.
  4. On the forwarder, add an input: splunk add monitor /var/log/messages -index os.
  5. Check the connection: splunk list forward-server should show the indexer under active forwards.

The outputs.conf fragment in the example above has no default group, so a forwarder doesn't know to use it unless that is the only group defined. A complete minimal file, in $SPLUNK_HOME/etc/system/local/outputs.conf or in a deployment app, looks like this:

[tcpout]
defaultGroup = my_indexers

[tcpout:my_indexers]
server = splunk_indexer1.acme.com:9997, splunk_indexer2.acme.com:9997

If you list several servers, the forwarder load-balances automatically across them. Restart the forwarder after you edit the file by hand: splunk restart.

Checking configuration with btool

Splunk merges .conf files from many apps according to its precedence rules. btool shows the merged result. With --debug, it also shows which file each setting comes from, which is the quickest way to find out why an input or setting isn't doing what you expect.

splunk btool inputs list --debug
splunk btool outputs list tcpout --debug
splunk btool indexes list newindex --debug
splunk btool check                       # report typos and invalid stanzasCode language: PHP (php)

splunk cmd btool, as used in the example above, works too. splunk btool is the shorter form.

Index lifecycle commands

TaskCommandSplunk state
Create an indexsplunk add index <name>Running
List indexessplunk list indexRunning
Disable (keep data)splunk disable index <name>Running
Re-enablesplunk enable index <name>Running
Delete all events in one indexsplunk clean eventdata -index <name> -fStopped
Delete events in all indexessplunk clean eventdata -fStopped
Delete index and its configsplunk remove index <name>Running
Help for clean optionssplunk help cleanEither

Note: clean eventdata and remove index permanently delete data, and you can't undo either one. Test on a non-production instance first. Never clean an index on a clustered indexer peer: indexer clusters manage buckets in their own way, and deleting data on one peer outside that process can leave the cluster inconsistent.

Before you remove an index, find every input that still sends to it (splunk btool inputs list --debug | grep <name>) and remove or repoint those inputs. Otherwise their events are dropped once the index is gone. The main index and internal indexes such as _internal can't be removed, but you can clean their event data.

Verifying that data arrives

After you add an input, confirm that events are indexed before you move on. You can search from the CLI:

splunk search 'index=newindex | head 5'
splunk search 'index=_internal source=*splunkd.log* log_level=WARN* | head 20'Code language: JavaScript (javascript)

The second search lists recent warnings from splunkd. Problems such as events going to a missing index, file permission errors and forwarder connection failures appear there.

Common errors and fixes

SymptomLikely causeFix
Connection refused when you run a CLI commandsplunkd is not runningsplunk start, then retry
Monitor added but no events appearIndex does not exist, or the splunk user can't read the filesplunk add index <name>; grant read access, for example with setfacl -m u:splunk:r <file>
Old file content is not indexed againThe file is already recorded in the fishbucket, so Splunk treats it as seenUse add oneshot for a one-time re-index
clean eventdata refuses to runSplunk is still runningsplunk stop first
Forwarder shows server as configured but inactiveReceiver is not listening, or a firewall blocks the portsplunk enable listen 9997 on the indexer; open TCP 9997
Splunk fails to start after you used sudoFiles under $SPLUNK_HOME are now owned by rootchown -R splunk:splunk $SPLUNK_HOME

Frequently asked questions

What is the difference between splunk add monitor and splunk add oneshot?

add monitor keeps watching a file or directory and indexes new lines as they are written. add oneshot indexes a file's current contents once and doesn't track it afterwards.

Does splunk add monitor create the index automatically?

No. The index named by -index must already exist. Create it with splunk add index <name>, otherwise events sent to it are dropped and splunkd logs a warning.

Do I need to stop Splunk to delete data from an index?

Yes, for splunk clean eventdata. Stop Splunk, run the clean, then start it again. splunk remove index runs while Splunk is up.

Where does the Splunk CLI save inputs I add?

In an inputs.conf file under $SPLUNK_HOME/etc, usually in the search app's or the system's local directory. Run splunk btool inputs list --debug to see the exact file.

How do I avoid typing -auth on every Splunk command?

Run splunk login once. The session stays authenticated for later commands, and your password stays out of your shell history.

Related tutorials

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals

Related Posts

Splunk Tutorial: Install & Configure Heavy Forwarders

A Splunk heavy forwarder is a full Splunk Enterprise installation that forwards data instead of indexing it, and it parses events before sending them on. This guide…

Read More

Splunk Tutorial: Install & Configure Splunk Server

This page shows how to download, install and start Splunk Enterprise on Linux from the .tgz, .rpm or .deb package, and how to run it as a…

Read More

Datadog Tutorial: win32_event_log log setup using powershell

This tutorial shows how to install the Datadog Agent on Windows with PowerShell and ship Windows Event Log channels such as Application, System, Security and Setup to…

Read More

Datadog Tutorials: Tag Specifications

In a Datadog Agent log collection config, the tags field adds key:value tags to every log line the Agent collects from a source. You can then filter,…

Read More

What Error Logs Can Teach You About Website Performance

An error log is the most detailed performance report a website produces, and almost nobody reads it. Every failed request and every server crash leaves a line…

Read More

Datadog Troubleshooting Master Guide

It covers: Datadog Agent, Kubernetes Agent, Cluster Agent, integrations, logs, APM/traces, custom metrics, DogStatsD, OpenTelemetry, API keys, Terraform, monitors, SLOs, RUM, Synthetics, cloud integrations, cost, permissions, and…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x