Linux日常运维1-3

Linux日常运维1-3

Linux日常运维1

1.1、w查看当前系统的负载
[root@antong ~]# w
 17:03:19 up 1 day,  5:35,  3 users,  load average: 0.12, 0.42, 0.45
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    106.125.147.37   16:34   28:37   0.03s  0.03s -bash
root     pts/2    106.125.147.37   16:54    8:31   0.02s  0.02s -bash
root     pts/3    106.125.147.37   17:03    3.00s  0.03s  0.00s w

第一行从左面开始显示的信息依次为:时间,系统运行时间,登录用户数,平均负载。第二行开始以及下面所有的行,告诉我们的信息是,当前登录的都有哪些用户,以及他们是从哪里登录的等等。

重要:第一行中的 ‘load average:’ 后面的三个数值。第一个数值表示1分钟内系统的平均负载值;第二个数值表示5分钟内系统的平均负载值;第三个数值表示15分钟系统的平均负载值。

1.2、查看cpu个数
[root@antong ~]# cat /proc/cpuinfo |head -n20
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 85
model name      : Intel(R) Xeon(R) Platinum 8255C CPU @ 2.50GHz
stepping        : 5
microcode       : 0x1
cpu MHz         : 2494.140
cache size      : 36608 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 syscall nx pdpe1gb rdtscp 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 invpcid_single fsgsbase bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 arat avx512_vnni
1.3、使用 vmstat 监控系统的状态

通过 vmstat 就可以知道具体是哪里有压力,通过参数可以持续查看系统状态

[root@antong ~]# vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 5  0      0 106788   6740 185180    0    0    11    38   79  362  8  3 88  0  0
 [root@antong ~]# vmstat 1 5
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 5  0      0  97540   7656 188316    0    0    11    38  104  411  8  3 88  0  0
 0  0      0  97556   7664 188316    0    0     0   172 5741 11368 12  3 84  1  0
 1  0      0  97556   7664 188316    0    0     0     0 5838 11570  9  2 88  0  0
 1  0      0  97432   7664 188316    0    0     0     0 5847 11598 11  2 87  0  0
 1  0      0  97008   7664 188316    0    0     0    20 5862 11636 11  2 87  0  0

procs显示进程相关信息:

r :表示运行和等待cpu时间片的进程数,如果长期大于服务器cpu的个数,则说明cpu不够用了

b :表示等待资源的进程数,比如等待I/O, 内存等,这列的值如果长时间大于1,则需要关注一下了

memory 内存相关信息:

swpd 表示切换到交换分区中的内存数量

free 当前空闲的内存数量

buff 缓冲大小,(即将写入磁盘的)

cache :缓存大小,(从磁盘中读取的)

swap 内存交换情况:

si :由交换区写入到内存的数据量

so :由内存写入到交换区的数据量

io 磁盘使用情况

bi :从块设备读取数据的量(读磁盘)

bo: 从块设备写入数据的量(写磁盘)

system 显示采集间隔内发生的中断次数

in :表示在某一时间间隔中观测到的每秒设备中断数

cs :表示每秒产生的上下文切换次数

CPU 显示cpu的使用状态

us :显示了用户下所花费 cpu 时间的百分比

sy :显示系统花费cpu时间百分比

id :表示cpu处于空闲状态的时间百分比

wa :表示I/O等待所占用cpu时间百分比

st :表示被偷走的cpu所占百分比(一般都为0,不用关注)

1.4、top实时显示占用资源

top -bn1 它表示非动态打印系统资源使用情况

和 top 命令唯一的区别就是,它一次性全部把所有信息输出出来而非动态显示。

[root@antong ~]# top -bn1
top - 17:17:33 up 1 day,  5:49,  4 users,  load average: 0.82, 0.38, 0.38
Tasks: 100 total,   2 running,  98 sleeping,   0 stopped,   0 zombie
%Cpu(s): 11.4 us,  4.2 sy,  0.0 ni, 84.4 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1881996 total,    97800 free,  1587044 used,   197152 buff/cache
KiB Swap:        0 total,        0 free,        0 used.   142940 avail Mem 
  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND              
