第12章 模块Lilux

合集下载

Linux系统程序设计教程 第12章综合案例

Linux系统程序设计教程 第12章综合案例


客户端:与服务器建立连接后,从键盘获得数据并发送给服务器,然 后接收服务器发来的信息并显示到屏幕上。如果键盘输入bye,则断 开与服务器的连接。另外将建立连接、发送数据、接收数据、断开连 接过程的时间及相关信息写入日志文件(日志文件名在连接建立成功 后根据当前的年月日时分秒确定)。
5
12.1.3 项目实施
9
12.2.1 项目构思


பைடு நூலகம்
服务器端等待用户连接,等待客户端连接后创建 子进程,并在子进程中根据客户端输入的命令进 行响应,如“ls”命令能显示服务器端进程所在目 录文件,“get 文件名”命令将指定文件传输给 客户端,“put文件名”命令将从客户端接收文件, “exit”命令将断开与客户端的连接。 客户端与服务器端建立连接后,可以通过“ls”、 “get 文件名”、“put 文件名”、“exit”命令 与服务器端进行交互。

见教材。
6
12.1.4 项目运行

见教材。
7
12.2简易的文件传输系统



12.2.1 项目构思 12.2.2 项目设计 12.2.3 项目实施 12.2.4 项目运行
8
12.2.1 项目构思

简易的文件传输系统,完成多进程并发服务器与 客户端进行文件列表查看、上传、下载的简易功 能,类似FTP的上传和下载。
Linux程序设计教程
第12章 综合案例
第12章 综合案例

12.1 Linux网络传输系统项目

12.2简易的文件传输系统
2
12.1 Linux网络传输系统项目



12.1.1 项目构思 12.1.2 项目设计 12. 1.3 项目实施 12. 1.4 项目运行

《计算机操作系统实验指导》第十——十二章

《计算机操作系统实验指导》第十——十二章

1、添加一个类似ext2的文件系统myext2
(2)修改文件的内容 • 使用编辑器的替换功能,把 /lib/modules/$(uname -r)/build/include/linux/ myext2_fs.h,和/lib/modules/$(uname r)/build/include/asm-generic/bitops/下的 myext2-atomic.h与myext2-atomic-setbit.h文件 中的“ext2”、“EXT2”分别替换成“myext2”、 “MYEXT2”。 • 也可以使用vim进行修改,如图修改 myext2_fs.h文件。
编写一个简单的内核模块
4. 执行内核模块的装入命令 # sudo insmod helloworld.ko 可通过dmesg查看控制台输出,预期结果为<1> Hello World! 还可以使用lsmod命令查看,Ismod命令的作用是列出所有在内核中运行的模块的信息,包括 模块的名称、占用空间的大小、使用计数以及当前状态和依赖性。
5. 当不需要使用该模块时,卸载这个模块。 # sudo rmmod helloworld 可通过dmesg查看控制台输出,预期结果为<1>Goodbye!
利用内核模块实现/proc文件系统
• proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间。它以文件系 统的方式为访问系统内核数据的操作提供接口。用户和应用程序可以通过proc得到系统 的信息,并可以改变内核的某些参数。由于系统的信息,如进程,是动态改变的,所 以用户或应用程序读取proc文件时,proc文件系统是动态从系统内核读出所需信息并提 交的 。
第10章 内核模块
《计算机操作系统实验指导》

Red.Hat.Linux服务器配置与应用(第2版)第12章 网络数据库MySQL服务的配置与应用

Red.Hat.Linux服务器配置与应用(第2版)第12章 网络数据库MySQL服务的配置与应用

表的创建、复制、删除和修改
1.创建表 在MySQL中创建表,可使用以下格式的SQL语句。 CREATE TABLE 表名称(字段1,字段2,…字段 n,[表级约束]) [TYPE=表类型]; 其中: (1)字段i(i=1,2,…,n)的格式为: 字段名 字段类型 [字段约束]
表的创建、复制、删除和修改
用户的创建和删除
原来当首次安装MySQL时, MySQL安装程序在数据库mysql中设 置5个MySQL授权表(如表12-5所 示),由这5个授权表共同决定哪个用 户可以连接服务器、从哪里连接以及 连接后可以执行哪些操作。初始化 时,表host、tables_priv和 columnts_priv是空的,表user和db就 决定了MySQL默认的访问规则。下面 以MySQL管理员的身份查看一下表 user和db的内容。 授权表
表的创建、复制、删除和修改
【例】要在学生选课数据库中创建一个名为student的表 (存放学生的有关信息),可输入下面的命令。 mysql>create table student( ->sno varchar(7) not null, ->sname varchar(20) not null, ->ssex char(1) default 't', ->sbirthday date, ->sdepa char(20), ->primary key (sno) ->);
表的创建、复制、删除和修改
2.复制表 在MySQL 5中,可以使用下面的SQL语句来复制表结构。 CREATE TABLE 新表名称 LIKE 源表名称; 3.删除表 MySQL中删除一个或多个表的SQL语句格式为: DROP TBALE 表名称1[,表名称2,…]; 4.修改表 在创建表之后,如果修改表结构,例如添加、删除或者修改 表字段,创建或撤销索引,更改表的名称和类型等,则需 要使用ALTER语句来进行。ALTER语句的基本格式为: ALTER TABLE 表名称 更改动作1[,更改动作2,…];

第12章(75)教材配套课件

第12章(75)教材配套课件

