linux进阶

159 阅读3分钟

1. 启动和内核

固件系统(用于启动和初始化硬件):bios、uefi

引导加载程序(用于启动操作系统):grub(grub2)、windows-boot-manager、uefi-boot-manager

/boot/ 目录

启动过程

image.png

模块管理

  • lsmod
  • insmod\modprobe
  • rmmod
  • depmod

2. 网络连接

网络

查看所有网络接口
ip a

查看路由表
ip route
netstat -rn

DNS

三种方法查询域名对应的 ip,可以指定 nameserver

nslookup host [server]
nslookup jd.com
nslookup jd.com 114.114.114.114

dig [@server] host
dig google.com
dig @8.8.8.8 google.com

host host [server]
host google.com
host google.com 8.8.8.8

通用 dns 配置文件

  • /etc/hosts
  • /etc/resolv.conf
  • /etc/nsswitch.conf

不同系统下的网络配置

ubuntu:

  • 16: /etc/network/interfaces
  • 18: /etc/netplan/*

centos: /etc/sysconfig/network-scripts 中保存了网络接口的配置文件

...

网络接口绑定模式

balance-rr、active backup、balance-xor、broadcast、802.3ad、balance-tlb、balance-alb

3. 存储管理

磁盘分区方式:mbr、gpt

创建分区,工具:

  • parted/gparted
  • fdisk
  • ...

lsblk:查看系统的块设备

格式化文件系统

  • ext:最常用的、成熟,有 ext2/ext3/ext4,推荐 ext4
  • dos:有 ntfs/vfat/fat32 等 fs
  • xfs
  • btrfs

挂载分区

  • mount/unmout
  • /etc/fstab
  • ...

LVM: PV, VG, LV

LVM 指的是 Logical Volume Manager(逻辑卷管理器),是一种在 Linux 系统中用于管理硬盘空间的技术。它允许用户将多个物理硬盘(或分区)合并为一个逻辑卷组,并在其上创建逻辑卷,在逻辑卷上创建文件系统。

RAID0, RAID1, RAID5

使用 mdadm 工具管理 raid 设备

4. 软件管理

从压缩包安装:压缩编译安装三部曲。缺点是更新软件时很麻烦

debian/ubunt: apt, apt-get, aptitude, dpkg

# 安装,不会自动下载依赖
dpkg -i file.deb
# 清除未安装的包
dpkg -r xxx
# 推荐 使用 apt 安装
apt install xxx

# 获取可更新软件列表
apt update
# 更新可更新的软件
apt upgrade

rpm 包管理器:yum, rpm, dnf

# 安装,不会自动下载依赖包
rpm -i file.rpm
# 安装
yum install xxx
# 更新所有
yum update

配置 apt 仓库:

  • sources.list / sources.list.d
  • gpg keys
  • ppa

配置 yum 仓库:yum.repos.d

arch linux: pacman

opensuse: zypper

5. 管理用户和组

用户信息:全名、用户名、密码、主组、副组、家目录、默认shell 等

用户:

  • adduser
  • useradd / usermod / usermod

组:

  • groupadd / groupmod / groupdel
# 查看某个用户的所在组。第一个为主组
groups [username]

# 将用户加入某个组中
usermod -a -G [groupName] [userName]

查看用户账户及登录状态: whoami, who, id, w, last, id

用户信息和组信息保存的地方:

/etc/passwd, /etc/shadow, /etc/group, /etc/gshadow

编辑这些文件的命令:

vipw, vipw -s, vigr, vigr -s

硬限额、软限额

定位用户 profile

  • 系统级别的:/etc/environment, /etc/bash.bashrc, /etc/bashrc, /etc/profile
  • 个人级别的:/home/user/.bashrc, /home/user/.profile, /home/user/.bash_profile

6. 文件操作

文本编辑器:nano, vi

head, tail, more, less

grep

管道、重定向

# 两种功能相同的写法。前者使用管道、后者使用标准输入
cat file | grep text
grep text < file

# ff 不存在,命令报错,产生标准错误流,不会进行重定向
ls ff > result.txt
# 使用标准错误重定向
ls ff 2> err.txt

/dev/null: 黑洞,bit bucket

ls ~ ff > /dev/null 2>&1

tee,xargs

cat file1 | tee file2
cat file.txt | xargs mkdir
cat file.txt | xargs rmdir

sort, wc

cut, paste

cut -c 1 file.txt
cut -c 3,4,5 file.txt
paste file1 file2

awk, sed

cat file | sed s/a/b/g

awk '{print $2 " " $1}' file

软硬链接:硬链接指向 inode;软链接是快捷方式只记录目标文件位置

find, locate

find / -name *New* 2> /dev/null

scp, rsync

scp user@host:~/file .
scp file user@host:~/

# 传输文件夹(递归)
rsync -av user@host:~/Desktop .

7. 管理本地服务

systemd/sysd

systemctl

  • enable,disable
  • start、stop、status

本地服务配置文件位置:

  • /etc/systemd/system/*
  • /usr/lib/systemd/system/*

sys5/sysv/system5

runlevel,boot target

  • /etc/init.d/
  • service
  • chkconfig:管理服务的 runlevel

systemd init

0 - poweroff
1 - rescue
3 - multi-user
5 - graphical
6 - reboot

systemctl get-default
systemctl set-default multi-user.target
systemctl set-default graphical.target

8. 服务器角色

容器化:docker

ssl

file、mail、proxy、db等服务

日志和监测

vpn

docker vs vm

集群和负载均衡

9. 自动化和计划任务

/etc/cron.d/

  • crontab
  • at, atq, atrm

ctrl-z, &, jobs, fg, bg, nohup

nohup sleep 22222 & > /dev/null 2>&1

10. 设备

dmesg

lsusb, pci, dev, blk, cpu

  • lsblk: 查看块设备
  • lscpu: 查看 cpu 信息
  • lspci: 查看 pci 设备
  • lsusb
  • ...

虚拟文件夹:

  • /proc/
  • /sys/
  • /dev/

cpus 系统:

  • web 界面
  • lpr、lpq、lprm

udev 工具


refer