Linux使用笔记
本文是笔者使用 Linux 过程中的一些总结
内核模块(module)
module 是 Linux 内核的一个重要组成部分,内核模块是 Linux 内核的可加载部分。内核模块可以在运行时动态地加载和卸载。内核模块可以是设备驱动程序、文件系统、网络协议栈等。
加载内核模块的命令是 insmod
和 modprobe
。insmod
命令用于加载一个内核模块,而 modprobe
命令用于加载一个内核模块及其依赖的其他内核模块。基本用法如下所示:
1
2
insmod ./xxx
modprobe xxx
遇到过的问题
- How to make modprobe nf_conntrack_ftp persist a reboot on CentOS 7 and firewalld? - Unix & Linux Stack Exchange
- linux的top命令参数详解 - ggjucheng - 博客园
- logs - monitor files (à la tail -f) in an entire directory (even new ones) - Unix & Linux Stack Exchange
- You know “rwx”, but what is “rws” when run
ls -l
- Linux Tips - DEV Community
磁盘管理
参见 Linux磁盘管理
关于 Linux 下的文件类型可参见 Unix file types - Wikipedia
网络管理
Linux 下的网络管理主要有两种方式:ifconfig
和 ip
。ifconfig
是 Linux 的传统网络管理方式,而 ip
是现代 Linux 发行版的网络管理方式。
不同发行版的 Linux 可能会有不同的网络管理工具,如 NetworkManager
、connman
等。Centos 7 及以上版本使用 NetworkManager
,而 Centos 6.x 及以下版本使用 network
。Ubuntu 16.04 及以上版本使用 netplan
,而 Ubuntu 14.04 及以下版本使用 ifupdown
。
对于 Ubuntu 的配置网络可参见我的另一博客 Ubuntu网络配置
monitor network traffic?
使用 nload(以 CentOS 为例):
1
yum -y install nload
安装好后执行nload
命令即可:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# nload --help
nload version 0.7.4
Copyright (C) 2001 - 2012 by Roland Riegel <feedback@roland-riegel.de>
nload comes with ABSOLUTELY NO WARRANTY. This is free software, and you are
welcome to redistribute it under certain conditions. For more details see the
GNU General Public License Version 2 (http://www.gnu.org/copyleft/gpl.html).
Command line syntax:
nload [options] [devices]
nload --help|-h
Options:
-a period Sets the length in seconds of the time window for average
calculation.
Default is 300.
-i max_scaling Specifies the 100% mark in kBit/s of the graph indicating the
incoming bandwidth usage. Ignored if max_scaling is 0 or the
switch -m is given.
Default is 10240.
-m Show multiple devices at a time; no traffic graphs.
-o max_scaling Same as -i but for the graph indicating the outgoing bandwidth
usage.
Default is 10240.
-t interval Determines the refresh interval of the display in milliseconds.
Default is 500.
-u h|b|k|m|g Sets the type of unit used for the display of traffic numbers.
H|B|K|M|G h: auto, b: Bit/s, k: kBit/s, m: MBit/s etc.
H: auto, B: Byte/s, K: kByte/s, M: MByte/s etc.
Default is h.
-U h|b|k|m|g Same as -u, but for a total amount of data (without "/s").
H|B|K|M|G Default is H.
devices Network devices to use.
Default is to use all auto-detected devices.
--help
-h Print this help.
example: nload -t 200 -i 1024 -o 128 -U M
The options above can also be changed at run time by pressing the 'F2' key.
性能监控
Linux下有很多性能监控工具,下面列出一些常用的:
top
: 几乎所有 Linux 发行版都自带,但输出内容较难理解,推荐htop
htop
:htop
是top
的增强版,提供了更友好的界面和更多的功能uptime
: 查看系统运行时间和负载free
: 查看内存使用情况
top
参见 linux的top命令参数详解 - ggjucheng - 博客园
释放内存
drop_caches
drop_caches
Writing to this will cause the kernel to drop clean caches, as well as
reclaimable slab objects like dentries and inodes. Once dropped, their
memory becomes free.
To free pagecache:
echo 1 > /proc/sys/vm/drop_caches
To free reclaimable slab objects (includes dentries and inodes):
echo 2 > /proc/sys/vm/drop_caches
To free slab objects and pagecache:
echo 3 > /proc/sys/vm/drop_caches
This is a non-destructive operation and will not free any dirty objects.
To increase the number of objects freed by this operation, the user may run
`sync’ prior to writing to /proc/sys/vm/drop_caches. This will minimize the
number of dirty objects on the system and create more candidates to be
dropped.
This file is not a means to control the growth of the various kernel caches
(inodes, dentries, pagecache, etc…) These objects are automatically
reclaimed by the kernel when memory is needed elsewhere on the system.
Use of this file can cause performance problems. Since it discards cached
objects, it may cost a significant amount of I/O and CPU to recreate the
dropped objects, especially if they were under heavy use. Because of this,
use outside of a testing or debugging environment is not recommended.
You may see informational messages in your kernel log when this file is
used:
cat (1234): drop_caches: 3
These are informational only. They do not mean that anything is wrong
with your system. To disable them, echo 4 (bit 3) into drop_caches.
服务管理
Linux 下的服务管理主要有两种方式:System V
和 systemd
。System V
是 Linux 的传统服务管理方式,而 systemd
是现代 Linux 发行版的服务管理方式。
System V
使用 /etc/init.d/
目录下的脚本来管理服务,而 systemd
使用 /etc/systemd/system/
目录下的单元文件来管理服务。
System V
的服务管理命令主要有 service
和 chkconfig
,而 systemd
的服务管理命令主要有 systemctl
。
推荐使用 systemd
,因为它是现代 Linux 发行版的标准服务管理方式。当然,部分 Linux 较老或者是裁剪版本,故可能不支持 systemd
,如 CentOS 6.x 及以下版本。
常见服务
crond
crond 是 Linux 下的定时任务管理工具,类似于 Windows 下的计划任务。crond
会定期检查 /etc/crontab
文件和 /etc/cron.d/
目录下的文件,并执行相应的任务。
查看 crond 是否在运行:
1
systemctl status crond
GUI
Linux 下的图形界面主要有两种:X Window System
和 Wayland
。X Window System
是 Linux 的传统图形界面,而 Wayland
是现代 Linux 发行版的图形界面。
Remote Desktop
Linux 上安装xrdp
或者 vnc 服务
X11
Configure X11 Forwarding with PuTTY and Xming - CentLinux
需要注意的是,Xming 已经不再维护了,推荐使用 VcXsrv
Terminal
tmux
tmux 是 Linux 下的终端复用器,可以在一个终端窗口中运行多个 shell 会话。tmux 可以在后台运行,并且可以在不同的终端之间切换。
遇到过的问题
Shell
shell 是 Linux 下的命令行解释器,可以执行命令和脚本。Linux 下有多种 shell,如 bash
、zsh
、fish
等。其中 bash
是 Linux 的默认 shell。
遇到过的问题
linux - Shell command to monitor changes in a file - Server Fault:tail -F
bash - Select unique or distinct values from a list in UNIX shell script - Stack Overflow:uniq -u
string - How to trim whitespace from a Bash variable? - Stack Overflow
bash - Check if string is neither empty nor space in shell script - Stack Overflow
linux - Print a file, skipping the first X lines, in Bash - Stack Overflow
bash - How to get the start time of a long-running Linux process? - Stack Overflow
bash - Is it possible to make writing to .bash_history immediate? - Ask Ubuntu