创建文件并挂载到swap 通过free命令查看当前内存使用情况:
1 2 3 4 5 $ free -m               total        used        free      shared  buff/cache   available Mem:           3790         122        3467           8         200        3435 Swap :          3071           0        3071
首先,需要使用dd命令创建一个固定大小的空文件:
1 2 3 4 5 $ dd if =/dev/zero of=test.file bs=1 M count=1024  1024 +0  records in 1024 +0  records out1073741824  bytes (1 .1  GB) copied, 0 .696093  s, 1 .5  GB/s
以上命令就创建了一个大小为1G的文件test.file。
注意到其中if=/dev/zero就是指定输入文件为/dev/zero,类似的还有/dev/null、/dev/random、/dev/urandom。
/dev/null,“空”设备,也有人称它为黑洞。任何输入到这个“设备”的数据都将被直接丢弃。最常用的用法是把不需要的输出重定向到这个文件。/dev/zero,“零”设备,可以无限的提供空字符(0x00,ASCII代码NUL)。常用来生成一个特定大小的文件。/dev/random,随机数设备,提供不间断的随机字节流。产生随机数据依赖系统中断,当系统中断不足时,/dev/random设备会“挂起”,因而产生数据速度较慢,但随机性好。/dev/urandom,随机数设备,提供不间断的随机字节流。不依赖系统中断,数据产生速度快,但随机性较低。 
然后,将这个文件转换为swap格式,并挂载:
1 2 3 4 $ mkswap test.file Setting up swapspace version 1 , size = 1048572  KiB no label, UUID =ddb65a66 -7f6b -4394-8014-8ad22a10a953  
1 2 3 4 $ swapon test.file swapon: /home /long /test.file : insecure  permissions  0664, 0600 suggested . swapon : /home /long /test.file : insecure  file  owner  1000, 0 (root ) suggested .
再次通过free命令查看当前内存使用情况:
1 2 3 4 5 $ free -m               total        used        free      shared  buff/cache   available Mem:           3790         122        2414           8        1252        3408 Swap :          4095           0        4095
可以看到swap空间增加了1G。
也许你想要将这个swap文件开机自动挂起:
1 2 3 4 $ vim /etc/fstab 在文件中添加一行: /home/long/test.file swap swap defaults 0  0  
重启之后可以看到:
1 2 3 4 5 6 $ free -m               total        used        free      shared  buff/cache   available Mem:           3790         123        3462           8         205        3433 Swap :          4095           0        4095
可以看到完成了开机自动挂载。
最后,可以相应的通过使用swapoff命令来进行卸载:
1 2 3 4 5 6 7 8 $ swapoff test.file $ free -m               total        used        free      shared  buff/cache   available Mem:           3790         117        3486           8         186        3448 Swap :          3071           0        3071
当然还要删除 /etc/fstab中的开机自动挂载的命令。
配置静态ip,重启后也能生效,系统可以访问公网 首先使用ip addr查询当前接口:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 $ ip addr 1 : lo: <LOOPBACK,UP,LOWER_UP> mtu 65536  qdisc noqueue state UNKNOWN group default qlen 1000     link/loopback 00 :00 :00 :00 :00 :00  brd 00 :00 :00 :00 :00 :00      inet 127 .0 .0 .1 /8  scope host lo        valid_lft forever preferred_lft forever     inet6 ::1 /128  scope host         valid_lft forever preferred_lft forever 2 : enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500  qdisc pfifo_fast state UNKNOWN group default qlen 1000     link/ether 08 :00 :27 :26 :e4:08  brd ff:ff:ff:ff:ff:ff     inet 192 .168 .31 .202 /24  brd 192 .168 .31 .255  scope global noprefixroute enp0s3        valid_lft forever preferred_lft forever     inet6 fe80::ac:cc04:a87b:74 ae/64  scope link noprefixroute         valid_lft forever preferred_lft forever 
可以看到网卡名为enp0s3,网卡的配置文件在/etc/sysconfig/network-script/目录下,
在其中可以看到当前的网卡配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 $ cat /etc/sysconfig/network-scripts/ifcfg-enp0s3 TYPE =EthernetPROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=dhcp 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=enp0s3 UUID=0 ee98256-fc86-4 fbf-aba5-8 c46e914fa89 DEVICE=enp0s3 ONBOOT=yes DNS1=8 .8 .8 .8  DNS2=9 .9 .9 .9  
将其中的BOOTPROTO修改为static,再加入静态ip的相关配置,如下:
1 2 3 4 BOOTPROTO=static IPADDR=192 .168 .31 .202  NETMASK=255 .255 .255 .0  GATEWAY=192 .168 .31 .254  
重启网络服务:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 $ systemctl restart network $ ip addr 1 : lo: <LOOPBACK,UP,LOWER_UP> mtu 65536  qdisc noqueue state UNKNOWN group default qlen 1000     link/loopback 00 :00 :00 :00 :00 :00  brd 00 :00 :00 :00 :00 :00      inet 127 .0 .0 .1 /8  scope host lo        valid_lft forever preferred_lft forever     inet6 ::1 /128  scope host         valid_lft forever preferred_lft forever 2 : enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500  qdisc pfifo_fast state UNKNOWN group default qlen 1000     link/ether 08 :00 :27 :26 :e4:08  brd ff:ff:ff:ff:ff:ff     inet 192 .168 .31 .202 /24  brd 192 .168 .31 .255  scope global noprefixroute enp0s3        valid_lft forever preferred_lft forever     inet6 fe80::ac:cc04:a87b:74 ae/64  scope link noprefixroute         valid_lft forever preferred_lft forever 
可以看到已经完成了静态ip的设置,测试连通性:
1 2 3 4 5 6 7 $ ping  www.baidu.com PING  www.a.shifen.com (14 .215 .177 .38 ) 56 (84 ) bytes of data.64  bytes from 14 .215 .177 .38  (14 .215 .177 .38 ): icmp_seq=1  ttl=53  time =7 .23  ms64  bytes from 14 .215 .177 .38  (14 .215 .177 .38 ): icmp_seq=2  ttl=53  time =6 .77  ms64  bytes from 14 .215 .177 .38  (14 .215 .177 .38 ): icmp_seq=3  ttl=53  time =7 .00  ms
可以看到网络连接正常,也就完成了静态ip的设置。
问题: 
在测试连通性的过程中,会发现主机(win 10)可以ping通虚拟机(centos7.5),但是虚拟机却无法ping通主机,
通过查询博客,发现问题主要是由于win10自带的防火墙造成的,可以通过以下方法来解决这个问题:
控制面板-系统和安全-Windows Defender防火墙-高级设置-入站规则-文件和打印机共享(回显请求 - ICMPv4-In) 
 
