Linux basic knowledge

本文介绍如何在Linux系统中使用命令行工具查询CPU、内存和硬盘信息,并详细解释了硬链接和软链接的区别及应用场景。

Linux获取CPU,内存和硬盘信息

本文目的

本文搜集了Linux上常用的获取机器配置信息和实时信息相关命令。这些命令在开发运维相关程序时,比较常用。

CPU

  • CPU位数(32或64): getconf LONG_BIT
  • CPU配置信息:cat /proc/cpuinfo
  • CPU实时信息:top -n 1 | grep Cpu | cut -d ',' -f 4 (获取实时CPU空闲率)

          P.S.: 上面命令中最后的4可以换成其他值,以获取不同CPU实时信息

Memeory

  • 内存实时数据:free

          P.S.:第二行数据以系统角度,第三行数据以用户角度

  • 内存实时统计数据:cat /proc/meminfo

Hard Disk

  • 硬盘实时数据:df 
  • 获取目录下每个文件大小:du

Top

top程序可以获取当前时刻系统cpu,mem的相关信息,可以使用相关shell命令,抓取你所需要的特定信息,正如上面获取CPU实时信息中使用的方式。


Hard link is a bit different object when compared to a symlink. In softlink a new file and a new Inode is created, but in hard link, only an entry into directory structure is created for the file, but it points to the inode location of the original file. Which means there is no new inode creation in the hard link

【硬连接】
硬连接指通过索引节点来进行连接。在Linux的文件系统中,保存在磁盘分区中的文件不管是什么类型都给它分配一个编号,称为索引节点号(Inode Index)。在Linux中,多个文件名指向同一索引节点是存在的。一般这种连接就是硬连接。硬连接的作用是允许一个文件拥有多个有效路径名,这样用户就可以建立硬连接到重要文件,以防止“误删”的功能。其原因如上所述,因为对应该目录的索引节点有一个以上的连接。只删除一个连接并不影响索引节点本身和其它的连接,只有当最后一个连接被删除后,文件的数据块及目录的连接才会被释放。也就是说,文件真正删除的条件是与之相关的所有硬连接文件均被删除。
硬连接的2个限制:
1  不允许给目录创建硬链接
2  只有在同一文件系统中的文件之间才能创建链接。 即不同硬盘分区上的两个文件之间不能够建立硬链接。这是因为硬链接是通过结点指向原始文件的,而文件的i-结点在不同的文件系统中可能会不同。
 
【软连接】
另外一种连接称之为符号连接(Symbolic Link),也叫软连接。软链接文件有类似于Windows的快捷方式。它实际上是一个特殊的文件。在符号连接中,文件实际上是一个文本文件,其中包含的有另一文件的位置信息。
这就允许符号链接(经常简写为symlinks)指向位于其他分区、甚至是其他网络硬盘上的某个文件
删除原文件,查看软连接会报找不到文件;删除原文件,对硬链接没有影响。
 
本质的区别:
(1)软连接可以 跨文件系统 ,硬连接不可以 。实践的方法就是用共享文件把windows下的 aa.txt文本文档连接到linux下/root目录 下 bb,cc . ln -s aa.txt /root/bb 连接成功 。ln aa.txt /root/bb 失败;

(2)关于 I节点的问题 。硬连接不管有多少个,都指向的是同一个I节点,会把结点连接数增加,只要结点的连接数不是 0,文件就一直存在 ,不管你删除的是源文件还是连接的文件。只要有一个存在,文件就 存在(其实也不分什么 源文件连接文件的 ,因为他们指向都是同一个 I节点)。 当你修改源文件或者连接文件任何一个的时候,其他的 文件都会做同步的修改 。
 
软链接不直接使用i节点号作为文件指针, 而是使用文件路径名作为指针。所以删除连接文件对源文件无影响,但是删除源文件,连接文件就会找不到要指向的文件。软链接有自己的inode, 并在磁盘上有一小片空间存放路径名。
 
(3)硬链接不增加系统的I节点数目,软链接增加系统I节点数目



Understanding UNIX / Linux symbolic (soft) and hard links

Inodes are associated with precisely one directory entry at a time. However, with hard links it is possible to associate multiple directory entries with a single inode. To create a hard link use ln command as follows:
# ln /root/file1 /root/file2
# ls -l

Above commands create a link to file1. Symbolic links refer to:



A symbolic path indicating the abstract location of another file.

Hard links refer to:

The specific location of physical data.

Hard link vs. Soft link in Linux or UNIX

  • Hard links cannot link directories.
  • Cannot cross file system boundaries.

Soft or symbolic links are just like hard links. It allows to associate multiple filenames with a single file. However, symbolic links allows:

  • To create links between directories.
  • Can cross file system boundaries.

These links behave differently when the source of the link is moved or removed.

  • Symbolic links are not updated.
  • Hard links always refer to the source, even if moved or removed.

