
nscd Command in Linux
The nscd command in Unix and Linux stands for Name Service Cache Daemon. It's a daemon that provides a cache for the most common name service requests, such as passwd, group, and hosts databases. These requests typically include user account information, group information, and host information. By caching these results, nscd reduces the time needed for repeated lookups, which can be especially beneficial in environments with high traffic and frequent queries.
The default behavior of nscd is configured through the /etc/nscd.conf file, where various settings can be adjusted to optimize caching performance. Notably, nscd maintains separate caches for positive and negative results, each with its own time-to-live (TTL) values. This distinction allows for more efficient caching, as positive results (items found) and negative results (items not found) are treated differently.
In addition, nscd provides caching for accesses to the following databases through standard C library (libc) interfaces −
- passwd − For user account information, interfaces like getpwnam and getpwuid.
- group − For group information, interfaces like getgrnam and getgrgid.
- hosts − For host information, interfaces like gethostbyname.
However, it's important to note that the shadow password file is not cached, meaning calls to getspnam remain uncached. This behavior prevents changing from one non-nscd user to another via the su service when nscd is running.
Table of Contents
Here is a comprehensive guide to the options available with the nscd command −
Syntax of nscd Command
The following is the general syntax for the nscd command −
nscd [options]
nscd Command Options
The following table provides a detailed list of the different options available for the nscd command −
Options | Description |
---|---|
-d, --debug | Run nscd in debug mode, where it won't fork and will display messages on the current terminal. |
-f, --config-file=NAME | Use the specified configuration file NAME instead of the default. |
-F, --foreground | Run nscd in the foreground, behaving like a daemon without forking. |
-g, --statistics | Print current configuration statistics. |
-i, --invalidate=TABLE | Invalidate the specified cache table (e.g., passwd, group, hosts, services, netgroup). |
-K, --shutdown | Shut down the running nscd daemon. |
-p, --print=NAME | Print the contents of the offline cache file NAME. |
-t, --nthreads=NUMBER | Start nscd with the specified number of threads. |
-?, --help | Display the help list with available options. |
--usage | Give a brief usage message. |
-V, --version | Print the program version. |
Examples of nscd Command in Linux
In this section, we'll explore various practical examples that should help you manage the nscd service and cache more effectively.
- Run nscd in Debug Mode
- Use an Alternative Configuration File
- Run nscd in the Foreground
- Print Current Configuration Statistics
- Invalidate a Specific Cache Table
- Shut Down the nscd Daemon
- Print the Version of nscd
- Start nscd with a Specific Number of Threads
Run nscd in Debug Mode
To start nscd in debug mode, use the -d option. This mode is particularly useful for troubleshooting, as it allows you to see detailed messages on the current terminal −
sudo nscd -d
In this example, nscd won't fork and will display debug messages on the terminal, helping you understand how nscd is operating and any potential issues it encounters.

Use an Alternative Configuration File
To specify an alternative configuration file for nscd, use the -f option followed by the path to the configuration file −
sudo nscd -f /home/Tutorialspoint/custom_nscd.conf
In this example, nscd will use the specified configuration file instead of the default /etc/nscd.conf, allowing you to control its behavior based on the custom settings in the specified file.

Run nscd in the Foreground
To run nscd in the foreground, use the -F option. This is useful for monitoring nscd directly −
sudo nscd -F
This command allows nscd to run in the foreground like a daemon without forking, enabling you to observe its operations directly on the terminal.

Print Current Configuration Statistics
To print the current configuration statistics of nscd, you can simply use the "-g" option. This can help you monitor the effectiveness of the caching and understand the current state of the cache.
sudo nscd -g
Running this command will output the current statistics of the nscd cache, providing you with valuable information about cache hits, misses, and other relevant metrics.

Invalidate a Specific Cache Table
To invalidate (clear) a specific cache table, such as the hosts cache, use the "-i" option followed by the name of the table. This is useful when you want to clear outdated or incorrect entries from the cache.
sudo nscd -i hosts
In this example, the host's cache will be invalidated, removing all current entries. This forces nscd to refresh its cache by querying the name service again for subsequent requests.

Shut Down the nscd Daemon
To shut down the running nscd daemon, you can use the "-K" option. This is useful when you need to stop nscd for maintenance or troubleshooting purposes.
sudo nscd -K
Running this command will stop the nscd daemon, halting its caching operations until it is restarted.

Print the Version of nscd
To print the version of nscd, you can use the "-V" flag. This allows you to check which version of nscd is installed on your system.
sudo nscd -V
This command helps you verify the version of nscd and ensure that you are using the correct version for your needs.

Start nscd with a Specific Number of Threads
To start nscd with a specified number of threads, you can simply use the "-t" option followed by the number of threads. This can help optimize performance based on your system's workload.
sudo nscd -t 4
In this example, nscd will start with 4 threads, potentially improving its performance by handling multiple requests concurrently.

Conclusion
The nscd command is an essential tool for optimizing system performance by caching frequently requested name service data, such as user accounts, groups, and hostnames. By reducing lookup times for these requests, nscd helps to minimize the load on underlying services and improve overall system efficiency, especially in high-traffic environments.
In this tutorial, we provided a detailed overview of the nscd command, covering its syntax, available options, and practical examples. From running nscd in debug mode to managing cache tables and adjusting the number of threads, these examples give you the flexibility to configure and control nscd based on your system's specific needs.
Whether you're troubleshooting or optimizing your server's caching performance, mastering the nscd command is an important skill for administrators seeking to improve system responsiveness and reliability.