14716 root      20   0 4073084   1.3g   8396 S 14.7 73.7 138:22.55 java                   
 1648 root      20   0 1034076  29248   8980 S  0.3  1.6   7:38.06 YDService             
 1756 root      20   0  741548  11704   2040 S  0.3  0.6   5:04.80 barad_agent           
    1 root      20   0   44648   3484   2172 S  0.0  0.2   0:13.94 systemd               
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.02 kthreadd               
    4 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H           
    6 root      20   0       0      0      0 S  0.0  0.0   0:05.69 ksoftirqd/0           
    7 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0           
    8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh                 
    9 root      20   0       0      0      0 S  0.0  0.0   0:11.57 rcu_sched             
   10 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 lru-add-drain         
   11 root      rt   0       0      0      0 S  0.0  0.0   0:00.26 watchdog/0             
   13 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs             
   14 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 netns                 
   15 root      20   0       0      0      0 S  0.0  0.0   0:00.02 khungtaskd             
   16 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 writeback             
   17 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kintegrityd           
   18 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset                 
   19 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset                 
   20 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset                 
   21 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kblockd               
   22 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 md                     
   23 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 edac-poller           
   24 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 watchdogd             
   30 root      20   0       0      0      0 S  0.0  0.0   0:00.54 kswapd0               
   31 root      25   5       0      0      0 S  0.0  0.0   0:00.00 ksmd                  
   32 root      39  19       0      0      0 S  0.0  0.0   0:00.24 khugepaged            
   33 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 crypto                 
1.5、free查看内存使用状况

total为总计内存 used为已经使用free为可以使用

free -m 以M为单位显示

free -h使用比较频繁,更加明了

[root@antong ~]# free 
              total        used        free      shared  buff/cache   available
Mem:        1881996     1586260       97416         692      198320      143732
Swap:             0           0           0
[root@antong ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1837        1549          94           0         194         140
Swap:             0           0           0
[root@antong ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        1.5G         94M        692K        194M        140M
Swap:            0B          0B          0B
1.6、ps 查看系统进程

这个ps命令是在工作中用的非常多的命令之一。

关于ps命令的使用,经常会连同管道符一起使用,用来查看某个进程或者它的数量

[root@antong ~]# ps aux | grep java
root     14716 12.4 73.7 4073084 1387936 pts/1 Sl+  Aug25 140:20 java -jar -Xmx1800M -Xms1024M spigot-1.16.5.jar
root     26965  0.0  0.0 115928  1016 pts/4    R+   17:29   0:00 grep --color=auto java
1.7、netstat 查看网络状况

netstat命令用来打印网络连接状况、系统所开放端口、路由表等信息。最常用的关于netstat的命令就是这个 netstat -lntp

[root@antong ~]# netstat -lntp 
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1238/sshd           
tcp6       0      0 :::25565                :::*                    LISTEN      14716/java   

Linux日常运维2

2.1、重启某一个网卡

ifdown 即停掉网卡,ifup即启动网卡。如果我们远程登录服务器,当使用ifdown eth0这个命令的时候,很有可能后面的命令ifup eth0不会被运行,这样导致我们断网而无法连接服务器,所以请尽量使用 service network restart 这个命令来重启网卡。

[root@antong ~]# ifdown eth0; ifup eth0
2.2、给一个网卡设定多个IP
[root@antong ~]# cd /etc/sysconfig/network-scripts/
[root@antong ~]# cp ifcfg-eth0 ifcfg-eth0\:1
[root@antong ~]# vim ifcfg-eth0\:1
修改DEVICE=eth0:1和IPADDR即可
2.3、更改主机名

默认主机名为localhost,使用hostname可以查看主机名

[root@antong ~]#  hostname
antong

使用命令修改主机名

[root@antong ~]# hostnamectl set-hostname localhost
[root@antong ~]# su
[root@localhost ~]#
2.4、设置DNS
[root@antong ~]# cat /etc/resolv.conf    
; generated by /usr/sbin/dhclient-script
nameserver 183.60.83.19
nameserver 183.60.82.98

手动添加临时解析域名

[root@antong ~]# vim /etc/hosts
127.0.0.1  localhost  localhost.localdomain
::1  localhost.localdomain   localhost
10.10.10.10 master root
2.5、SeLinux

SeLinux是Redhat/CentOS系统特有的安全机制。不过因为这个东西限制太多,配置也特别繁琐所以几乎没有人去真正应用它。所以装完系统,我们一般都要把seLinux关闭,以免引起不必要的麻烦。

[root@antong ~]# cat /etc/selinux/config 
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@antong ~]# getenforce 
Disabled

Linux日常运维3

3.1、crontab工具

crontab时间计算网站,可以计算出表达式的执行时间

要使用crontab工具来定时处理脚本或任务

-u :指定某个用户,不加-u选项则为当前用户;

-e :制定计划任务;

-l :列出计划任务;

-r :删除计划任务。(谨慎使用)

[root@antong ~]#  crontab  -e
no crontab for root -using an empty one
* * * * * echo `data` > /root/cron.log  (每分钟执行一次)

从左到右,依次为:分,时,日,月,周,命令行。而上面的例子的含义是:每分钟执行命令 echo data >> /root/cron.log

posted @ 2021-08-26 18:43  殇黯瞳  阅读(135)  评论(0编辑  收藏  举报