How do I create symbolic link?

You can create symbolic link with ln command:
$ ln -s /path/to/file1.txt /path/to/file2.txt
$ ls -ali

Above command will create a symbolic link to file1.txt.

Task: Symbolic link creation and deletion

Let us create a directory called foo, enter:
$ mkdir foo
$ cd foo

Copy /etc/resolv.conf file, enter:
$ cp /etc/resolv.conf .
View inode number, enter:
$ ls -ali
Sample output:

total 152
1048600 drwxr-xr-x   2 vivek vivek   4096 2008-12-09 20:19 .
1015809 drwxrwxrwt 220 root  root  143360 2008-12-09 20:19 ..
1048601 -rwxr-xr-x   1 vivek vivek    129 2008-12-09 20:19 resolv.conf

Now create soft link to resolv.conf, enter:
$ ln -s resolv.conf alink.conf
$ ls -ali

Sample output:

total 152
1048600 drwxr-xr-x   2 vivek vivek   4096 2008-12-09 20:24 .
1015809 drwxrwxrwt 220 root  root  143360 2008-12-09 20:19 ..
1048602 lrwxrwxrwx   1 vivek vivek     11 2008-12-09 20:24 alink.conf -> resolv.conf
1048601 -rwxr-xr-x   1 vivek vivek    129 2008-12-09 20:19 resolv.conf

The reference count of the directory has not changed (total 152). Our symbolic (soft) link is stored in a different inode than the text file (1048602). The information stored in resolv.conf is accessible through the alink.conf file. If we delete the text file resolv.conf, alink.conf becomes a broken link and our data is lost:
$ rm resolv.conf
$ ls -ali

If alink.conf was a hard link, our data would still be accessible through alink.conf. Also, if you delete the soft link itself, the data would still be there. Read man page of ln for more information.
Continue reading rest of the Understanding Linux file system series (this is part VI):

  • Part I – Understanding Linux superblock
  • Part II – Understanding Linux superblock
  • Part III – An example of Surviving a Linux Filesystem Failures
  • Part IV – Understanding filesystem Inodes
  • Part V – Understanding filesystem directories
  • Part VI – Understanding UNIX/Linux symbolic (soft) and hard links
  • Part VII – Why isn’t it possible to create hard links across file system boundaries?
grub配置文件默认为:/boot/grub/grub.conf
 
cat /etc/grub.conf(为软链接)
 
default 定义缺省启动 系统 (多系统有意思)
 
timeout 定义默认等待时间
 
splashimage 定义GRUB界面图片
 
title 定义菜单项名称
 
root  定义内核所在分区
 
kernel 指定内核文件所在位置
 
initrd  指定镜像文件所在位置
 
单用户是干嘛的? 能干嘛?  其实就跟window的安全模式一样 假如你的密码忘记 进去可以修改  单用户也可就是init1 级别
 
     进入单用户模式  开机后进grub界面。按下e进入菜单项,选中kernel行,再次按下e进入编辑模式,在行后空格并输入1,火车保存按b键引导,既可以进入单用户模式
 
   要是想修改你的启动画面等 都可以都这里设置

9.2. GRUB

The GNU GRand Unified Boot loader (GRUB) is a program which enables the selection of the installed operating system or kernel to be loaded at system boot time. It also allows the user to pass arguments to the kernel. 

9.2.1. GRUB and the x86 Boot Process

This section discusses the specific role GRUB plays when booting an x86 system. For a look at the overall boot process, refer to Section 30.2, “A Detailed Look at the Boot Process”

GRUB loads itself into memory in the following stages: 

  1. The Stage 1 or primary boot loader is read into memory by the BIOS from the MBR[4]. The primary boot loader exists on less than 512 bytes of disk space within the MBR and is capable of loading either the Stage 1.5 or Stage 2 boot loader. 

  2. The Stage 1.5 boot loader is read into memory by the Stage 1 boot loader, if necessary. Some hardware requires an intermediate step to get to the Stage 2 boot loader. This is sometimes true when the /boot/partition is above the 1024 cylinder head of the hard drive or when using LBA mode. The Stage 1.5 boot loader is found either on the /boot/ partition or on a small part of the MBR and the /boot/ partition. 

  3. The Stage 2 or secondary boot loader is read into memory. The secondary boot loader displays the GRUB menu and command environment. This interface allows the user to select which kernel or operating system to boot, pass arguments to the kernel, or look at system parameters. 

  4. The secondary boot loader reads the operating system or kernel as well as the contents of /boot/sysroot/ into memory. Once GRUB determines which operating system or kernel to start, it loads it into memory and transfers control of the machine to that operating system. 

The method used to boot Red Hat Enterprise Linux is called direct loading because the boot loader loads the operating system directly. There is no intermediary between the boot loader and the kernel. 