VFS inode结构包装在一起,定义了一种新的容器结构
bdev_inode,如下:
struct bdev_inode {
struct block_device bdev;
22
12.1.3 块设备的逻辑表示
结构gendisk和hd_struct主要是给块设备驱动程序使用的, 所描述的是块设备的物理特征。每类块设备驱动程序都会在自 己的gendisk结构中注册操作集和请求队列。对驱动程序来说, 以扇区为单位操作块设备是方便而灵活的,因而gendisk和 hd_struct中的基本单位都是扇区。但对块设备的上层用户(如物 理文件系统)来说,扇区的尺寸过小,通常无法满足它们的需 求。事实上,物理文件系统通常以块(由若干连续的扇区组成) 为单位操作块设备。
由于块设备的上述特点,Linux定义了较为复杂的块设备 管理层来专门管理系统中的块设备。块设备管理层用结构 gendisk描述块设备的物理特征,用结构block_device描述块设 备的逻辑特征。前者所描述的设备可称为物理块设备,后者所 描述的设备可称为逻辑块设备。物理块设备的操作单位是扇区, 逻辑块设备的操作单位是块。
系统中所有物理块设备的gendisk结构被组织在Hash表 bdev_map[]中,Hash值是主设备号。数组bdev_map[]中的每个 指针指向一个probe结构,拥有同样Hash值的probe结构被串成 一个单向链表,如图12.2所示。结构probe的定义如下:
17
struct probe {
在近期的发展中,开发者们开始借助虚文件系统的管理机 制来管理Linux的其它子系统,如管道、消息队列、共享内存 等。这种纯粹在内核中使用、用户无法看到的虚文件系统可称 为伪文件系统,如pipefs、mqueue、shm等。

精品课件-Linux原理与结构-第12章

精品课件-Linux原理与结构-第12章
7
与普通文件一样,块设备特殊文件也可以被打开并被读写。向块 设备特殊文件的第i块写入的数据实际被写入到了块设备的第i块 中,从块设备特殊文件的第i块中读出的数据实际是块设备第i块 中的数据。
Linux的用户用块设备特殊文件的文件名来标识块设备, Linux内核却用设备号来标识块设备。设备号的类型为dev_t,由 两部分组成,其中的主设备号(major)标识的是块设备整体,如 整块磁盘,次设备号(minor)标识的是块设备中的分区。一个块 设备号可以唯一地标识一个块设备,正如一个PID可以唯一地标 识一个进程一样。
随着Linux的发展,人们逐渐认识到文件是一种通用的抽象 手段,文件系统是一种定义良好的操作接口。除了可以表示存储 在块设备中的真实实体之外,还可以用文件描述动态生成的信息, 如内核中各子系统的状态等。
2
ห้องสมุดไป่ตู้
这类动态生成的文件可以称为虚文件,用于管理虚文件的系统可 以称为虚文件系统。将虚文件系统插入到VFS框架之后,用户可 以用常规的文件操作接口查看、修改虚文件,进而查看内核的状 态、修改内核的参数等。为此Linux开发了多种不需要物理块设 备支持的虚文件系统,如proc、sysfs等,极大地提高了内核的 透明度和管理质量。
在近期的发展中,开发者们开始借助虚文件系统的管理机制 来管理Linux的其它子系统,如管道、消息队列、共享内存等。 这种纯粹在内核中使用、用户无法看到的虚文件系统可称为伪文 件系统,如pipefs、mqueue、shm等。
3
从虚拟文件系统的角度看,不管文件系统能否为用户所见, 不管其信息是否驻留在物理块设备之上,只要实现了VFS的下层 接口,它就是一个物理文件系统。
4
12.1 块 设 备 管 理 除了用于特殊目的的伪文件系统和虚文件系统之外,主流的 物理文件系统都建立在块设备之上。块设备与字符设备和网络设 备一道,构成了Linux的三大外部设备。

Linux操作系统基础与实训教程-第12章

Linux操作系统基础与实训教程-第12章
1.变量赋值 格式如下: <变量名>=<字符串> 功能:将等号右边的字符串赋给等号左边的变量。
《Linux操作系统基础与实训教程》
说明:“=”两边不能有空格,如果字符串含有空格,则在字符串外加上双引号
。 2.引用变量 格式如下: $<变量名>或者${<变量名>}
功能:引用变量的值。
12.2.1 用户变量
但是如果能像对待其它Linux命令那样,只输入脚本程序的名字就可以调用它
,在保存脚本程序的目录中输入“./脚本程序名”。
12.1.3 脚 本 调 测
《Linux操作系统基础与实训教程》
Shell脚本程序没有好的调测方法,因为它是解释型语言,常用方法是使用
“set -v”命令。通常在需要调测程序块前后添加调测标记,块前插入语句“set -x”,块后插入语句“set +x”,这样调测时就可以显示程序执行流程、变量的 值和参数当前值等信息。程序调测正确后,及时要去掉“set”语句。 如果shell程序是在太长,建议还是用其它语言来写,shell程序的语句最好 不要超过1000行。
[root@localhost ~]# echo $test [root@localhost ~]# sat=01-20 [root@localhost ~]# sun=${sat}urday [root@localhost ~]# echo \$sun
12.2.1 用户变量
输入函数:
《Linux操作系统基础与实训教程》
(2)把脚本文件设置为可执行。
《Linux操作系统基础与实训教程》
[root@localhost ~]# chmod +x first.sh (3)运行脚本。 有两种方法,比较简单的方式是调用Shell,并把脚本名当成一个参数,如下所

Red Hat Linux 9.0基础教程第2版课件chapter12


备份工具
特点
tar
通常用来进行文件的归档,
可用于磁盘和磁带等任何介质
dump
直接读取文件系统,通常用于 磁盘备份
优点
可用于归档文件中检索 但个文件
直接的文件系统访问可 不影响文件属性中 的时间戳,也更加 高效。
缺点
效率较低,不支持直接 的备份级
备份程序专用于特定的 文件系统类型,
dump命令只能识别 ext2文件系统
的知识。 • df的说明书页 — 键入man df来学习关于df和它的选项的知识。 • du 的说明书页 — 键入man du来学习关于du和它的选项的知
识。 • lspci的说明书页 — 键入man lspci来学习更多关于lspci命令
和它的许多选项的信息。 • /proc — /proc 目录的内容也可以用来收集更详细的系统信息。
12-11
12.7.4 简单备份
• 完全备份 、增量备份
• 备份级别:完全备份-0级备份,其他级别表示的是 自前一级别以来被修改的文件
• 备份策略
星期天0级备份 星期一1级备份 星期二1级备份 星期三1级备份 星期四1级备份 星期五1级备份 星期六1级备份
星期天0级备份 星期一1级备份 星期二2级备份 星期三3级备份 星期四4级备份 星期五5级备份 星期六6级备份
• 磁盘空间用量
– df – 系统监视器
• 监控文件系统
– diskcheck
12-5
12.4 获取硬件信息
• 硬件浏览器程序 • lspci
12-6
12.5 查看日志文件
• 定位日志文件
– /var/log – logrotate软件包
• /etc/logrotate.conf • /etc/logrotate.d

Red Hat Linux 9系统管理(第二版) 第12章 DNS服务器配置


DNS服务的一般知识 主域名服务器、辅助域名服务器 域委派及子域DNS服务器 DNS负载均衡 域名直接解析,DNS泛域名解析的配置方法 图形化配置DNS服务器的方法
2019年5月11日
第4页
Red Hat Linux 9系统管理(第二版)
清华大学出版社
12.1 DNS简介
通过DNS服务可以将易于记忆的域名和不 易记忆的IP地址进行转换,从而使得人们 能通过简单好记的域名来代替IP地址访问 网络
辅助域名服务器主要有提供容错能力、加快查询 速度和分担主域名服务器的负担等优点
2019年5月11日
第8页
Red Hat Linux 9系统管理(第二版)
清华大学出版社
12.7 配置域名服务器的负载均衡
域名服务器的负载均衡实现方法是在网络中为域 名服务器中的同一个域名配置多个IP地址,即为 一个主机名设置多条A资源记录,在应答DNS查 询时,域名服务器对每个查询将以域名服务器中 主机记录的IP地址按顺序返回不同的解析结果, 将客户端的访问引导到不同的计算机上去,使得 不同的客户端访问不同的服务器
RReedd HHaatt LLiinnuuxx 99系系统统管管理理((第第二二版版))
清清华华大大学学出出版版社社
第12章 DNS服务器配置
教学目标 教学重点 教学过程
2019年5月11日
第1页
Red Hat Linux 9系统管理(第二版)
教学目标
清华大学出版社
本章主要介绍DNS服务器的配置方法和管 理方式
根域名服务器指向文件:named.ca,路径为
/var/named/named.ca。它用于缓存服务器的初始
配置 2019年5月11日

NDG Linux Essentials - Chapter 12 - Network Configuration

12.1 IntroductionHaving access to the network is a key feature of most Linux systems. Users want to surf the net, send and receive email and transfer files with other users.Typically the programs that perform these functions (web browsers, email clients, etc.) are fairly easy to use. However, they all rely on an important feature: the ability of your computer to communicate with another computer. In order to have this communication, you need to know how to configure your system's network.Linux provides you with several tools to both configure your network as well as monitor how it is performing. In this chapter you will learn how to use both GUI-based tools as well as command line tools.12.2 Basic Network TerminologyBefore setting up a network or accessing an existing network, it is important to know some key terms that are related to networking. This section explores the terms you should be aware of. Some of the terms are basic and you may already be familiar with them, however others are more advanced.Host: A host is basically a computer. However, many people have a more limited idea of what a computer is (like a desktop computer or a laptop). In reality, many other devices are also computers, such as cell phones, digital music players and many modern televisions. In networking terms, a host is any device that communicates with another device.Network: A network is a collection of two or more hosts (computers) that are able to communicate with each other. This communication can be via a wired connection or wireless.Internet: The Internet is an example of a network. It consists of a publically accessible network that connects millions of hosts throughout the world. Many people use the Internet to surf web pages and send/receive email, but the Internet has many additional capabilities besides these activities.Wi-Fi: The term Wi-Fi refers to wireless networks.Server: A host that provides a service to another host or client is called a server. For example, a web server stores, processes and delivers web pages. An email server receives incoming mail and delivers outgoing mail.Service: A feature being provided from a host is a service. An example of a service would be when a host provides web pages to another host.Client: A client is a host that is accessing a server. When you are working on a computer surfing the Internet, you are considered to be on a client host.Router: Also called a gateway, a router is a machine that connects hosts from one network to another network. For example, if you work in an office environment, the computers within the company can all communicate via the local network created by the administrators. To access the Internet, the computers would have to communicate with a router that would be used to forward network communications to the Internet. Typically when you communicate on a large network (like the Internet), there are several routers that are used before your communication reaches its final destination.The following diagram provides a visual reference for several of the terms discussed above:12.3 Networking Features TerminologyIn addition to the networking terms discussed in the last section, there are some additional terms that you should be aware of. These terms focus more on the different types of networking services that are commonly used as well as some of the techniques that are used to communicate between machines.Network packet: A network packet is used to send network communication between hosts. By breaking down communication into smaller chunks (packets), the data delivery method is much more efficient.IP address: An Internet Protocol (IP) address is a unique number assigned to a host on a network. Hosts use these numbers to "address" network communication. More discussion on IP addresses will occur later in this chapter.Network mask: Also called a netmask or mask, a network mask is a number system that can be used to define which IP addresses are considered to be within a single network. Because of how routers perform their functions, networks have to be clearly defined.Hostname: Each host on a network could have its own hostname. This makes it easier for humans to address network packets to another host because names are easier for humans to remember than numbers. Hostnames are translated into IP addresses before the network packet is sent on the network.DHCP: Hosts can be assigned hostnames, IP addresses and other network-related information by a DHCP (Dynamic Host Configuration Protocol) server. In the world of computers, a protocol is a well-defined set of rules. DHCP defines how network information is assigned to client hosts and the DHCP server is the machine that provides this information. While setting up a DHCP server is beyond the scope of this chapter, you will see how to configure a DHCP client machine later in this chapter.DNS: As mentioned previously, hostnames are translated into IP addresses, prior to the network packet being sent on the network. This means that your host needs to know the IP address of all of the other hosts that you are communicating with. When working on a large network (like the Internet), this can pose a challenge as there are so many hosts. A DNS (Domain Name Server) server provides the service of translating domain names into IP addresses. While setting up a DNS server is beyond the scope of this chapter, you will see how to configure a DNS client machine later in this chapter.Ethernet: In a wired network environment, Ethernet is the most common way to physically connect the hosts into a network. Ethernet cables are connected to network cards that support Ethernet connections. Ethernet cables and devices (such as routers) are specifically designed to support different speeds of communications, the lowest being 10 Mbps (10 Megabits per second) and the highest being 100 Gbps (100 gigabits per second). The most common speeds are 100 Mbps and 1 Gbps.TCP/IP: The Transmission Control Protocol/Internet Protocol (TCP/IP) is a fancy name for a collection of protocols (remember, protocol = set of rules) that are used to define how network communication should take place between hosts. While it isn't the only collection of protocols used to define network communication, it is the most often utilized one. As an example, TCP/IP includes the definition of how IP addresses and network masks work.12.4 IP AddressesAs previously mentioned, hosts "address" network packets by using the IP address of the destination machine. The network packet also includes a "return address", the IP address of the sending machine.There are, in fact, two different types of IP addresses: IPv4 and IPv6. To understand why there are two different types, you need to understand a brief bit of IP addressing history.For many years, the IP addressing technique that was used by all computers was IPv4 (IP version 4). In an IPv4 address, a total of four 8-bit (8-bit = numbers from 0 to 255) numbers are used to define the address. For example: 192.168.10.120. Note, this is considered a 32-bit address (4 x 8-bit = 32).Each host on the Internet must have a unique IP address. In an IPv4 environment, there is a technical limit of about 4.3 billion IP addresses. However, many of these IP addresses are not really useable for various reasons. Also, IP addresses have been assigned to organizations that haven't fully made use of all of the IP addresses they had available.While it seems like there should be plenty of IP addresses to go around, various factors (the increasing number of hosts on the Internet, reserved private IP addresses, etc.) led to a problem: The Internet started running out of IP addresses.This, in part, encouraged the development of IPv6. IPv6 was officially "created" in 1998. In an IPv6 network the addresses are much larger, 128-bit addresses that look like this: 2001:0db8:85a3:0042:1000:8a2e:0370:7334. Essentially this provides for a much larger address pool, so large that running out of addresses any time in the near future is very unlikely.It is important to note the difference between IPv4 and IPv6 isn't just "more IP addresses". IPv6 has many other advanced features that address some of IPv4's limitations, including better speed, more advanced package management and more efficient data transportation.Considering all the advantages, you would think that by now all hosts would be using IPv6. This isn't the case at all. The majority of network-attached devices in the world still use IPv4 (something like 98-99% of all devices). So, why hasn't the world embraced the superior technology of IPv6?There are primarily two reasons:1. The invention of NAT: Invented to overcome the possibility of running out of IP addresses in an IPv4 environment, Net AddressTranslation (NAT) used a technique to provide more hosts access to the Internet. In a nutshell, a group of hosts are placed into a private network with no direct access to the Internet; a special router provides Internet access and only this one router needs an IP address to communicate on the Internet. In other words, a group of hosts share a single IP address, meaning a lot more computers can attach to the Internet. This feature means the need to move to IPv6 is less critical then before the invention of NAT.2. Porting issues: Porting is switching over from one technology to another. IPv6 has a lot of great new features, but all of thehosts need to be able to utilize these features. Getting everyone on the Internet (or even just some) to make these changes poses a challenge.Most experts agree that IPv6 will eventually replace IPv4, so understanding the basics of both is important for those who work in the IT industry.12.5 Configuring Network DevicesWhen you are configuring network devices, there are two initial questions that you need to ask:1. Wired or wireless? Configuring a wireless device will be slightly different than a wired device because of some of the additionalfeatures typically found on wireless devices (such as security).2. DHCP or static address? Recall that a DHCP server provides network information, such as your IP address and subnet mask.If you don't make use of a DHCP server, then you will need to manually provide this information to your host. This is called usinga static IP address.Generally speaking, a desktop machine will use wired network, while a laptop will use wireless. Normally a wired machine uses a static IP address, but these can also often be assigned via a DHCP server. In almost all cases, wireless machines use DHCP since they are almost always mobile and attached to different networks.12.5.1 Configuring the Network Using a GUIIf you have access to a GUI (Graphical User Interface) environment, you will likely also have access to a GUI-based tool that will allow you to configure your network. These tools vary from one distribution to another. The following examples were performed on a CentOS machine.To start the network configuration tool, click on System in the menu bar, then Preferences > and then Network Connections:The tool first lists all of the current network devices. In the example below, there is only a Wired device:The network device itself is eth0. Network devices are named eth0, eth1, etc. To modify this network device, click on the device name and then click the Edit button:Note that a full discussion of all network features is beyond the scope of this course. The focus on this section will be to change key network components.If you click on the IPv4 Settings tab, the following would be displayed:Recall that you can assign a static IP address or use a DHCP server (if one is available). This change can be made by clicking on the drop down list next to Method:If you choose Manual, then you will be able to change the current address by clicking in the area where the address is currently specified:Note that if you choose Automatic (DHCP), then the Addresses location is "grayed out":Important: If you switch from Automatic (DHCP) back to Manual, all of the previous data is "gone". By clicking the Cancel button and editing the eth0 device again, the data will reappear.Most GUI-based tools make changes take effect immediately after you save them. However, in some cases, you might need to either reboot the machine or run a command as the administrator to make the changes take effect. The following demonstrates the command that would need to be executed on a CentOS system:12.5.2 Configuring the Network Using Configuration FilesThere will be times when no GUI-based tool will be available. In those cases, it is helpful to know the configuration files that are used to store and modify network data.These files can vary depending on the distribution that you are working on. The following examples are provided for CENTOS systems.12.5.2.1 Primary IPv4 Configuration FileThe primary configuration file for an IPv4 network interface is the /etc/sysconfig/network-scripts/ifcfg-eth0 file. The following demonstrates what this file looks like when configured for a static IP address:root@localhost:~# cat /etc/sysconfig/network-scripts/ifcfg-eth0DEVICE="eth0"BOOTPROTO=noneNM_CONTROLLED="yes"ONBOOT=yesTYPE="Ethernet"UUID="98cf38bf-d91c-49b3-bb1b-f48ae7f2d3b5"DEFROUTE=yesIPV4 _FAILURE_FATAL=yesIPV6INOT=noNAME="System eth0"IPADDR=192.168.1.1PREFIX=24GATEWAY=192.168.1.1DNS1=192.168.1.2HWADDR=00:50:56:90:18:18LAST_CONNECT=1376319928root@localhost:~#If the device was configured to be a DHCP client, then the IPADDR, GATEWAY and DNS1 values would not be set. Additionally, the BOOTPROTO value would be set to dhcp.12.5.2.2 Primary IPv6 Configuration FileOn a CentOS system, the primary IPv6 configuration file is the same file where IPv4 configuration is stored: the/etc/sysconfig/network-scripts/ifcfg-eth0 file. If you want to have your system have a static IPv6 address, add the following to the configuration file:IPV6INIT=yesIPV6ADDR=<IPv6 IP Address>IPV6_DEFAULTGW=<IPv6 IP Gateway Address>If you want your system to be a DHCP IPv6 client, then add the following setting:DHCPV6C=yesYou also need to add the following setting to the /etc/sysconfig/network file:NETWORKING_IPV6=yes12.5.2.3 Domain Name Service (DNS)When a computer is asked to access a website, such as , it does not necessarily know what IP address to use. In order for the computer to associate an IP address with the URL or hostname request, the computer relies upon the DNS service of another computer. Often, the IP address of the DNS server is discovered during the DHCP request, while a computer is receiving important addressing information to communicate on the network.The address of the DNS server is stored in the /etc/resolv.conf file. A typical /etc/resolv.conf file is automatically generated and looks like the following:sysadmin@localhost:~$ cat /etc/resolv.confnameserver 127.0.0.1sysadmin@localhost:~$The nameserver setting is often set to the IP address of the DNS server. The following example uses the host command discussed later in this chapter. Note that the example server is associated with the IP address 192.168.1.2 by the DNS server:sysadmin@localhost:~$ host has address 192.168.1.2sysadmin@localhost:~$It is also common to have multiple nameserver settings, in the event that one DNS server isn't responding.12.5.2.4 Additional Network Configuration FilesThe following table describes additional network configuration files to be aware of. Although they are not specifically listed in the exam objectives, the objectives do include the general term Network configuration, so these files may in fact appear on the exam:Command Explanation/etc/hosts This file contains a table of hostnames to IP addresses. It can be used to supplement a DNS server./etc/sysconfig/network This file has two settings. The NETWORK setting can determine if networking is turned on (yes) or off (no). The HOSTNAME setting defines the local machine's hostname./etc/nsswitch.conf This file can be used to modify where hostname lookups occur. For example, the setting hosts:files dns would have hostname lookups occur in the /etc/hosts file first and then the DNSserver second. If switched to hosts: dns files, the DNS server would be searched first.12.5.2.5 Restarting the NetworkAfter changing a network configuration file (for example, the /etc/sysconfig/network-scripts/ifcfg-eth0 file or the/etc/resolv.conf file), you either need to reboot the machine or run a command as the administrator to make the changes take effect. The following example demonstrates the command that would need to be executed on a CentOS system:12.6 Network ToolsThere are several commands that you can use to view network information. These tools can also be useful when you are troubleshooting network issues.12.6.1 ifconfig CommandThe ifconfig command stands for "interface configuration" and is used to display network configuration information. Not all network settings are covered in this course, but it is important to note from the output below that the IP address of the primary network device (eth0) is 192.168.1.2 and that the device is currently active (UP):root@localhost:~# ifconfigeth0 Link encap:Ethernet HWaddr b6:84:ab:e9:8f:0ainet addr:192.168.1.2 Bcast:0.0.0.0 Mask:255.255.255.0inet6 addr: fe80::b484:abff:fee9:8f0a/64 Scope:LinkUP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:95 errors:0 dropped:4 overruns:0 frame:0TX packets:9 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000RX bytes:25306 (25.3 KB) TX bytes:690 (690.0 B)lo Link encap:Local Loopbackinet addr:127.0.0.1 Mask:255.0.0.0inet6 addr: ::1/128 Scope:HostUP LOOPBACK RUNNING MTU:65536 Metric:1RX packets:6 errors:0 dropped:0 overruns:0 frame:0TX packets:6 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:0RX bytes:460 (460.0 B) TX bytes:460 (460.0 B)root@localhost:~#The lo device is referred to as the loopback device. It is a special network device used by the system when sending network-based data to itself.The ifconfig command can also be used to temporarily modify network settings. Typically these changes should be permanent, so using the ifconfig command to make such changes is fairly rare.The ifconfig command is becoming obsolete in some Linux distributions (deprecated) and is being replaced with a form of the ip command, specifically ip addr show. Note that the same information highlighted above can also be found using this command:root@localhost:~# ip addr show1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWNlink/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope hostvalid_lft forever preferred_lft forever6476: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codelstate UP qlen 1000 link/ether b6:84:ab:e9:8f:0a brd ff:ff:ff:ff:ff:ffinet 192.168.1.2/24 scope global eth0valid_lft forever preferred_lft foreverinet6 fe80::b484:abff:fee9:8f0a/64 scope linkvalid_lft forever preferred_lft foreverroot@localhost:~#12.6.2 route CommandRecall that a router (or gateway) is a machine that will allow hosts from one network to communicate with another network. To view a table that describes where network packages are sent, use the route command:root@localhost:~# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.1.0 * 255.255.255.0 U 0 0 0 eth0default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0root@localhost:~#The first red box in the example above indicates that any network package sent to a machine in the 192.168.1 network is not sent to a gateway machine (the * indicates "no gateway"). The second red box indicates that all other network packets are sent to the host with the IP address of 192.168.1.1 (the router).Some users prefer to display this information with numeric data only, by using the -n option to the route command. For example, look at the following and focus on where the output used to display default:root@localhost:~# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth00.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0root@localhost:~#The 0.0.0.0 refers to "all other machines", or the same as "default".The route command is becoming obsolete in some Linux distributions (deprecated) and is being replaced with a form of the ip command, specifically ip route show. Note that the same information highlighted above can also be found using this command:root@localhost:~# ip route showdefault via 192.168.1.254 dev eth0 proto static192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.2root@localhost:~#12.6.3 ping CommandThe ping command can be used to determine if another machine is "reachable". If the ping command can send a network package to another machine and receive a response, then you should be able to connect to that machine.By default, the ping command will continue sending packages over and over. To limit how many pings to send, use the -c option. If the ping command is successful, you will see output like the following:root@localhost:~# ping -c 4 192.168.1.2PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.64 bytes from 192.168.1.2: icmp_req=1 ttl=64 time=0.051 ms64 bytes from 192.168.1.2: icmp_req=2 ttl=64 time=0.064 ms64 bytes from 192.168.1.2: icmp_req=3 ttl=64 time=0.050 ms64 bytes from 192.168.1.2: icmp_req=4 ttl=64 time=0.043 ms--- 192.168.1.2 ping statistics ---4 packets transmitted, 4 received, 0% packet loss, time 2999msrtt min/avg/max/mdev = 0.043/0.052/0.064/0.007 msroot@localhost:~#If the ping command fails, you will receive a message stating, Destination Host Unreachable:root@localhost:~# ping -c 4 192.168.1.1PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.From 192.168.1.2 icmp_seq=1 Destination Host UnreachableFrom 192.168.1.2 icmp_seq=2 Destination Host UnreachableFrom 192.168.1.2 icmp_seq=3 Destination Host UnreachableFrom 192.168.1.2 icmp_seq=4 Destination Host Unreachable--- 192.168.1.1 ping statistics ---4 packets transmitted, 0 received, +4 errors, 100% packet loss, time 2999mspipe 4root@localhost:~#It is important to note that just because the ping command fails does not mean that the remote system is really unreachable. Some administrators configure their machines to not respond to ping requests.This is because a server can be attacked by something called a denial of service attack. In this sort of attack, a server is overwhelmed by a massive number of network packets. By ignoring ping requests, the server is less vulnerable.As a result, the ping command may be useful for checking the availability of local machines, but not always for machines outside of your own network.12.6.4 netstat CommandThe netstat command is a powerful tool that provides a large amount of network information. It can be used to display information about network connections as well as display the routing table similar to the route command.For example, you may want to display statistics regarding network traffic. This can be accomplished by using the -i option to the netstat command:root@localhost:~# netstat -iKernel Interface tableIface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flgeth0 1500 0 137 0 4 0 12 0 0 0 BMRUlo 65536 0 18 0 0 0 18 0 0 0 LRUroot@localhost:~#The most important statistics from the output above are the TX-OK and TX-ERR. A high percentage of TX-ERR may indicate a problem on the network, such as too much network traffic.If you want to use the netstat command to display routing information, use the -r option:root@localhost:~# netstat -rKernel IP routing tableDestination Gateway Genmask Flags MSS Window irtt Iface192.168.1.0 * 255.255.255.0 U 0 0 0 eth0default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0root@localhost:~#The netstat command is also commonly used to display open ports. A port is a unique number that is associated with a service provided by a host. If the port is open, then the service is available for other hosts.For example, you can log into a host from another host using a service called SSH. The SSH service is assigned port #22. So, if port #22 is open, then the service is available to other hosts.It is important to note that the host also needs to have the services itself running; this means that the program that allows remote users to log in needs to be started (which it typically is, for most Linux distributions).To see a list of all currently open ports, you can use the following command:root@localhost:~# netstat -tlnActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address Statetcp 0 0 192.168.1.2:53 0.0.0.0:* LISTENtcp 0 0 127.0.0.1:53 0.0.0.0:* LISTENtcp 0 0 0.0.0.0:22 0.0.0.0:* LISTENtcp 0 0 127.0.0.1:953 0.0.0.0:* LISTENtcp6 0 0 :::53 :::* LISTENtcp6 0 0 :::22 :::* LISTENtcp6 0 0 ::1:953 :::* LISTENroot@localhost:~#As you can see from the output above, port #22 is "LISTENing", which means it is open.In the previous example, -t stands for TCP (recall this protocol from earlier in this chapter), -l stands for "listening" (which ports are listening) and -n stands for "show numbers, not names".Sometimes showing the names can be more useful. Just drop the -n option:root@localhost:~# netstat -tlActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address Statetcp 0 0 cserver.example.:domain *:* LISTENtcp 0 0 localhost:domain *:* LISTENtcp 0 0 *:ssh *:* LISTENtcp 0 0 localhost:953 *:* LISTENtcp6 0 0 [::]:domain [::]:* LISTENtcp6 0 0 [::]:ssh [::]:* LISTENtcp6 0 0 localhost:953 [::]:* LISTENroot@localhost:~#On some distributions you may see the following message in the man page of the netstat command:NOTEThis program is obsolete. Replacement for netstat is ss. Replacement fornetstat -r is ip route. Replacement for netstat -i is ip -s link.Replacement for netstat -g is ip maddr.While no further development is being done on the netstat command, it is still an excellent tool for displaying network information. The goal is to eventually replace the netstat command with commands such as the ss and ip commands. However, it is important to realize that this may take some time.The netstat command is covered in this course because it is available on all Linux distributions, still widely used and it is a Linux Essentials exam objective (the ss and ip commands are not).12.6.5 dig CommandThere may be times when you need to test the functionality of the DNS server that your host is using. One way of doing this is to use the dig command. This command will perform queries on the DNS server to determine if the information needed is available on the server.In the following example, the dig command is used to determine the IP address of the host:root@localhost:~# dig ; <<>> DiG 9.8.1-P1 <<>> ;; global options: +cmd;; Got answer:;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 45155;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 0;; QUESTION SECTION:;. IN A;; ANSWER SECTION:. 86400 IN A 192.168.1.2;; AUTHORITY SECTION:. 86400 IN NS .;; Query time: 0 msec;; SERVER: 127.0.0.1#53(127.0.0.1);; WHEN: Tue Dec 8 17:54:41 2015;; MSG SIZE rcvd: 59root@localhost:~#。

第12章 系 统 监 控


12.1.1 性能分析准则
图形界面下查看系统性能
12.1.2 内存监控
字段 r b swpd free buff cache si so bi bo in cs us sy id wa st
类别 procs(进程) procs(进程) memory(内存) memory(内存) memory(内存) memory(内存) swap(交换页面) swap(交换页面) IO(块设备) IO(块设备) system(系统) system(系统) CPU(处理器) CPU(处理器) CPU(处理器) CPU(处理器) CPU(处理器)
消息级别 emerg alert crit err
说明 最紧急的消息 紧急消息 重要消息 出错消息
消息来源 mail news
syslog
user uucp local0-local7
说明 邮件子系统 网络新闻子系统 syslogd 内 部 产 生 的 信 息 一般用户级别信息 UUCP子系统 本地用户
12.2.1 syslog简介
日志文件 /var/log/message /var/log/secure /var/log/maillog /var/log/cron /var/log/spooler /var/log/boot.log
说明 系统启动后的信息和错误日志,是Red Hat Linux中最常用的日志之一 与安全相关的日志信息
与邮件相关的日志信息 与定时任务相关的日志信息 与UUCP和news设备相关的日志信息 守护进程启动和停止相关的日志消息
12.2.2 syslog配置
消息来源 authpriv cron
daemon
ftp kern lpr
说明 安全/授权信息 定时任务
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

第十二章 模块 本章主要描叙Linux核心动态加载功能模块(如文件系统)的工作原理。 Linux核心是一种monolithic类型的内核,即单一的大程序,核心中所有的功能部件都可以对其全部内部数据结构和例程进行访问。核心的另外一种形式是微内核结构,此时核心的所有功能部件都被拆成独立部分,这些部分之间通过严格的通讯机制进行联系。这样通过配置进程将新部件加入核心的方式非常耗时。比如说我们想为一个NCR 810 SCSI卡配置SCSI驱动,但是核心中没有这个部分。那么我们必须重新配置并重构核心。 Linux可以让我们可以随意动态的加载与卸载操作系统部件。Linux模块就是这样一种可在系统启动后的任何时候动态连入核心的代码块。当我们不再需要它时又可以将它从核心中卸载并删除。Linux模块多指设备驱动、伪设备驱动, 如网络设备和文件系统。 Linux为我们提供了两个命令:使用insmod来显式加载核心模块,使用rmmod来卸载模块。同时核心自身也可以请求核心后台进程kerneld来加载与卸载模块。 动态可加载代码的好处在于可以让核心保持很小的尺寸同时非常灵活。在我的Intel系统中由于使用了模块,整个核心仅为406K字节长。由于我只是偶尔使用VFAT文件系统, 所以我将Linux核心构造成当mount VFAT分区时自动加载VFAT文件系统模块。当我卸载VFAT分区时系统将检测到我不再需要VFAT文件系统模块,将把它从系统中卸载。模块同时还可以让我们无需重构核心并频繁重新启动来尝试运行新核心代码。尽管使用模块很自由,但是也有可能同时带来与核心模块相关的性能与内存损失。可加载模块的代码一般有些长并且额外的数据结构可能会占据一些内存。同时对核心资源的间接使用可能带来一些效率问题。 一旦Linux模块被加载则它和普通核心代码一样都是核心的一部分。它们具有与其他核心代码相同的权限与职责;换句话说Linux核心模块可以象所有核心代码和设备驱动一样使核心崩溃。 模块为了使用所需核心资源所以必须能够找到它们。例如模块需要调用核心内存分配例程kmalloc()来分配内存。模块在构造时并不知道kmalloc()在内存中何处,这样核心必须在使用这些模块前修改模块中对kmalloc()的引用地址。核心在其核心符号表中维护着一个核心资源链表这样当加载模块时它能够解析出模块中对核心资源的引用。Linux还允许存在模块堆栈,它在模块之间相互调用时使用。例如VFAT文件系统模块可能需要FAT文件系统模块的服务,因为VFAT文件系统多少是从FAT文件系统中扩展而来。某个模块对其他模 块的服务或资源的需求类似于模块对核心本身资源或服务的请求。不过此时所请求的服务是来自另外一个事先已加载的模块。每当加载模块时,核心将把新近加载模块输出的所有资源和符号添加到核心符号表中。 当试图卸载某个模块时,核心需要知道此模块是否已经没有被使用,同时它需要有种方法来通知此将卸载模块。 模块必须能够在从核心种删除之前释放其分配的所有系统资源,如核心内存或中断。当模块被卸载时,核心将从核心符号表中删除所有与之对应的符号。 可加载模块具有使操作系统崩溃的能力,而编写较差的模块会带来另外一种问题。当你在一个或早或迟构造的核心而不是当前你运行的核心上加载模块时将会出现什么结果?一种可能的情况是模块将调用具有错误参数的核心例程。核心应该使用严格的版本控制来对加载模块进行检查以防止这种这些情况的发生。 12.1 模块的加载 图12.1 核心模块链表s 核心模块的加载方式有两种。首先一种是使用insmod命令手工加载模块。另外一种则是在需要时加载模块;我们称它为请求加载。当核心发现有必要加载某个模块时,如用户安装了核心中不存在的文件系统时,核心将请求核心后台进程(kerneld)准备加载适当的模块。这个核心后台进程仅仅是一个带有超级用户权限的普通用户进程。当系统启动时它也被启动并为核心打开了一个进程间通讯(IPC)通道。核心需要执行各种任务时用它来向kerneld发送消息。 kerneld的主要功能是加载和卸载核心模块, 但是它还可以执行其他任务, 如通过串行线路建立PPP连接并在适当时候关闭它。kerneld自身并不执行这些任务,它通过某些程序如insmod来做此工作。它只是核心的代理,为核心进行调度。 insmod程序必须找到要求加载的核心模块。请求加载核心模块一般被保存在/lib/modules/kernel-version 中。这些核心模块和系统中其他程序一样是已连接的目标文件,但是它们被连接成可重定位映象。即映象没有被连接到在特定地址上运行。这些核心模块可以是a.out或ELF文件格式。insmod将执行一个特权级系统调用来找到核心的输出符号。这些都以符号名以及数值形式,如地址值成对保存。核心输出符号表被保存在核心维护的模块链表的第一个module结构中,同时module_list指针指向此结构。只有特殊符号被添加到此表中,它们在核心编译与连接时确定,不是核心每个符号都被输出到其模块中。例如设备驱动为了控制某个特定系统中断而由核心例程调用的"request_irq"符号。在我的系统中,其值为0x0010cd30。我们可以通过使用ksyms工具或者查看/proc/ksyms来观看当前核心输出符号。ksyms工具既可以显示所有核心输出符号也可以只显示那些已加载模块的符号。insmod将模块读入虚拟内存并通过使用来自核心输出符号来修改其未解析的核心例程和资源的引用地址。这些修改工作采取由insmod程序直接将符号的地址写入模块中相应地址来修改内存中的模块映象。 当insmod修改完模块对核心输出符号的引用后,它将再次使用特权级系统调用来申请足够的空间来容纳新核心。核心将为其分配一个新的module结构以及足够的核心内存来保存新模块, 并将它放到核心模块链表的尾部。 然后将其新模块标志为UNINITIALIZED。 图12.1给出了一个加载两个模块:VFAT和FAT后的核心链表示意图。不过图中没有画出链表中的第一个模块: 用来存放核心输出符号表的一个伪模块。lsmod可以帮助我们列出系统中所有已加载的核心模块以及相互间依赖关系。它是通过重新格式化从核心module结构中建立的/proc/modules来进行这项工作的。核心为其分配的内存被映射到insmod的地址空间, 这样它就能访问核心空间。insmod将模块拷贝到已分配空间中, 如果为它分配的核心内存已用完,则它将再次申请。不过不要指望多次将加载模块到相同地址,更不用说在两个不同Linux系统的相同位置。另外此重定位工作包括使用适当地址来修改模块映象。 这个新模块也希望将其符号输出到核心中,insmod将为其构造输出符号映象表。每个核心模块必须包含模块 初始化和模块清除例程,它们的符号被设计成故意不输出,但是insmod必须知道这些地址,这样它可以将它们传递给核心。所有这些工作做完之后,insmod将调用初始化代码并执行一个特权级系统调用将模块的初始化与清除例程地址传递给核心。 当将一个新模块加载到核心中间时,核心必须更新其符号表并修改那些被新模块使用的老模块。那些依赖于其他模块的模块必须维护在其符号表尾部维护一个引用链表并在其module数据结构中指向它。图12.1中VFAT 依赖于FAT文件系统模块。所以FAT模块包含一个对VFAT模块的引用;这个引用在加载VFAT模块时添加。核心调用模块的初始化例程,如果成功它将安装此模块。模块的清除例程地址被存储在其module结构中,它将在模块卸载时由核心调用。最后模块的状态被设置成RUNNING。 12.2 模块的卸载 模块可以通过使用rmmod命令来删除, 但是请求加载模块将被kerneld在其使用记数为0时自动从系统中删除。 kerneld在其每次idle定时器到期时都执行一个系统调用以将系统中所有不再使用的请求加载模块从系统中删除。这个定时器的值在启动kerneld时设置;我系统上的值为180秒。这样如果你安装一个iso9660CDROM并且你的iso9660文件系统是一个可加载模块, 则在卸载CD ROM后的很短时间内此iso9660模块将从核心中删除。 如果核心中的其他部分还在使用某个模块,则此模块不能被卸载。例如如果你的系统中安装了多个VFAT文件系统则你将不能卸载VFAT模块。执行lsmod我们将看到每个模块的引用记数。如: Module: #pages: Used by: msdos 5 1 vfat 4 1 (autoclean) fat 6 [vfat msdos] 2 (autoclean)

此记数表示依赖此模块的核心实体个数。在上例中VFAT和msdos模块都依赖于fat模块, 所以fat模块的引用记数为2。vfat和msdos模块的引用记数都为1,表示各有一个已安装文件系统。如果我们安装另一个VFAT文件系统则vfat模块的引用记数将为2。模块的引用记数被保存在其映象的第一个长字中。这个字同时还包含AUTOCLEAN和VISITED标志。请求加载模块使用这两个标志域。如果模块被标记成AUTOCLEAN则核心知道此模块可以自动卸载。VISITED标志表示此模块正被一个或多个文件系统部分使用;只要有其他部分使用此模块则这个标志被置位。每次系统被kerneld要求将没有谁使用的请求模块删除时,核心将在所有模块中扫描可能的候选者。但是一般只查看那些被标志成AUTOCLEAN并处于RUNNING状态的模块。如果某模块的VISITED标记被清除则它将被删除出去。如果某模块可以卸载,则可以调用其清除例程来释放掉分配给它的核心资源。它所对应的module结构将被标记成DELETED并从核心模块链表中断开。其他依赖于它的模块将修改它们各自的引用域来表示它们间的依赖关系不复存在。此模块需要的核心内存都将被回收。

相关文档
最新文档