Linux
0. Table of Contents
Article Directory
- Linux
-
- 0. Table of Contents
- 1. Introduction to Linux
- 2.Linux installation
- 3. Common directory structure
- 4. Commonly used commands
- 5.VI editor
- 6. Software installation
- 7. User Management
- 8. Authority management (absent)
- 9. File system management
- 10. Booting and repairing
- 11. Service Management
- 12. System Management
- 13. Log management (missing)
1. Introduction to Linux
1.1 What is Linux
-
Linux is a free and open source UNIX-like operating system. It was created in 1991 by Linus Stovarz and was mainly inspired by the ideas of Minix and UNIX.
-
Linux complies with the GNU General Public License (GPL), everyone can freely use the source code, or freely modify and redistribute
-
Linux is a multi-user, multi-task, multi-thread and multi-CPU operating system based on POSIX and UNIX
-
Linux can run the main UNIX tool software, applications and network protocols, and inherits the design idea of UNIX with network as the core.
1.2 Linux distribution?
The LInux release is simply to encapsulate the Linux kernel and application software
The mainstream distributions on the market are: Ubuntu, RedHat, CentOS, Debian, Fedora, SuSE, OpenSUSE, Arch Linux, SolusOS, etc.
1.3 LInux application field?
From embedded devices to supercomputers, and to the server field, the status is determined. Usually the server uses the combination of LAMP (Linux+Apache+Mysql+PHP) and LNMP (Linux+Nginx+Mysql+PHP)
At present, Linux is not only used in homes, but also in enterprises at the national level.
1.4 Linux vs Windows?
Compare | Windows | Linux |
---|---|---|
interface | The interface is unified, the shell program is fixed, the menus of all Windows programs are almost the same, and the shortcut keys are almost the same | The style of the graphical interface differs depending on the release version and may not be compatible with each other. GNU/Linux terminals are inherited from UNIX, and the basic commands and operating methods are almost the same. |
driver | The driver program is rich, and the version is updated frequently. The default installation program generally contains the popular hardware drivers when the version is released, and the new hardware drivers released afterwards depend on the hardware manufacturers. For some old hardware, it is sometimes difficult to support it without the original driver. In addition, sometimes the hardware manufacturer does not provide the required version of the driver under Windows, which can be a headache. | Developed by volunteers and released by the Linux core development team. Many hardware manufacturers do not provide drivers due to copyright considerations. Although most of them do not need to be installed manually, the installation is relatively complicated, causing new users to face driver problems (whether they exist and install Method) will be unable to do anything. But under the open source development model, many old hardwares are easy to find drivers even though they are difficult to support under Windows. Hardware vendors such as HP, Intel, and AMD are gradually supporting open source drivers to varying degrees, and the problem is being alleviated. |
use | It is relatively simple to use and easy to get started. The graphical interface is very beneficial to users who have no computer background knowledge. | The graphical interface is simple to use and easy to get started. The text interface requires learning to master. |
Learn | The system structure is complex, changes frequently, and knowledge and skills are eliminated quickly, making it difficult to study in depth. | The system structure is simple and stable, and the inheritance of knowledge and skills is good, and it is relatively easy to study in depth. |
software | Each specific function may need the support of commercial software, and the corresponding authorization needs to be purchased. | Most of the software is freely available, and there are fewer choices of software with the same function. |
2.Linux installation
VMware virtual machine: www.vmware.com
VMware features:
- Two operating systems on the same PC without partitioning and restarting
- The local machine and the virtual machine can communicate over the network
- You can modify the virtual machine configuration at any time
installation steps:
- slightly
2.1 System partition
- Partition Table
- MBR partition table
- Maximum 2.1TB, 4 partitions
- Primary partition (up to four)
- Extended partition (1, data cannot be written)
- Logical partition
- GPT partition table
- Maximum 9.4ZB, no limit to the number of partitions (theoretical)
- MBR partition table
- format
- Also known as logical formatting, it refers to the file system selected by the user (type: ext4)
- Mount
2.2 Matters needing attention
-
Linux is strictly case sensitive
-
linux everything is a file
-
Linux does not distinguish file types by extension
-
All storage devices in Linux are not allowed to be mounted before they can be used
-
Windows programs cannot be used directly in linux
3. Common directory structure
table of Contents | effect |
---|---|
/bin | Directory to store system commands |
/sbin | Directory to store system commands |
/boot | System startup directory |
/dev | Device file directory |
/etc | Configuration file directory |
/home | Ordinary user home directory |
/lib | System call library |
/ mnt | Mount directory |
/opt | Software installation directory |
/proc | Virtual file directory (kernel, process, external device status, network status) |
/sys | Virtual file directory (kernel related) |
/root | root home directory |
/tmp | Temporary directory |
/usr | System software resource directory |
/where | Dynamic data storage directory (cache, log, software running file) |
4. Commonly used commands
command | effect | |
---|---|---|
Directory operations | ls | check the file |
cd | Switch directory | |
pwd | View current directory | |
mkdir | Create a directory | |
rmdir | Delete directory | |
File operations | touch | Create empty files, modify metadata |
stat | check the detail information | |
cat | View file content | |
more | View file contents in split screen | |
less | View file content in branch | |
head | View the file header | |
tail | View the end of the file | |
ln | Soft connection | |
All catalog files are operable | rm | delete |
cp | copy | |
mv | Move or rename | |
Access control | chmod | Modify permissions |
chown | 修改文件所有者和所属组 | |
chgrp | 修改文件所属组 | |
帮助命令 | man | 显示联机帮助手册 |
info | 完整的帮助信息 | |
help | 查看内置命令信息 | |
搜索命令 | whereis | 搜索系统命令(帮助文档位置) |
which | 搜索系统命令 | |
locate | 搜索普通文件 | |
find | 在目录中搜索文件 | |
grep | 显示匹配的行 | |
|管道 | 管道 | |
文本操作 | awk | 匹配截取 |
grep | 匹配 | |
cut | 截取 | |
sed | 截取 | |
压缩、解压命令 | zip | 压缩 |
unzip | 解压.zip | |
gzip | 压缩 | |
gunzip | 解压.gz | |
bzip2 | 压缩 | |
bunzip2 | 解压.bz2 | |
tar -c | 压缩 | |
tar -x | 解压.tar | |
tar -z | .tar.gz | |
tar -j | .tar.bz2 | |
关机重启 | sync | 刷新文件系统缓冲区(数据同步) |
shutdown | 关机-h或重启-r | |
reboot | 重启 | |
halt | 关机(不安全) | |
poweroff | 关机(不安全) | |
init | 关机或重启(不安全) | |
网络命令 | ifconfig | 配置网络接口 |
ping | 通过ICMP网络探测 | |
netstat | 查看网络状态、查看端口 | |
write | 向指定用户发信息 | |
wall | 向所有用户发信息(包括自己) | |
给其他用户发邮件 | ||
系统痕迹命令 | w | 显示正在登陆的用户信息 |
who | 显示正在登陆的用户信息(简单) | |
last | 查看所有登陆过的用户信息 | |
lastlog | 查看最后登陆过的用户信息 | |
lastb | 查看登陆错误信息 | |
挂载命令 | mount | 挂载 |
umount | 卸载 |
5.VI编辑器
vi 、vim 打开文件,进入编辑模式
三种模式:编辑模式、输入模式、末行模式(有不同叫法)
5.1 编辑模式
5.1.1 模式切换
: 进入末行模式
i、a、o 进入输入模式(前、后、下行)
I、A、O进入输入模式(行首、行尾、上行)
5.1.2 移动光标
- 字符间
h: 左;j: 下;k: 上;l: 右 - 单词
w: 移至下一个单词的词首
e: 跳至当前或下一个单词的词尾
b: 跳至当前或前一个单词的词首 - 行内
0: 绝对行首
^: 行首的第一个非空白字符
$: 绝对行尾 - 行间
G:文章末尾
3G:第3行
gg:文章开头 - 翻屏
ctrl:f,b
5.1.3 编辑
- 删除&替换单个字符
x:删除光标位置字符
3x:删除光标开始3个字符
r:替换光标位置字符 - 删除命令 : d
dw,dd - 复制粘贴&剪切
yw,yy
p
P - 撤销&重做
u 撤销
ctrl+r 重做 撤销的操作
. 重复上一步的操作
5.1.4 退出
-
直接退出
ZZ:没修改退出
-
进入末行模式退出
wq:保存并退出
q!:不保存退出
5.2 输入模式
-
标准输入
-
模式切换
Esc进入编辑模式
5.3 末行模式
-
模式切换
Esc Esc 进入编辑模式
-
退出
:q 退出 没有动过文件
:wq 保存并退出 动过了,不后悔
:q! 不保存并退出 动过了,后悔了
:w 保存
:w! 强行保存
:wq --> :x -
高级操作
:set 设置 set nu 设置行数
/ 查找
s/// 查找并替换
6.软件安装
软件包分为源码包和二进制包
6.1 二进制包安装
6.1.1 RPM包安装
-
命令
安装命令:rpm -ivh 包全名
服务启动:service 服务名 start
升级命令:rpm -Uvh 包全名
卸载命令:rpm -e 包名
-
默认安装位置
/etc/ 配置文件
/usr/bin/ 可执行文件
/usr/lib/ 函数库
/usr/share/doc/ 使用手册
/usr/share/man/ 帮助文件
6.1.2 yum安装
- 安装yum
- 安装软件:yum -y install 软件名
6.2 源码包安装
安装步骤
-
下载软件包
-
解压缩
-
进入解压目录
-
编译前准备
./configure
-
编译
make
-
编译安装
make install
卸载:删除文件目录即可
7.用户管理
7.1 用户相关文件
目录 | 作用 | 说明 |
---|---|---|
/etc/passwd | 用户信息文件 | 用户名:密码:用户ID:组ID:用户说明:用户家目录:用户命令行 |
/etc/shadow | 影子文件 | 用户名:加密密码:更改时间:更改间隔:有效期:到期前警告天数:过期后宽限天数:失效时间:保留 |
/etc/group | 组信息文件 | 组名:组密码位:组内其他用户(组) |
/etc/gshadow | 组影子文件 | 保存组密码 |
/home/用户名、/root | 用户家目录 | |
/var/spool/mail/用户名 | 邮箱目录 | |
/etc/skel | 用户模版目录 |
7.2 用户管理命令
命令 | 作用 |
---|---|
useradd | 添加用户 |
passwd | 设置密码 |
usermod | 修改用户 |
userdel | 删除用户 |
su | 切换用户 |
7.3 组管理命令
命令 | 作用 |
---|---|
groupadd | 添加用户组 |
groupdel | 删除用户组 |
gpasswd | 添加删除组成员 |
newgrp | 改变有效组 |
8.权限管理(缺)
9.文件系统管理
9.1 硬盘(缺)
9.2 文件系统
9.2.1 linux文件系统特性
super block(超级块):记录date block、inode的情况
date block(数据块):数据实际存储
inode(i节点):类似元数据
9.2.2 常见文件系统
常见文件系统 | 简介 |
---|---|
ext | linux早期文件系统 |
ext2 | 升级版(更大分区和文件) |
ext3 | 升级版(日志功能和可靠性) |
ext4 | CentOS6.x默认文件系统(颠覆性) |
xfs | CentOS7.x默认文件系统(XFS+LVM) |
swap | 交换分区文件系统(暂时代替内存) |
nfs | 网络文件系统 |
iso9660 | 光盘标准文件系统 |
fat | Windows的fat16 |
vfat | Windows的fat32 |
NTFS | Windows的NTFS |
ufs | SunOS和Solaris |
proc | 基于内存的虚拟文件系统 |
sysfs | 基于内存的虚拟文件系统 |
tmpfs | 基于内存的虚拟文件系统 |
9.3 命令
命令 | 作用 |
---|---|
df | 统计空间大小 |
du | 统计文件大小 |
fsck | 文件系统修复命令 |
dumpe2fs | 显示磁盘状态 |
stat | 显示文件状态 |
file | 判断文件类型 |
fdisk | 分区命令 |
parted | 分区命令 |
parkporbe | 强制重读分区文件、重新挂载所有分区(几乎不用) |
mkfs | 格式化(创建文件系统) |
free | 查看内存 |
swapon | 增加swap分区容量 |
9.4 手动创建分区
9.4.1 MBR分区
fdisk /dev/sdb #进入分区交互界面
n #创建分区
p #查看分区
m #帮助
l #显示文件系统类型
w #保存退出
mkfs -t ext4 /dev/sdb1 #格式化
mkdir /disk1 #创建挂点
mount /dev/sdb1 /disk1 #挂载
vi /etc/fstab #自动挂载
#查看
mount
fdisk -l
df
#修复权限
mount -o remount,rw /
9.4.2 GPT分区
parted /dev/sdb #进入分区交互界面
mklabel gpt #修改类型
mkpart #创建分区
mkfs #创建文件系统
resize #修改分区大小
re #删除分区
print #查看分区
#挂载……
9.4.3 swap分区
fdisk /dev/sdb #进入分区交互界面
mkswap /dev/sdb1 #创建swap分区
swapon name #增加分区
9.5 磁盘配额
9.5.1 依赖
- 内核支持磁盘配额
- grep CONFIG_QUOTA /boot/config-2.6.32-279.e16.i686
- 已安装quota
- rpm -qa |grep quota
9.5.2 概念
- 用户配额和组配额
- 磁盘容量限制和文件个数限制
- 软限制和硬限制
- 宽限时间
9.5.3 配额步骤
-
将分区挂载到目录上
-
用户、用户组、目录(条件)
-
开启磁盘配额功能
mount -o remount,usrquota /disk
vi /etc/fstab
-
defaults,usrquota
-
-
建立配额文件(需关闭SELinux)
quotacheck -avug quotacheck -avugm(跟目录)
-
设置配额
edquota -u username(进入命令行) edquota -p user1 -u user2(配额复制)
-
启动关闭配额
quotaon -avug /disk/ quotaoff -avug /disk/
-
测试
dd if=/dev/zero of=/disk/testfile bs=1M count=60
-
非交互式配额
setquota -u 用户 容量软限制 容量硬限制
9.6 LVM逻辑卷
9.6.1 步骤
-
将物理硬盘分区(也可以整体分区)
fdisk
-
将分区建立物理卷(PV)
pvcreate /dev/sdb pvscan pvdisplay pvremove
-
将物理卷整合为卷组(VG)
vgcreate 选项 卷组名 物理卷名 vgscan
-
把卷组划分为逻辑卷(LV)
lvcreate 选项 卷组名
10.启动引导与修复
10.1 系统运行级别
查看:runlevel
改变级别:init 级别
级别:
- 0:关机
- 1:单用户模式
- 2:不完全的命令行模式(无NFS)
- 3:标准命令行模式
- 4:系统保留
- 5:图形模式
- 6:重启动
永久默认启动级别设置:/etc/inittab
10.1.1 开机执行文件
文件:/etc/rc.d/rc.local
- /var/lock/subsys/local 开机触摸
10.2 启动引导程序
10.2.1 grub引导程序
优点:更多的文件系统、查找内核文件、启动时修改启动选项、动态配置grub配置文件
10.2.2 /boot/grub目录
作用:
- 加载系统内核
- 选择系统的菜单
- 调用其他引导程序,实现多系统引导
10.2.3 grub配置文件
前四行:
- 默认启动系统:0
- 等待选择时间:5
- 背景文件:
- 隐藏按钮
后每四行:可用系统
- 标题
- 启动目录:
- 内核:
- 虚拟文件系统:
10.2.4 grub加密
- grub-md5-crypt
- 将秘钥放在grub配置文件背景文件后
- 不能加lock
10.3 系统修复模式
10.3.1 单用户模式
修改密码:
- 启动时按e,选中grub配置文件内核,按e
- 在grub配置文件内核加空格1,进入系统
- passwd root修改密码
修改启动级别:
- 修改/etc/inittab
10.3.2 光盘修复模式
- 光盘启动
- chroot /mnt/sysimage
- 修复
11.服务管理
11.1 服务分类
- RPM包安装服务
- 独立服务
- 基于xinetd服务
- 源码包安装服务
11.2 管理
11.2.1 RPM包独立服务
-
启动
-
使用/etc/init.d目录下启动脚本启动
/etc/init.d/httpd start
-
使用service命令启动
service 独立服务名 start|stop|restart
-
-
自启动
-
chkconfig命令设置
chkconfig --level 级别 服务名 on|off
-
/etc/rc.d/rc.local文件配置
vi /etc/rc.d/rc.local
-
ntsysv命令设置
ntsysv --level 级别
-
11.2.2 基于xinetd服务
-
启动
- 配置/etc/xinetd.d/telnet
service 独立服务名 start|stop|restart
-
自启动
-
chkconfig命令设置
chkconfig 服务名 on|off
-
11.2.3 源码包服务
-
启动
-
使用/usr/local/服务/bin/…启动脚本启动
/usr/local/服务/bin/... start
-
-
自启动
-
/etc/rc.d/rc.local文件配置
vi /etc/rc.d/rc.local
-
-
配置命令识别
-
service命令识别:软连接启动脚本到/etc/init.d目录下
-
chkconfig命令识别:启动脚本前加入
#chkconfig: 运行级别 启动顺序 关闭 #description:说明
chkconfig --add 服务名
-
12.系统管理
12.1 进程管理
ps 静态查看进程
top 动态查看进程
pstree 查看进程树
kill 杀死进程
killall 杀死进程
pkill 杀死进程
12.2 工作管理
转入后台:
- &
- ctrl+z
后台命令:
- jobs -l 查看
- fg %工作号 恢复到前台
- bg %工作号 后台暂停>后台执行
12.3 资源查看
vmstat 监控系统资源
dmesg 开机时内核检查
free 内存使用情况
/proc/cpuinfo cpu信息
w 已经登陆用户信息
who 已经登陆用户信息
uptime 系统启动时间和平均负载
uname 查看系统与内核信息
12.4 定时任务
at 一次性定时任务
crontab 循环定时任务
anacron 关机情况定时任务
13.日志管理(缺)
常见日志文件目录:/var/log/
13.1 rsyslogd服务
配置文件:/etc/rsyslog.conf
13.2 日志轮替
@笔记分享