21. Linux Kernel

Date:

2018-09-02

kernel是操作系统的核心,掌控着所有硬件设备的控制权。

linux系统是单内核,模块化体系。

21.1. uname的使用

1[root@centos6 ~]$ uname -a
2Linux centos6.zzjlogin 2.6.32-696.el6.x86_64 #1 SMP Tue Mar 21 19:29:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
3[root@centos6 ~]$ uname -r
42.6.32-696.el6.x86_64
5[root@centos6 ~]$ uname -n
6centos6.zzjlogin

21.2. 内核模块管理

21.2.1. 查看模块

# 方法1:
[root@centos6 ~]$ lsmod
Module                  Size  Used by
rfcomm                 71047  4
sco                    17493  2
bridge                 85706  0
bnep                   16370  2
l2cap                  54210  16 rfcomm,bnep
autofs4                27000  3
bnx2fc                 91541  0
#############省去下面的行#################

# 方法2
[root@centos6 ~]$ cat /proc/modules
rfcomm 71047 4 - Live 0xffffffffa0676000
sco 17493 2 - Live 0xffffffffa066c000
bridge 85706 0 - Live 0xffffffffa064e000
bnep 16370 2 - Live 0xffffffffa0646000

# 方法3
[root@centos6 ~]$ modinfo e1000
filename:       /lib/modules/2.6.32-696.el6.x86_64/kernel/drivers/net/e1000/e1000.ko
version:        7.3.21-k8-NAPI
license:        GPL

21.2.2. 装载模块

[root@centos6 ~]$ insmod /lib/modules/2.6.32-696.el6.x86_64/kernel/net/bluetooth/bluetooth.ko
[root@centos6 ~]$ modprobe  bluetooth

21.2.3. 卸载模块

[root@centos6 ~]$ rmmod /lib/modules/2.6.32-696.el6.x86_64/kernel/net/bluetooth/bluetooth.ko
[root@centos6 ~]$ modprobe -r bluetooth

21.3. 内核参数

修改内核参数

  1. 方法1

    临时修改,重启后失效

1[root@centos6 ~]$ echo "1" > /proc/sys/net/ipv4/ip_forward
  1. 方法2

1[root@centos6 ~]$ sysctl -w  net.ipv4.ip_forward=1
2net.ipv4.ip_forward = 1
  1. 方法3

    重启依然生效。

1[root@centos6 ~]$ vim /etc/sysctl.conf
2[root@centos6 ~]$ sysctl -p

21.4. 编译内核

编译内核的情况:
  • 有新的功能需求,如支持虚拟化

  • 原来的内核太过臃肿

  • 升级来提高稳定性

  • 个人定制或专业用途

  • 个人学习

编译内核的步骤
  1. 查看硬件设备信息

  2. 开发环境和源码

  3. 下载并解压源码

  4. 复制系统的默认的编译配置

  5. make menconfig

  6. make -j 4

  7. make modules_install

  8. make install

21.5. screen命令

screen可以让执行的命令脱离终端和终端无关。

选项:

-d

将指定的screen作业离线

-r

恢复指定的作业

-S

创建一个作业

-ls

查看作业

使用样例:

 1# 创建一个test名字的
 2[root@centos-155 ~]# screen -S test
 3[detached from 14681.test]
 4# 使用ctrl + a +d 来离线
 5
 6# 查看作业
 7[root@centos-155 ~]# screen -ls
 8There is a screen on:
 9    14681.test      (Detached)
101 Socket in /var/run/screen/S-root.
11# 恢复特定的作业
12[root@centos-155 ~]# screen -r test