您的位置:首页 > 运维架构 > Linux

Linux basic knowledge

2016-05-07 18:33 441 查看


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

by VIVEK GITE on JANUARY
30, 2006 last updated MAY 6, 2012
in FILE
SYSTEM, LINUX, UNIX





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:

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.

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.

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.

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: