欢迎来到我的博客小站。  交流请加我微信好友: studyjava。  也欢迎关注公众号:Java学习之道 Fork me on GitHub

玩转Linux服务器常用命令

查看服务器的一些基本信息:

linux基础命令:http://cnblogs.com/mmzs/p/8406208.html

Linux某些命令找不到/无法使用:http://www.cnblogs.com/mmzs/p/9043190.html

Linux之定时任务Crond使用:http://cnblogs.com/mmzs/p/8178487.html

Linux命令-基本操作和软件安装:http://cnblogs.com/mmzs/p/8183935.html

显示安装的系统版本

[root@VM_0_5_centos ~]# cat /etc/redhat-release 
[root@VM_0_5_centos ~]# cat /proc/version
[root@VM_0_5_centos ~]# uname -r

显示系统是多少位的(32/64bit)

getconf LONG_BIT 

查看所有安装的软件包

//查看所有安装的软件包
[root@VM_0_5_centos ~]# rpm -qa 
libX11-devel-1.6.5-1.el7.x86_64
xmlrpc-c-client-1.32.5-1905.svn2451.el7.x86_64
setup-2.8.71-7.el7.noarch
gtk+-1.2.10-77.el7.x86_64
dhcp-libs-4.2.5-58.el7.centos.x86_64
langtable-data-0.0.31-3.el7.noarch
libXfixes-devel-5.0.3-1.el7.x86_64

查看配置的环境变量

//查看配置的环境变量
[root@VM_0_5_centos ~]# env

查看当前用户的计划任务服务

// 查看当前用户的计划任务服务
[root@VM_0_5_centos ~]# crontab -l 

查看所有网络接口的属性,包括IP地址

// 查看所有网络接口的属性,包括IP地址
[root@VM_0_5_centos ~]# ifconfig

防火墙

//centos7的防火墙相关:
firewall-cmd --state (防火墙状态)
systemctl list-unit-files|grep firewalld.service(防火墙状态)
systemctl restart firewalld.service(关闭防火墙)
systemctl disable firewalld.service(禁止开机自动启动)
//centos7以下的防火墙相关:
service iptables stop(关闭防火墙centos7以下)
chkconfig iptables off(禁止开机自动启动)

内存/硬盘大小 

//内存大小
[root@VM_0_5_centos ~]# cat /proc/meminfo |grep MemTotal
MemTotal:        1883616 kB
//硬盘大小
[root@VM_0_5_centos ~]# fdisk -l |grep Disk
Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 sectors
Disk label type: dos
Disk identifier: 0x000c5e30

查看系统内存情况

[root@VM_0_5_centos ~]# free
              total        used        free      shared  buff/cache   available
Mem:        1883616      875260       67028         388      941328      574040
Swap:             0           0           0
//total:总计物理内存的大小
//used:已使用多大
//free:可用有多少
//Shared:多个进程共享的内存总额
//Buffers/cached:磁盘缓存的大小
//available:可用物理内存大小
//查看内存总量
grep MemTotal /proc/meminfo 
//查看空闲内存量
grep MemFree /proc/meminfo 

CPU大小

//查看CPU相关参数
[root@VM_0_5_centos ~]# cat /proc/cpuinfo

[root@VM_0_5_centos ~]# cat /proc/cpuinfo |grep "model name" && cat /proc/cpuinfo |grep "physical id"
model name    : Intel(R) Xeon(R) CPU E5-26xx v4
physical id    : 0
//说明:Linux下可以在/proc/cpuinfo中看到每个cpu的详细信息。但是对于双核的cpu,在cpuinfo中会看到两个cpu。常常会让人误以为是两个单核的cpu。
//其实应该通过Physical Processor ID来区分单核和双核。而Physical Processor ID可以从cpuinfo或者dmesg中找到. flags 如果有 ht 说明支持超线程技术 判断物理CPU的个数可以查看physical id 的值,相同则为同一CPU

查看分区使用情况

