The linux disk management, network, time task

Disk Management

Check the disk space occupied

df  # 磁盘占有率
-h  # 以人类易读的方式展示

Linux下磁盘命名格式:/dev/sda
查看目录的占用空间

/dev/sd[a-z]num总大小 

linux下磁盘的命名格式如下图
size used avail user% mounted on
The total size Occupancy Surplus Occupancy rate Mount point on which to mount the directory

View space directory

du 命令

du /          # 查看根目录
du -sh .      # 当前
du -sh .      # 人类易读方式
du -s /*      # 查看根目录下每一个目录占用空间

dd

1.用来快速生成文件 2.测试磁盘的速度

dd if=/dev/zero of=file bs=size count=number
if = input file   表示从inputfile里读取内容
of = output file  表示将读出的数据写入到哪个文件
bs = byte size    大小,可以是K,M,G,T

dd if=/dev/zero of=b bs=100M count=10

RAID array card

raid0  # 把一份文件拆开两份分别写到两个盘上
 1.读,写速度有所提升
 2.可用空间N (磁盘个数)*每一块的大小
 3.没有容错能力
 4.最少磁盘,两个
raid1 # 把一份文件分别写到两个盘上
 1.读有所提升,写性能下降
 2.可用空间,所有磁盘中最小的一块大小
 3.有容错能力
 4.最少两块,2N
raid5 
# 最常见的RAID等级,把校验数据随机分布在所有磁盘上,当一个数据盘孙缓释,系统可以根据其他硬盘上数据和校验数据重建损坏数据.
 1.读写速度有提升
 2.可用空间(N-1)*大小
 3.有容错能力,最多可以坏一块硬盘
 4.最少要三块
raid6
# 如果两个磁盘同时发生故障,数据将无法恢复。 RAID6引入双重校验的概念,它可以保护阵列中同时出现两个磁盘失效时,阵列仍能够继续工作,不会发生数据丢失。RAID6不仅要支持数据的恢复,还要支持校验数据的恢复.
 1.读写速度有所提升
 2.可用空间(N-2)*大小
 3.有容错能力,最多可以坏两块
 4.最少4块
raid10
 # 各自先做两个raid1 然后联合做成raid0
 1.读写速度有提升
 2.可用空间N*大小/2
 3.有容错能力,每组镜像可以坏一块
 4.最少4块
raid01
 # 先实现raid0,在实现raid1
 1.读写性能有所提成
 2.可用空间N*大小/2
 3.有容错能力,只能坏同一组
 4.最少4块

The internet

Network Fundamentals

ipv4:4段 32位
网络位 + 主机位
网段
主机:通过网络访问外网的都叫主机

Machine may be used for A, B, C

A:

  • 8-bit network
  • Host bit 24
  • Network to the first bit is 0 can not change 1-126
    • 0 000001
    • 0 1111111
  • 127 special purposes, (loopback address)
  • Number of available network: 2 ^ 7 (128) 1 (loopback address)
  • Each segment available host: 2 ^ 24-2 (two unavailable)
  • Host segment unavailable:
    • Host bits are all 0 is unavailable. Represents a network segment
    • Host 1 bits are all unavailable, which represents the network broadcast address
  • Private Address: their own network can access the private address section 10
  • Subnet Mask: 255.0.0.0
  • Public Address: can be accessed as public address

B:

  • Network position: 16
  • Host bits: 16
  • The first 10-bit network can not change 128-191
    • 10 000000 00000000
    • 10 1111111 111111111
  • Available segment: 2 ^ 14
  • Each segment available host 2 ^ 16-2 (two unavailable)
  • Private Address: 172.16-172.31
  • Subnet Mask: 255.255.0.0.1

C:

  • Network 24 bits before, after eight bits for the host
  • First three immutable 110192-223
    • 110 00000 00000000 00000000
    • 110 111111 11111111 11111111
  • Available segment 21 ^ 2
  • Each segment number available machine 2 ^ 8-2 (two unavailable)
  • Private Address: 192.168.0-192.168.255
  • Subnet Mask: 255.255.255.0

D:

  • As a multicast address
  • Before four 1110 0000224--239-1
    • 1110 0000
    • 1110 1111

E: Use as a research 240-255

# 可以被机器使用的只有A,B,C三类 1-223可用

CIDR (Classless Inter-Domain Routing)

局域网内多个私有地址同一连接一个公网地址,这个公网地址负责和运营商接口连接,由运营商连接其他公网
1.解决主机远远小于ABC某一类可用主机问题,避免造成ip浪费
# 划分方式
网络位向主机位借位
# 示例
一家公司有2000台主机,申请B类地址则造成ip大大浪费
如B类地址的网络位有16位,向主机位去位.
16 + 5 = 21 位网络位
主机位剩余 11位 2^11-2 = 2046
这样就大大减小了ip浪费

# 子网掩码计算
255.255.11111000.0
子网掩码:255.255.248.0

# 最多网络位只能借到30位

# ip还可以用网络位表示类别
如127.0.0.1/8 此处8为网络位,可以判断为A类网

Subnet Mask

1.通过ip地址和子网掩码来确定网段
2.子网掩码不能单独存在,它必须结合IP地址一起使用。子网掩码只有一个作用,就是将某个IP地址划分成网络地址和主机地址两部分。

网络位全为1,主机位全为0

1567072061537

已知IP地址和子网掩码,用IP的二进制与子网掩码的二进制数据作'与'运算,就可以得到网段地址。

具体步骤如下:
1、把IP地址转换成二进制数:
199.32.59.64=11000111.100000.111011.1000000;
2、把子网掩码转换成二进制数:
255.255.255.224=11111111.11111111.11111111.11100000;
3、IP地址和子网掩码二进制数分别相“与”:
11000111.100000.111011.1000000 &11111111.11111111.11111111.11100000
=11000111.100000.111011.1000000;
4、将以上结果转换成十进制数:199.32.59.64,就是所求网段地址值
# 按位与的运算  &
# 只要有0就是0,全部为1,才是1
 1 & 0  0     
 2 % 3  2
 
# 按位或 |
# 只要有1就是1,全部为0才是0
 1 | 0  1
 2 | 3  3

# 异或 ^
# 相同为0,不同为1
 1 ^ 0   1
 2 ^ 3   1
 3 ^ 5   6

# 取反  ~
# 公式-(n+1)
~2  --> -3
~4  -->  3
# 左移 n*2的左移次方
2 <<  2  8
10 << 2  80
# 右移 n/2的右移次方(向下取整,不是四舍五入 )
12 >> 2  --> 3
13 >> 2  --> 3
14 >> 2  --> 3

Network Configuration

  • Manually
    • ip
    • Profiles (permanent)
  • automatic allocation
    • dhcp

ip

增加ip地址 ip a add # dev 增加到enss33
ip a add 192.168.21.23/24 dev ens33

# 删除ip地址 
ip a del
ip a del 192.168.21.23/24 dev ens33

# 增加IP地址起别名 lbel
ip a add 192.168.21.23/24 dev ens33 label ens33:0 # 别名

# 删除别名
ip a del 192.168.21.23/24 dev ens33 label ens33:0 # 别名

不写到文件中则重启无法重新生效

Network Configuration File

1.进入配置文件
vim /etc/sysconfig/network-scripts/ifcfg-网卡

TYPE="Ethernet"    # 网卡的接口类型, Ethernet 以太网
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="dhcp"    # 获取IP地址的方式,可以是dhcp,static(静态文件),none
动态主机设置协议(英语:Dynamic Host Configuration Protocol,DHCP)是一个局域网的网络协议,使用UDP协议工作,主要有两个用途:用于内部网或网络服务供应商自动分配IP地址;给用户用于内部网管理员作为对所有计算机作中央管理的手段。
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"    # 网卡名称
UUID="78416ae5-00d7-49d5-b346-a5782c761d38" # 设备唯一标识
DEVICE="ens33"  # 设备用到的文件
ONBOOT="yes"    # 开机是否启动

HWADDR=''               # MAC地址
# 手动设置
IPADDR=192.168.107.133  # ip地址,设置成功之后就只能连这个
NETMASK=255.255.255.0   # 子网掩码
GATEWAY=192.168.107.2   # 网关

2.# 重启网络
systemctl restart network

4.# 连接此ip是否能连接到
ping

# 配置dns   不需要重启 这里不配置会无法解析域名
vim /etc/resolv.conf
nameserver 192.168.107.2 # 网关

DNS configuration file

To translate domain names into IP addresses software called the Domain Name System, or DNS

  • /etc/resolv.conf
DNS,Domain Name System或者Domain Name Service(域名系统或者余名服务)。域名系统为Internet上的主机分配域名地址和IP地址。用户使用域名地址,该系统就会自动把域名地址转为IP地址。域名服务是运行域名系统的Internet工具。执行域名服务的服务器称之为DNS服务器,通过DNS服务器来应答域名服务的查询。

vim /etc/resolv.conf
nameserver 192.168.107.2
nameserver 8.8.8.8  # 谷歌
nameserver 114.114.114.114  # 电信
nameserver 202.106.114.1

Resolve

  • Local parsing the configuration file vim / etc / HOSTS (higher priority is resolved locally resolved dns)
  • dns resolution
  • A total of 13 sets the root node

CPU name

hostname      # 获取主机名
hostname name # 设置主机名,退出,临时生效

# 永久生效
1.配置文件
vim /etc/hostname          # centos7
vim /etc/sysconfig/network # centos6

2.hostnamectl set-hostname s22
# 永久生效,只能在centos7中使用

ss netstat

ss is Socket Statistics acronym. As the name suggests, ss command can be used to obtain statistical information socket, it can display and netstat similar content. Display more detailed information about the connection status of TCP and

ss command to print status information linux system, allowing network administrators to better understand

centos6:netstat
centos7:ss

-a # 所有
ss -a
-l # 监听中
ss -l

-t # tcp
ss -t

-u # udp
-x # unix socket文件
-p # 相关展示程序 
-n # 不显示服务名字,显示服务的端口号
ss -tln |grep 22

ss -tlnp |grep '22'

* 表示ipv4, 0.0.0.0 表示的当前主机上所有ip地址
: : ivpv6     地址
# 示例
LISTEN     0      128          *:22                       *:*                   users:(("sshd",pid=1154,fd=3))
LISTEN     0      128         :::22                      :::*                   users:(("sshd",pid=1154,fd=4))


常用组合 -anlp tnlp unlclp

wget

Network resources to download

-q # 静默模式
-c # 断点续传
-O # filename 另存为的名称
wget 地址 -O /opt/1.jpg

-P # 保存到指定的目录
wget 地址 -O /tmp

-r # 递归下载

-p # 下载所有的html文件
wget -p 地址

systemctl

Management Services service

start name    # 启动服务
stop name     # 关闭服务
reload name   # 重新加载配置文件(平滑重启)不踢掉当前用户
restart name  # 重启服务
status name   # 查看状态
systemctl status sshd

enabled name  # 开机自启
disabled name # 关闭开机自启动

# 查看服务状态是否开机自启动
systemctl list-unit-files |grep sshd

systemctl disabled sshd # 这个是关闭开机自启动不要试 

# centos6版本:
service + 动作 + 服务
service sshd start

chkconfig # 管理服务的开机启动
chkconfig --help
chkconfig add sshd  # 添加
chkconfig sshd on 启动

Scheduled Tasks (cron job)

文件启动计划任务
vim /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
22 * * * * * root touch a.txt     # 每天的每小时的第22分钟做的事
25 15 * * * * root touch c.txt  # 每天下午3:25创建
27 15,16,17 * * * root     # 每天的15,16,17点做的事
28 15-19 * * *  root touch # 15-19点做的事
30 10-20/5 * * * root touch /tmp/f.txt  # 每隔多久做的事,包括10
*/7 * * * * root touch /tmp/f.txt       # 每隔7分钟执行,每小时从0开始
37 * 10,20,30 * 4 root echo $(date) >. /tmp/g.txt 
# 每个月的10,20,30号 或者周四的那天每小时的37分钟执行的任务

建议:
1.分钟不要写成*
$? 查看任务执行状态

/etc/cron.d/0hourly
# 也是个定时任务,每个小时第一分钟执行/cron.d/0hourly

crontab command to start the implementation plan

-e               # 编辑用户的计划状态
crontab -e       # 进入文件
# 按配置文件照启动计划格式写进文件

-u               # 指定用户,如果不写,默认为当前用户

-l               # 列出当前用户的计划任务
crontab -l       
-r               # 删除用户的计划任务
crontab -r

cat /var/spool/cron/root
# 默认存在该文件下,不存在/etc/crontab.

Scheduled Tasks role

  • Regularly delete some logs
  • Backup
  • Time synchronization ntpdate time.windows.com

Guess you like

Origin www.cnblogs.com/taosiyu/p/11432970.html