温馨提示×

centos cpuinfo配置技巧

小樊
52
2025-09-28 14:15:15
栏目: 智能运维

Understanding /proc/cpuinfo in CentOS
The /proc/cpuinfo file is a virtual file generated by the Linux kernel that provides detailed information about the system’s CPU(s), including model name, core count, thread count, frequency, and cache size. It is read-only—modifying it directly can lead to system instability or failure. For most users, viewing this file is sufficient to understand CPU configuration.

Key Commands for Viewing CPU Information
To efficiently retrieve specific CPU details, use these command-line tools:

  • lscpu: A user-friendly command that summarizes CPU architecture (e.g., x86_64), physical sockets, cores per socket, threads per core, and CPU frequency. It’s ideal for quick overviews.
  • cat /proc/cpuinfo: Displays raw, detailed CPU information. Combine with grep to filter specific data:
    • View CPU model: grep "model name" /proc/cpuinfo | uniq (shows unique model names).
    • Count logical cores: grep -c ^processor /proc/cpuinfo (total logical processors).
    • Count physical CPUs: grep "physical id" /proc/cpuinfo | sort | uniq | wc -l (unique physical CPU IDs).
  • dmidecode: Provides hardware-level details (requires root). Use sudo dmidecode -t processor to get CPU model, socket count, and manufacturer.

Adjusting CPU Performance Settings
While you can’t modify /proc/cpuinfo, you can optimize CPU behavior using these tools:

  • cpufrequtils: Adjust CPU frequency scaling to balance performance and power usage. Install it with sudo yum install cpufrequtils. Common commands:
    • Set performance mode (maximize performance): sudo cpufreq-set -g performance.
    • Set a specific frequency (e.g., 2.0 GHz for core 0): sudo cpufreq-set -c 0 -f 2.0GHz.
  • Kernel Parameters: Modify /proc/sys/kernel/irqaffinity to disable CPU节能模式 (set to “performance”) or enable it (set to “powersave”). Apply changes with sysctl -p.

Limiting CPU Usage for Processes
To prevent a process from consuming excessive CPU resources, use cpulimit (install via sudo yum install cpulimit). For example, to limit process ID 1234 to 50% CPU usage: cpulimit -p 1234 -l 50. This is useful for maintaining system stability under heavy load.

Sharing CPU Information Securely
If you need to share CPU details with other systems or users, use these methods:

  • Text File: Run lscpu > cpu_info.txt to save CPU info to a file, then copy it via SCP/FTP.
  • Network Sharing:
    • Samba: Install Samba (sudo yum install samba), configure /etc/samba/smb.conf to share the CPU info file (e.g., [CPUInfo] path = /path/to/cpu_info.txt read only = yes guest ok = yes), and restart Samba (sudo systemctl restart smb).
    • Web Server: Place the file in Apache/Nginx’s root directory (e.g., /var/www/html/) and access it via browser.
      Always restrict access to sensitive files (e.g., use passwords for Samba, HTTPS for web servers).

0