// 查看所有分区
[root@VM_0_5_centos ~]# fdisk -l

Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000c5e30

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 104857566 52427759+ 83 Linux
//查看各分区使用情况
[root@VM_0_5_centos ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        50G   15G   33G  32% /
devtmpfs        911M     0  911M   0% /dev
tmpfs           920M   24K  920M   1% /dev/shm
tmpfs           920M  364K  920M   1% /run
tmpfs           920M     0  920M   0% /sys/fs/cgroup
tmpfs           184M     0  184M   0% /run/user/0

查看指定目录的大小

//查看指定目录的大小,不指定时默认是当前目录
[root@VM_0_5_centos ~]# du -sh /software/
2.1G    /software/

查看用户信息

[root@VM_0_5_centos ~]# w 
// 查看活动用户
[root@VM_0_5_centos ~]# id 
// 查看指定用户信息
[root@VM_0_5_centos ~]# last 
// 查看用户登录日志
[root@VM_0_5_centos ~]# cut -d: -f1 /etc/passwd
 // 查看系统所有用户
[root@VM_0_5_centos ~]# cut -d: -f1 /etc/group
// 查看系统所有组

查看CPU信息的linux系统信息命令

//查看CPU信息的linux系统信息命令
[root@VM_0_5_centos ~]# cat /proc/cpuinfo 
processor    : 0
vendor_id    : GenuineIntel
cpu family    : 6
model        : 79
model name    : Intel(R) Xeon(R) CPU E5-26xx v4
stepping    : 1
microcode    : 0x1
cpu MHz        : 2394.446
cache size    : 4096 KB
physical id    : 0
siblings    : 1
core id        : 0
cpu cores    : 1
apicid        : 0
initial apicid    : 0
fpu        : yes
fpu_exception    : yes
cpuid level    : 13
wp        : yes
flags        : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch bmi1 avx2 bmi2 rdseed adx xsaveopt
bogomips    : 4788.89
clflush size    : 64
cache_alignment    : 64
address sizes    : 40 bits physical, 48 bits virtual
power management:

查看系统运行/启动时间

//最清晰的查看方式
[root@VM_0_5_centos ~]# cat /proc/uptime| awk -F. '{run_days=$1 / 86400;run_hour=($1 % 86400)/3600;run_minute=($1 % 3600)/60;run_second=$1 % 60;printf("系统已运行:%d天%d时%d分%d秒",run_days,run_hour,run_minute,run_second)}'
系统已运行:0天0时6分7秒

//who -b 查看最后一次系统启动的时间。
//who -r 查看当前系统运行时间
[root@VM_0_5_centos ~]# who -b
         system boot  2018-06-28 16:52 //系统上次启动时间
[root@VM_0_5_centos ~]# who -r
         run-level 3  2018-06-28 16:52 

//last reboot可以看到Linux系统历史启动的时间。 重启一下操作系统后,然后
[root@VM_0_5_centos ~]# last reboot
reboot   system boot  3.10.0-693.el7.x Thu Jun 28 16:52 - 11:21 (3+18:29)   
reboot   system boot  3.10.0-693.el7.x Mon Jun 25 11:21 - 16:52 (3+05:30)   
reboot   system boot  3.10.0-693.el7.x Tue Jun 19 22:21 - 16:52 (8+18:31)   

//如果只需要查看最后一次Linux系统启动的时间
[root@VM_0_5_centos ~]# last reboot | head -1
reboot   system boot  3.10.0-693.el7.x Thu Jun 28 16:52 - 11:27 (3+18:35) 

// w命令查看,up后表示系统到目前运行了多久时间,反过来推算系统重启时间
[root@VM_0_5_centos ~]# w
 11:30:35 up 3 days, 18:38,  1 user,  load average: 78.52, 73.84, 72.60
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    222.211.233.166  11:18    3.00s  0.02s  0.00s w

//uptime 命令查看
[root@VM_0_5_centos ~]# uptime
 11:33:18 up 3 days, 18:40,  1 user,  load average: 71.31, 72.56, 72.30

//通过/proc/uptime查看
[root@VM_0_5_centos ~]# cat /proc/uptime
266.50 209.15//第一数字即是系统已运行的时间266.50秒
//运用系统工具date即可算出系统启动时间
[root@VM_0_5_centos ~]# date -d "$(awk -F. '{print $1}' /proc/uptime) second ago" +"%Y-%m-%d %H:%M:%S"
2018-06-28 16:52:24
[root@VM_0_5_centos ~]# cat /proc/uptime| awk -F. '{run_days=$1 / 86400;run_hour=($1 % 86400)/3600;run_minute=($1 % 3600)/60;run_second=$1 % 60;printf("系统已运行:%d天%d时%d分%d秒",run_days,run_hour,run_minute,run_second)}'
系统已运行:0天0时6分7秒

//top命令
[root@VM_0_5_centos ~]# top
top - 11:29:30 up 3 days, 18:37, 1 user, load average: 71.57, 72.68, 72.20
Tasks: 164 total, 70 running, 92 sleeping, 0 stopped, 2 zombie
%Cpu(s): 13.0 us, 86.7 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st
KiB Mem : 1883616 total, 75328 free, 804624 used, 1003664 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 642936 avail Mem
//top命令说明
第一行说明:
top – :系统当前时间
up:服务器连续运行的时间,笔者见过有服务器连续运行一年以上,linux服务器还是非常稳定的。
user:当前有多少用户登录系统
load average:这个边有3个数值分别表示系统在前1分钟,5分钟,15分钟的工作负载,一般单核负载在3-5之间比较合适,经常在1以下,说明cpu利用率不高,在5以上,cpu会处于较高负载状态,会容易宕机。
第二行就是显示任务的数量情况,其中zombie要注意一下,这个是表示僵尸进程,出现了僵尸进程要注意下僵尸进程是如何产生的。如果不找到产生原因,即使杀死了,可能也会再次出现。 第三行表示cpu的运行情况,按下1可以显示每个核的运行情况。 第四行表示内存memory的使用情况。 第五行表示交换空间swap的使用情况。 下面显示的就是进程的运行状态了。每个表头表示的含义如下: PID:进程编号 USER:进程所属用户 PR
/NI:Priority/Nice value进程执行的优先顺序 VIRT:Virtual Image (kb) 虚拟内存使用总额 RES:Resident size (kb) 常驻内存 SHR:Shared Mem size (kb) 共享内存 S:Process Status 进程状态 %CPU:cpu使用率 %MEM:内存使用率 TIME+:进程开始运行时使用cpu的总时间 COMMAND:进程运行的命令 在top状态下按f可以查看表头字段说明。

 更多命令

1、uname -a 
//查看内核/操作系统/CPU信息的linux系统信息命令
2、head -n 1 /etc/issue 
//查看操作系统版本,是数字1不是字母L
3、hostname 
//查看计算机名的linux系统信息命令
4、lsusb -tv 
//列出所有USB设备的linux系统信息命令
5、lspci -tv 
//列出所有PCI设备
6、lsmod 
//列出加载的内核模块
7、cat /proc/loadavg 
// 查看系统负载磁盘和分区
8、mount | column -t 
// 查看挂接的分区状态
9、swapon -s 
// 查看所有交换分区
10、hdparm -i /dev/hda 
// 查看磁盘参数(仅适用于IDE设备)
11、dmesg | grep IDE 
// 查看启动时IDE设备检测状况网络
12、iptables -L 
// 查看防火墙设置
13、route –n
 // 查看路由表
14、netstat -lntp 
// 查看所有监听端口
15、netstat -antp 
// 查看所有已经建立的连接
16、netstat -s 
// 查看网络统计信息进程
17、ps -ef 
// 查看所有进程
18、chkconfig –list
 // 列出所有系统服务
19、chkconfig –list | grep on 
// 列出所有启动的系统服务程序
20、cat /proc/partitions 
//查看linux硬盘和分区信息的系统信息命令
21、cat /proc/meminfo 
//查看linux系统内存信息的linux系统命令
22、cat /proc/ioports 
//查看设备io端口
23、cat /proc/interrupts 
//查看中断
24、cat /proc/pci 
//查看pci设备的信息

 

posted @ 2018-07-05 12:11  淼淼之森  阅读(15089)  评论(0编辑  收藏  举报
  👉转载请注明出处和署名