The boot process used by other operating systems may differ. For example, the Microsoft®Windows® operating system, as well as other operating systems, are loaded using chain loading. Under this method, the MBR points to the first sector of the partition holding the operating system, where it finds the files necessary to actually boot that operating system. 

GRUB supports both direct and chain loading boot methods, allowing it to boot almost any operating system.



<think> 首先,用户是用中文提问的:"Linux基本常识",意思是"Linux basic knowledge"。我需要用中文回答这个问题。 我的回答必须基于用户的问题,并提供Linux的基本常识。然后,在回答结束后,我必须包括3到5个§§相关问题§§。第一行必须是完整的"§§相关问题§§",并且不能换行分散。 从示例中看,回答结束后,直接写出"§§相关问题§§",然后列出1.、2.、3.等相关问题。 内容要求:确保所有内容符合规则,否则无效。 所以,结构应该是: 1. 回答用户的问题:讲解Linux基本常识。 2. 结束回答。 3. 立即输出"§§相关问题§§"。 4. 然后列出3-5个相关问题。 现在,针对"Linux基本常识",我需要提供一个简洁的概述。既然是基本常识,我应该覆盖关键点: - Linux是什么:一个开源的、基于Unix的操作系统内核。 - 常见的Linux发行版:如Ubuntu, CentOS, Debian等。 - 基本命令:例如ls, cd, pwd, mkdir等常用命令。 - 文件系统结构:如根目录/ , /home, /etc等。 - 权限管理:用户、组、权限(rwx)。 - 开源特性。 我应该保持回答简明扼要,因为用户要求的是"基本常识",不是详尽教程。 回答后,提出3-5个相关问题。这些问题应该基于我的回答,鼓励用户进一步探讨Linux。例如: 1. Linux与Windows的主要区别是什么? 2. 如何安装一个Linux发行版? 3. 常见Linux命令有哪些及其用法? 4. Linux文件系统的基本结构是怎样的? 5. 如何在Linux中管理用户和权限? 我需要确保问题数量为3到5个,并且它们相关。 最后,输出格式:回答结束时,直接写"§§相关问题§§",然后列出问题,每个问题以数字点开始,如1. ..., 2. ..., 等等。 组织回答: - 开头:解释Linux基本常识。 - 结束回答。 - §§相关问题§§ - 1. ... - 2. ... - 3. ... - [可选4.和5.] 确保语言是中文,因为用户指定了"使用中文回答问题"。 大纲回答: 1. Linux的基本常识: - Linux是一个免费开源的操作系统内核,由林纳斯·托瓦兹于1991年创建。 - 它基于Unix,常用于服务器、嵌入式系统和桌面。 - 用户通常通过发行版使用Linux,如Ubuntu、Fedora、Debian等。 - 基本命令包括:ls(列出目录内容)、cd(改变目录)、mkdir(创建目录)、rm(删除文件)、chmod(改变权限)等。 - 文件系统以根目录/开始,包括/bin、/etc、/home等目录。 - 权限系统基于用户、组和权限位(读、写、执行)。 - Linux强调命令行界面(CLI),但也有图形界面(GUI)。 然后,结束这部分。 相关问题: 1. Linux有哪些常见的发行版? 2. 如何在Linux中查看系统信息? 3. Linux的文件权限是如何工作的? 4. 如何安装软件在Linux上? 5. Linux命令行中有哪些基本快捷键? 我需要列出3-5个,所以可以选择3个或4个。 最终输出: - 回答内容 - §§相关问题§§ - 1. ... - 2. ... - 3. ... </think> ### Linux基本常识 Linux是一种开源的操作系统内核,由林纳斯·托瓦兹于1991年创建。它基于Unix设计,以其稳定性、安全性和多用户支持而闻名。以下是关键常识: - **开源特性**:Linux内核和大多数发行版都是免费的,用户可以自由修改和分发源代码。 - **发行版(Distros)**:常见的Linux发行版包括Ubuntu(适合初学者)、CentOS(服务器常用)、Debian(稳定可靠)等,它们打包了内核、软件包管理器和桌面环境。 - **命令行界面(CLI)**:Linux强调命令行操作,基本命令如: - `ls`:列出目录内容 - `cd`:改变当前目录 - `pwd`:显示当前工作目录 - `mkdir`:创建新目录 - `rm`:删除文件或目录 - `chmod`:修改文件权限(读r、写w、执行x) - **文件系统结构**:以根目录`/`为起点,关键目录包括: - `/bin`:基本可执行文件 - `/etc`:系统配置文件 - `/home`:用户个人目录 - `/var`:日志和可变数据 - **权限管理**:Linux使用用户(user)、组(group)和权限位(如755)来控制访问,确保系统安全。 - **应用场景**:广泛用于服务器、云计算、嵌入式设备(如路由器)和桌面系统。 Linux的优势包括低资源消耗、高度可定制化,以及强大的社区支持。初学者可从Ubuntu等用户友好发行版入门。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值