将两条上面名字的入站规则启用,虚拟机就可以向外ping通主机了。
手动释放掉系统的全部buffer/cache,使得内存的free增加 这里需要手动释放内存,主要涉及到的文件就是/proc/sys/vm/drop_caches,通过查询可以了解到,
“/proc/sys是一个虚拟文件系统,可以通过对它的读写操作做为与kernel实体间进行通信的一种手段”,
可以对它进行不同的修改来让内核释放内存:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Writing to this file causes the kernel to drop clean caches, dentries and inodes from memory, causing that memory to become free. To free pagecache, use          echo 1 > /proc/sys/vm/drop_caches; to free dentries and inodes, use     echo 2 > /proc/sys/vm/drop_caches; to free pagecache, dentries and inodes, use     echo 3 >/proc/sys/vm/drop_caches. Because this is a non-destructive operation and dirty objects  are not freeable, the user should run sync first. 
可以看到,这里建议在清除内存之间执行sync命令,它将”将有关文件系统的存储器常驻信息送入物理介质内”。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 $ free -h               total        used        free      shared  buff/cache   available Mem:           3.7G         115M         3.4G         8.5M         161M         3.4G  Swap :          3.0G           0B         3.0G $ sync  $ echo  1 > /proc /sys /vm /drop_caches  $ free  -h                total         used         free       shared   buff /cache    available  Mem :           3.7G         116M         3.5G         8.5M          89M         3.4G Swap :          3.0G           0B         3.0G 
可以看到buff/cache从161M变化到了89M。
使用systemd的管理方式定制一个开机启动服务 原理: 
systemctl的每一个服务对应一个服务配置文件,以.service结尾,所有有效的服务配置文件默认存放在两个地方,
/usr/lib/systemd/system/这个目录存放的是服务的真实配置文件,/etc/systemd/system/这个目录存放的是
开机自启动的服务,多数为软连接。
启动顺序:
当运行systemctl命令时,systemctl先去上面/etc/systemd/system目录中找目标service文件,找不到则
去/usr/lib/systemd/system/中找,还找不到就失败。
找到service后,会读取service文件并将@后面的参数传给服务。
开始监控这个服务,接管它的基本操作。
 
目标: 
建立一个开机启动服务,每次开机时打印当前时间到/tmp/hello。
首先,写好shell脚本:
1 2 3 4 5 $ cat my_hello.sh #!/bin/bash date  +"%Y-% m-%d %H:% M:%S --- hello systemd" >> /tmp/hello
然后,编写服务脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 $ cat my_hello.service [Unit] Description=systemd test [Service] Type =simpleExecStart=/home/long/my_hello.sh [Install] WantedBy=multi-user.target 
可以看到这里将Type 指定为simple,将my_hello.sh作为启动脚本。
更多的Type 可以参考CentOS 7之Systemd详解之服务单元设置system.service 。
将服务脚本复制到/usr/lib/systemd/system/目录下:
1 $ cp my_hello.service /usr/lib/systemd/system/ 
启动服务看是否能够成功:
1 2 3 4 5 $ systemctl start  my_hello $ cat /tmp/hello 2019 -07 -12  15 :23 :33  --- hello systemd
可以看到服务启动成功,下面将它设置为开机自启:
1 2 3 $ systemctl enable my_hello Created symlink from /etc/systemd/system/multi-user.target.wants/my_hello.service to /usr/lib/systemd/system/my_hello.service. 
可以看到它为服务在/etc/systemd/system目录下创建了软连接。
1 2 3 4 5 6 $ reboot $ cat /tmp/hello 2019 -07 -12  15 :23 :33  --- hello systemd2019 -07 -12  15 :35 :44  --- hello systemd
可以看到服务自启动成功。
参考 Linux中的虚拟设备/dev/null、/dev/zero、/dev/random和/dev/urandom 
VirtualBox虚拟机ping不通宿主机解决方案 
echo N>/proc/sys/vm/drop_caches清理缓存 
Linux sync命令的作用 
Linux sync命令的作用分析 
CentOS 7之Systemd详解之服务单元设置system.service