capng_restore_state - Linux


capng_restore_state restores a previously captured process’s capabilities. It takes an encoded data structure representing the process’s capabilities and sets the capabilities accordingly. This allows you to temporarily modify a process’s capabilities, perform operations that require elevated privileges, and then restore the original capabilities.


capng_restore_state <encoded data structure>




Restore capabilities from a previously captured state:

capng_get_state 1234 > state.txt  # Capture original state
setcap cap_net_admin=+eip 1234     # Modify capabilities
capng_restore_state < state.txt  # Restore original state

Use capng_restore_state in a script:

capng_get_state $PID > state.txt
setcap cap_sys_admin=+eip $PID
sleep 10  # Do privileged operations
capng_restore_state < state.txt

Common Issues

  • Ensure that the provided encoded data structure is valid. Errors may occur if the data is corrupted or invalid.
  • Check that you have sufficient privileges to restore the process’s capabilities.


Combine capng_restore_state with other commands to perform advanced tasks:

  • Capture and restore capabilities in a Bash script: Use a loop to capture and restore capabilities for multiple processes.
  • Restore capabilities after using sudo: Capture capabilities before executing a command with sudo, and restore them afterward.

Related Commands

  • capng_get_state: Captures a process’s capabilities.
  • setcap: Sets the capabilities for a process or file.
  • getcap: Queries the capabilities of a process or file.