林桦 發表於 2025-6-20 00:00:00

ubuntu查看CPU、内存、硬盘的命令详解

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>1、查看CPU</li><li>2、查看内存</li><li>3、硬盘</li><li>4、其它材料<ul class="second_class_ul"><li>4.1、查看CPU总使用率</li><li>4.2、以下命令也可以查看主机的基本配置信息</li><li>4.3、输入以下命令也可以查看内存信息</li><li>4.4、查看指定目录下磁盘的容量</li></ul></li><li>ubuntu重启网络服务的方法小结<ul class="second_class_ul"><li>方法 1:使用&nbsp;systemctl&nbsp;重启&nbsp;Networking&nbsp;服务</li><li>方法 2:使用&nbsp;systemctl&nbsp;重启&nbsp;NetworkManager&nbsp;服务</li><li>方法 3:使用&nbsp;nmcli&nbsp;重启网络连接</li><li>方法 4:使用&nbsp;ifdown&nbsp;和&nbsp;ifup&nbsp;重启特定网络接口</li><li>方法 5:重启系统</li><li>注意事项</li></ul></li></ul></div><p class="maodian"></p><h2>1、查看CPU</h2>
<div class="dxycode"><pre class="brush:bash;">cat /proc/cpuinfo</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="973" src="https://zhuji.jb51.net/uploads/allimg/20250620/2-250620120Q0255.png" width="1200" /></p>
<p>我这台机器CPU是2核,所以这里是2核</p>
<p>或者使用如下命令也可以查看</p>
<div class="dxycode"><pre class="brush:bash;">lscpu</pre></div>
<p>查看CPU使用率</p>
<div class="dxycode"><pre class="brush:bash;">top</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="431" src="https://zhuji.jb51.net/uploads/allimg/20250620/2-250620120Q0153.png" width="817" /></p>
<p class="maodian"></p><h2>2、查看内存</h2>
<p>查看内存信息:</p>
<div class="dxycode"><pre class="brush:bash;">free -h</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="145" src="https://zhuji.jb51.net/uploads/allimg/20250620/2-250620120Q0a5.png" width="611" /></p>
<p>查看内存使用情况:</p>
<div class="dxycode"><pre class="brush:bash;">vmstat</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="139" src="https://zhuji.jb51.net/uploads/allimg/20250620/2-250620120Q01E.png" width="630" /></p>
<p class="maodian"></p><h2>3、硬盘</h2>
<p>查看硬盘使用情况:</p>
<div class="dxycode"><pre class="brush:bash;">df -h</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="207" src="https://zhuji.jb51.net/uploads/allimg/20250620/2-250620120Q1V4.png" width="391" /></p>
<p>查看硬盘详细信息(可选):</p>
<div class="dxycode"><pre class="brush:bash;">sudo fdisk -l</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="334" src="https://zhuji.jb51.net/uploads/allimg/20250620/2-250620120Q1O4.png" width="525" /></p>
<p>查看磁盘I/O情况:</p>
<div class="dxycode"><pre class="brush:bash;">iostat</pre></div>
<p>(需要先安装sysstat:<code>sudo apt-get install sysstat</code>)</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="500" src="https://zhuji.jb51.net/uploads/allimg/20250620/2-250620120Q2157.png" width="603" /></p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="529" src="https://zhuji.jb51.net/uploads/allimg/20250620/2-250620120Q2412.png" width="785" /></p>
<p class="maodian"></p><h2>4、其它材料</h2>
<p class="maodian"></p><h3>4.1、查看CPU总使用率</h3>
<p>如果需要获取CPU使用率,使用mpstat命令,它是sysstat包的一部分。首先,确保安装了sysstat包。如果没有安装,可以通过以下命令安装:</p>
<div class="dxycode"><pre class="brush:bash;">sudo apt-get update
sudo apt-get install sysstat</pre></div>
<p>然后,运行mpstat来查看CPU使用率:</p>
<div class="dxycode"><pre class="brush:bash;">mpstat1</pre></div>
<p>这个命令会每1秒更新一次CPU使用率的信息。可以看到每个CPU的使用详情,包括用户模式(%usr)、系统模式(%sys)等的使用率。</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="700" src="https://zhuji.jb51.net/uploads/allimg/20250620/2-250620120Q2c1.png" width="733" /></p>
<p class="maodian"></p><h3>4.2、以下命令也可以查看主机的基本配置信息</h3>
<div class="dxycode"><pre class="brush:bash;">sudo lshw</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="811" src="https://zhuji.jb51.net/uploads/allimg/20250620/2-250620120Q2151.png" width="759" /></p>
<p class="maodian"></p><h3>4.3、输入以下命令也可以查看内存信息</h3>
<div class="dxycode"><pre class="brush:bash;">cat /proc/meminfo</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="276" src="https://zhuji.jb51.net/uploads/allimg/20250620/2-250620120Q3912.png" width="434" /></p>
<p class="maodian"></p><h3>4.4、查看指定目录下磁盘的容量</h3>
<p>df -h只是查看系统中文件的使用情况,这里补充一下</p>
<ul><li>du -h --max-depth=1 /root/* 查看目录下的所有文件大小</li><li>du -h --max-depth=1 /root 列出root目录下面所有的一级目录文件大小;</li></ul>
<p>怎么找到机器上的大文件在哪些目录下呢</p>
<p>先进入/根目录,然后执行</p>
<div class="dxycode"><pre class="brush:bash;">sudo du -h --max-depth=1</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="631" src="https://zhuji.jb51.net/uploads/allimg/20250620/2-250620120Q3133.png" width="601" /></p>
<p>然后进入home目录,然后再执行</p>
<div class="dxycode"><pre class="brush:bash;">sudo du -h --max-depth=2</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="457" src="https://zhuji.jb51.net/uploads/allimg/20250620/2-250620120Q3542.png" width="486" /></p>
<p>进入./lighthouse/hmblogs,然后使用ll命令查看即可</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="381" src="https://zhuji.jb51.net/uploads/allimg/20250620/2-250620120Q3H3.png" width="642" /></p>
<p>发现nohup.out文件过大</p>
<p>我们通常使用ll来查看文件列表以及文件大小<br />但是默认单位是B,看起来很不方便<br />可以加入参数 --block-size=M/G来修改单位<br />ll --block-size=K #以Kb为单位显示大小<br />ll --block-size=M #以Mb为单位显示大小<br />ll --block-size=G #以Gb为单位显示大小</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="494" src="https://zhuji.jb51.net/uploads/allimg/20250620/2-250620120Q4629.png" width="670" /></p>
<p class="maodian"></p><h2>ubuntu重启网络服务的方法小结</h2>
<p class="maodian"></p><h3>方法 1:使用&nbsp;systemctl&nbsp;重启&nbsp;Networking&nbsp;服务</h3>
<p>如果你使用的是传统的&nbsp;<code>networking</code>&nbsp;服务,可以通过以下命令重启:</p>
<p>重启网络服务:</p>
<div class="dxycode"><pre class="brush:bash;">sudo systemctl restart networking</pre></div>
<p>检查网络状态:</p>
<div class="dxycode"><pre class="brush:bash;">sudo systemctl status networking</pre></div>
<p class="maodian"></p><h3>方法 2:使用&nbsp;systemctl&nbsp;重启&nbsp;NetworkManager&nbsp;服务</h3>
<p>如果你使用的是&nbsp;<code>NetworkManager</code>(现代 Ubuntu 版本的默认网络管理工具),可以通过以下命令重启:</p>
<p>重启&nbsp;<code>NetworkManager</code>&nbsp;服务:</p>
<div class="dxycode"><pre class="brush:bash;">sudo systemctl restart NetworkManager</pre></div>
<p>检查&nbsp;<code>NetworkManager</code>&nbsp;状态:</p>
<div class="dxycode"><pre class="brush:bash;">sudo systemctl status NetworkManager</pre></div>
<p class="maodian"></p><h3>方法 3:使用&nbsp;nmcli&nbsp;重启网络连接</h3>
<p>如果你使用的是&nbsp;<code>NetworkManager</code>,可以通过&nbsp;<code>nmcli</code>&nbsp;工具直接重启网络连接。</p>
<p>查看当前网络连接名称:</p>
<div class="dxycode"><pre class="brush:bash;">nmcli connection show</pre></div>
<p>重启指定网络连接(将&nbsp;<code>CONNECTION_NAME</code>&nbsp;替换为实际的连接名称):</p>
<div class="dxycode"><pre class="brush:bash;">nmcli connection down CONNECTION_NAME
nmcli connection up CONNECTION_NAME</pre></div>
<p class="maodian"></p><h3>方法 4:使用&nbsp;ifdown&nbsp;和&nbsp;ifup&nbsp;重启特定网络接口</h3>
<p>如果你知道需要重启的网络接口名称(如&nbsp;<code>eth0</code>&nbsp;或&nbsp;<code>ens33</code>),可以使用以下命令:</p>
<p>关闭网络接口:</p>
<div class="dxycode"><pre class="brush:bash;">sudo ifdown &lt;接口名称&gt;</pre></div>
<p>启动网络接口:</p>
<div class="dxycode"><pre class="brush:bash;">sudo ifup &lt;接口名称&gt;</pre></div>
<p>例如:</p>
<div class="dxycode"><pre class="brush:bash;">sudo ifdown eth0
sudo ifup eth0</pre></div>
<p class="maodian"></p><h3>方法 5:重启系统</h3>
<p>如果以上方法都无法解决问题,可以尝试直接重启系统:</p>
<div class="dxycode"><pre class="brush:bash;">sudo reboot</pre></div>
<p class="maodian"></p><h3>注意事项</h3>
<ul><li>如果你使用的是云服务器(如 AWS、Azure、Google Cloud),重启网络服务可能会导致 SSH 连接断开。建议通过控制台访问服务器。</li><li>确保你有管理员权限(使用&nbsp;<code>sudo</code>)。</li></ul>
<p>以上就是ubuntu查看CPU、内存、硬盘的详细内容,更多相关资料请阅读琼殿技术社区其它文章!</p>
頁: [1]
查看完整版本: ubuntu查看CPU、内存、硬盘的命令详解