How to use the command sysctl (with examples)
- Osx
- December 25, 2023
The sysctl
command is used to access kernel state information on macOS. It allows users to view and modify various system variables and parameters. This command is particularly useful for system administrators and advanced users who need to gather information about the system’s hardware and software configuration.
Use case 1: Show all available variables and their values
Code:
sysctl -a
Motivation: This use case is helpful when you need to gather comprehensive information about the system’s current state, including both hardware and software aspects. It provides a complete list of available variables and their respective values, allowing for in-depth analysis and troubleshooting.
Explanation:
The -a
option is used to display all available variables and their values. It retrieves and lists all kernel variables along with their current values.
Example output:
...
kern.osversion: 21G1119
kern.osrevision: 2817.100.429671.1816
kern.version: Darwin Kernel Version 21.3.0: Thu Oct 28 21:38:22 PDT 2021; root:xnu-8019.81.4~3/RELEASE_ARM64_T8019
kern.kaslr.present: 1
kern.corefile: /cores/core.%P
kern.nx: 1
...
Use case 2: Show Apple model identifier
Code:
sysctl -n hw.model
Motivation: Knowing the model identifier of your Apple device can be essential for various purposes, such as identifying compatibility with certain software or troubleshooting hardware-specific issues. This use case allows you to quickly retrieve the Apple model identifier.
Explanation:
The -n
option is used to suppress the printing of variable names and only display the corresponding values. In this case, hw.model
is the specific variable that represents the Apple model identifier.
Example output:
MacBookPro15,2
Use case 3: Show CPU model
Code:
sysctl -n machdep.cpu.brand_string
Motivation: Identifying the CPU model is useful when you need detailed information about your system’s processor, such as its performance characteristics and compatibility with certain software. This use case helps you retrieve the CPU model easily.
Explanation:
Similar to the previous use case, the -n
option is used to display only the value without printing the variable name. Here, machdep.cpu.brand_string
represents the variable that holds the CPU model information.
Example output:
Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Use case 4: Show available CPU features
Code:
sysctl -n machdep.cpu.features
Motivation: Knowing the available CPU features is essential for optimizing software performance or ensuring compatibility with specific CPU instructions. This use case allows you to check which features are supported by your CPU.
Explanation:
Once again, the -n
option is used to suppress the printing of variable names. Here, machdep.cpu.features
gives information about the CPU features, such as MMX, SSE, SSE2, SSE3, AES, etc.
Example output:
FPU VME DE PSE TSC MSR PAE MCE CX8 APIC SEP MTRR PGE MCA CMOV PAT PSE36 CLFSH DS ACPI MMX FXSR SSE SSE2 SS HTT TM PBE SSE3 PCLMULQDQ DTES64 MON DSCPL VMX EST TM2 SSSE3 SDBG FMA CX16 TPR PDCM SSE4.1 SSE4.2 x2APIC MOVBE POPCNT AES PCID XSAVE OSXSAVE SEGLIM64 TSCTMR AVX1.0 RDRAND F16C
Use case 5: Set a changeable kernel state variable
Code:
sysctl -w section.tunable=value
Motivation: Being able to modify kernel state variables can be useful for tweaking system configurations or applying performance optimizations. This use case demonstrates how to set a changeable kernel state variable.
Explanation:
To modify a kernel state variable, the -w
option is used followed by the specific variable you want to change and its desired value. The section.tunable
represents the specific variable, and value
indicates the new value you want to assign.
Example:
sysctl -w kernel.domain_max_size=500
Conclusion:
The sysctl
command is a powerful tool for accessing and modifying kernel state information on macOS. It provides valuable insights into the system’s hardware and software configuration. With the various use cases discussed above, users can leverage the command to gather information, identify hardware components, and tweak system variables to optimize performance or troubleshoot issues.