网页界面设计作品赏析,徐州seo推广,jsp做手机网站,wordpress网站费用1. 重置root密码 1. 重启服务器#xff08;虚拟机#xff09;2. 快速选择第二项#xff0c;然后按 e 键3. 在linux这一行的最后加上一个空格#xff0c;然后输入 rd.break#xff0c;然后按 ctrl x 来重启服务4. 在提示符所在位置输入 mount -o remount,rw /sysroot5. 在…1. 重置root密码 1. 重启服务器虚拟机2. 快速选择第二项然后按 e 键3. 在linux这一行的最后加上一个空格然后输入 rd.break然后按 ctrl x 来重启服务4. 在提示符所在位置输入 mount -o remount,rw /sysroot5. 在提示符后输入 chroot /sysroot6. 在提示符后输入 echo 新的密码 | passwd --stdin root7. 在提示符后输入 touch /.autorelabel8. 在提示符后输入 exit9. 在提示符后输入 exit 2. 查看IP # 第一种方式
[rootlocalhost ~]# ifconfig# 第二种方式
[rootlocalhost ~]# ip address# 还可以简化
[rootlocalhost ~]# ip addr
[rootlocalhost ~]# ip ad
[rootlocalhost ~]# ip a 判断网络是否畅通 [rootlocalhost ~]# ping -c 3 www.baidu.com 3. 命令的分类
在 linux 中命令是分为两大类 内部命令 外部命令 # 内部命令
[rootlocalhost ~]# type cd
cd is a shell builtin# 外部命令
[rootlocalhost ~]# type cat
cat is /usr/bin/cat 4. 查看时间
查看时间是使用 date 命令来完成的。 [rootlocalhost ~]# date
Wed Feb 21 10:01:15 AM CST 2024
[rootlocalhost ~]# date %j
date: invalid date ‘%j’
[rootlocalhost ~]# date %j
052
[rootlocalhost ~]# date %j
052
[rootlocalhost ~]# date %Y-%m-%d %H:%M:%S
2024-02-21 10:03:36# 或者使用timedatectl
[rootlocalhost ~]# timedatectl --help
timedatectl [OPTIONS...] COMMAND ...Query or change system time and date settings.Commands:status Show current time settingsshow Show properties of systemd-timedatedset-time TIME Set system timeset-timezone ZONE Set system time zonelist-timezones Show known time zonesset-local-rtc BOOL Control whether RTC is in local timeset-ntp BOOL Enable or disable network time synchronizationsystemd-timesyncd Commands:timesync-status Show status of systemd-timesyncdshow-timesync Show properties of systemd-timesyncdOptions:-h --help Show this help message--version Show package version--no-pager Do not pipe output into a pager--no-ask-password Do not prompt for password-H --host[USER]HOST Operate on remote host-M --machineCONTAINER Operate on local container--adjust-system-clock Adjust system clock when changing local RTC mode--monitor Monitor status of systemd-timesyncd-p --propertyNAME Show only properties by this name-a --all Show all properties, including empty ones--value When showing properties, only print the valueSee the timedatectl(1) man page for details.[rootlocalhost ~]# timedatectl statusLocal time: Wed 2024-02-21 10:07:04 CSTUniversal time: Wed 2024-02-21 02:07:04 UTCRTC time: Wed 2024-02-21 02:07:04Time zone: Asia/Shanghai (CST, 0800)
System clock synchronized: yesNTP service: activeRTC in local TZ: no5. 用户切换
用户切换需要使用到 su 命令 [rootlocalhost ~]# su redhat
[redhatlocalhost root]$ exit
exit
[rootlocalhost ~]# su - redhat
[redhatlocalhost ~]$ 在使用 su 来切换用户时带有 - 和不带的区别带有 - 的不仅仅是切换用户同时还会切换用户的运行环境。
如果希望查看用户的信息我们需要使用 id 命令 [rootlocalhost ~]# su redhat
[redhatlocalhost root]$ exit
exit
[rootlocalhost ~]# su - redhat
[redhatlocalhost ~]$ exit
logout
[rootlocalhost ~]# id root
uid0(root) gid0(root) groups0(root)
[rootlocalhost ~]# id redhat
uid1000(redhat) gid1000(redhat) groups1000(redhat),10(wheel) 在 linux 中用户分为三类 超级用户它的 uid 0它是用于管理系统的 系统用户它的 uid 1 ~ 999这些用户是不能登录的目的就是运行相关的软件 普通用户它的 uid 1000 ~ 65535 之间大部分用户都是这类
6. 设置主机名
设置主机名我们需要使用 hostnamectl 命令。 [rootlocalhost ~]# hostnamectl --help
hostnamectl [OPTIONS...] COMMAND ...Query or change system hostname.Commands:status Show current hostname settingshostname [NAME] Get/set system hostnameicon-name [NAME] Get/set icon name for hostchassis [NAME] Get/set chassis type for hostdeployment [NAME] Get/set deployment environment for hostlocation [NAME] Get/set location for hostOptions:-h --help Show this help--version Show package version--no-ask-password Do not prompt for password-H --host[USER]HOST Operate on remote host-M --machineCONTAINER Operate on local container--transient Only set transient hostname--static Only set static hostname--pretty Only set pretty hostname--jsonpretty|short|offGenerate JSON outputSee the hostnamectl(1) man page for details. 使用示例 # 查看主机状态
[rootlocalhost ~]# hostnamectl statusStatic hostname: (unset)
Transient hostname: localhostIcon name: computer-vmChassis: vm Machine ID: 5c5bb57619564f628a3f3f51cb63f773Boot ID: c1c700d3567c42609978aa3567144489Virtualization: vmwareOperating System: Red Hat Enterprise Linux 9.2 (Plow) CPE OS Name: cpe:/o:redhat:enterprise_linux:9::baseosKernel: Linux 5.14.0-284.11.1.el9_2.x86_64Architecture: x86-64Hardware Vendor: VMware, Inc.Hardware Model: VMware Virtual PlatformFirmware Version: 6.00# 查看主机名
[rootlocalhost ~]# hostnamectl hostname
localhost# 修改主机名称
[rootlocalhost ~]# hostnamectl hostname jock
[rootlocalhost ~]# hostnamectl hostname
jock 7. 文件类型
在 linux 中一切皆文件。这些文件也是有类型的但它与 windows 中的文件类型不一样windows 中文件类型是以扩展名来区分的而在 linux 中不存在扩展名的说法它不是以扩展名来进行区分的。linux 中的扩展名唯一的作用是让我们人类知道是什么类型的文件。
在 linux 中文件类型一共有 7 种类型 -表示普通文件 d表示目录 c表示字符设置文件 p表示管道符文件 b表示块设置文件 s表示套接字文件 l表示链接文件 软链接文件它有自己的 inode 值它的内容是这个文件的链接地址如果删除原文件则链接文件失效。 硬链接文件它只是引用同一个文件因此它们的 inode 值一致只是增加了文件的链接数据当删除源文件时链接数减1。
要想查看文件我们需要使用 ls list命令 [rootlocalhost ~]# ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.Mandatory arguments to long options are mandatory for short options too.-a, --all do not ignore entries starting with .-A, --almost-all do not list implied . and ..--author with -l, print the author of each file-b, --escape print C-style escapes for nongraphic characters--block-sizeSIZE with -l, scale sizes by SIZE when printing them;e.g., --block-sizeM; see SIZE format below-B, --ignore-backups do not list implied entries ending with ~-c with -lt: sort by, and show, ctime (time of lastmodification of file status information);with -l: show ctime and sort by name;otherwise: sort by ctime, newest first-C list entries by columns--color[WHEN] colorize the output; WHEN can be always (defaultif omitted), auto, or never; more info below-d, --directory list directories themselves, not their contents-D, --dired generate output designed for Emacs dired mode-f do not sort, enable -aU, disable -ls --color-F, --classify append indicator (one of */|) to entries--file-type likewise, except do not append *--formatWORD across -x, commas -m, horizontal -x, long -l,single-column -1, verbose -l, vertical -C--full-time like -l --time-stylefull-iso-g like -l, but do not list owner--group-directories-firstgroup directories before files;can be augmented with a --sort option, but anyuse of --sortnone (-U) disables grouping-G, --no-group in a long listing, dont print group names-h, --human-readable with -l and -s, print sizes like 1K 234M 2G etc.--si likewise, but use powers of 1000 not 1024-H, --dereference-command-linefollow symbolic links listed on the command line--dereference-command-line-symlink-to-dirfollow each command line symbolic linkthat points to a directory--hidePATTERN do not list implied entries matching shell PATTERN(overridden by -a or -A)--hyperlink[WHEN] hyperlink file names; WHEN can be always(default if omitted), auto, or never--indicator-styleWORD append indicator with style WORD to entry names:none (default), slash (-p),file-type (--file-type), classify (-F)-i, --inode print the index number of each file-I, --ignorePATTERN do not list implied entries matching shell PATTERN-k, --kibibytes default to 1024-byte blocks for disk usage;used only with -s and per directory totals-l use a long listing format-L, --dereference when showing file information for a symboliclink, show information for the file the linkreferences rather than for the link itself-m fill width with a comma separated list of entries-n, --numeric-uid-gid like -l, but list numeric user and group IDs-N, --literal print entry names without quoting-o like -l, but do not list group information-p, --indicator-styleslashappend / indicator to directories-q, --hide-control-chars print ? instead of nongraphic characters--show-control-chars show nongraphic characters as-is (the default,unless program is ls and output is a terminal)-Q, --quote-name enclose entry names in double quotes--quoting-styleWORD use quoting style WORD for entry names:literal, locale, shell, shell-always,shell-escape, shell-escape-always, c, escape(overrides QUOTING_STYLE environment variable)-r, --reverse reverse order while sorting-R, --recursive list subdirectories recursively-s, --size print the allocated size of each file, in blocks-S sort by file size, largest first--sortWORD sort by WORD instead of name: none (-U), size (-S),time (-t), version (-v), extension (-X)--timeWORD change the default of using modification times;access time (-u): atime, access, use;change time (-c): ctime, status;birth time: birth, creation;with -l, WORD determines which time to show;with --sorttime, sort by WORD (newest first)--time-styleTIME_STYLE time/date format with -l; see TIME_STYLE below-t sort by time, newest first; see --time-T, --tabsizeCOLS assume tab stops at each COLS instead of 8-u with -lt: sort by, and show, access time;with -l: show access time and sort by name;otherwise: sort by access time, newest first-U do not sort; list entries in directory order-v natural sort of (version) numbers within text-w, --widthCOLS set output width to COLS. 0 means no limit-x list entries by lines instead of by columns-X sort alphabetically by entry extension-Z, --context print any security context of each file-1 list one file per line. Avoid \n with -q or -b--help display this help and exit--version output version information and exitThe SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).
Binary prefixes can be used, too: KiBK, MiBM, and so on.使用示例 # 查看当前所在目录下的内容
[rootlocalhost ~]# ls
12a 12c BC1 BC3 Documents m1 m3 menu02.sh Music n72 passwd Public test5
12b 12d BC2 Desktop Downloads m2 menu01.sh menu03.sh n71 n73 Pictures Templates Videos# 查看当前目录下所有内容包括隐藏文件
[rootlocalhost ~]# ls -a
. 12b .bash_history .bashrc BC3 .cshrc Downloads m1 menu01.sh Music n73 Public Templates .viminfo
.. 12c .bash_logout BC1 .cache Desktop .lesshst m2 menu02.sh n71 passwd .ssh test5 .Xauthority
12a 12d .bash_profile BC2 .config Documents .local m3 menu03.sh n72 Pictures .tcshrc Videos# 以长列表的格式进行查看
[rootlocalhost ~]# ls -l
total 4
-rw-r--r--. 1 root root 0 Jan 25 14:43 12a
-rw-r--r--. 1 root root 0 Jan 25 14:43 12b
-rw-r--r--. 1 root root 0 Jan 25 14:43 12c
-rw-r--r--. 1 root root 0 Jan 25 14:43 12d
-rw-r--r--. 1 root root 0 Jan 25 14:38 BC1
-rw-r--r--. 1 root root 0 Jan 25 14:38 BC2
-rw-r--r--. 1 root root 0 Jan 25 14:38 BC3
drwxr-xr-x. 2 root root 6 Feb 19 09:08 Desktop
drwxr-xr-x. 2 root root 6 Feb 19 09:08 Documents
drwxr-xr-x. 2 root root 6 Feb 19 09:08 Downloads
-rw-r--r--. 1 root root 0 Jan 25 14:32 m1
-rw-r--r--. 1 root root 0 Jan 25 14:32 m2
-rw-r--r--. 1 root root 0 Jan 25 14:32 m3
-rw-r--r--. 1 root root 0 Jan 25 14:35 menu01.sh
-rw-r--r--. 1 root root 0 Jan 25 14:35 menu02.sh
-rw-r--r--. 1 root root 0 Jan 25 14:35 menu03.sh
drwxr-xr-x. 2 root root 6 Feb 19 09:08 Music
-rw-r--r--. 1 root root 0 Jan 25 14:34 n71
-rw-r--r--. 1 root root 0 Jan 25 14:34 n72
-rw-r--r--. 1 root root 0 Jan 25 14:34 n73
-rw-r--r--. 1 root root 2400 Jan 25 16:20 passwd
drwxr-xr-x. 2 root root 6 Feb 19 09:08 Pictures
drwxr-xr-x. 2 root root 6 Feb 19 09:08 Public
drwxr-xr-x. 2 root root 6 Feb 19 09:08 Templates# 查看指定目录的内容
[rootlocalhost ~]# ls -l /dev
total 0
crw-r--r--. 1 root root 10, 235 Feb 21 09:41 autofs
drwxr-xr-x. 2 root root 160 Feb 21 09:41 block
drwxr-xr-x. 2 root root 60 Feb 21 09:41 bsg
lrwxrwxrwx. 1 root root 3 Feb 21 09:41 cdrom - sr0
drwxr-xr-x. 2 root root 3040 Feb 21 09:41 char
crw--w----. 1 root tty 5, 1 Feb 21 09:41 console
lrwxrwxrwx. 1 root root 11 Feb 21 09:41 core - /proc/kcore
drwxr-xr-x. 6 root root 120 Feb 21 09:41 cpu
crw-------. 1 root root 10, 124 Feb 21 09:41 cpu_dma_latency
drwxr-xr-x. 8 root root 160 Feb 21 09:41 disk
brw-rw----. 1 root disk 253, 0 Feb 21 09:41 dm-0
brw-rw----. 1 root disk 253, 1 Feb 21 09:41 dm-1
drwxr-xr-x. 2 root root 60 Feb 21 09:41 dma_heap
crw-rw---- 1 root audio 14, 9 Feb 21 09:41 dmmidi
drwxr-xr-x. 3 root root 100 Feb 21 09:41 dri# 如果只希望查看目录本身而不是目录的内容
[rootlocalhost ~]# ls -l Documents/
total 0
[rootlocalhost ~]# ls -l -d Documents/
drwxr-xr-x. 2 root root 6 Feb 19 09:08 Documents/
[rootlocalhost ~]# ls -ld Documents/
drwxr-xr-x. 2 root root 6 Feb 19 09:08 Documents/# 查看文件的inode值
[rootlocalhost ~]# ls -lai
total 4833554562 dr-xr-x---. 15 root root 4096 Feb 21 09:42 .128 dr-xr-xr-x. 18 root root 235 Oct 15 19:36 ..34762523 -rw-r--r--. 1 root root 0 Jan 25 14:43 12a34762524 -rw-r--r--. 1 root root 0 Jan 25 14:43 12b34776687 -rw-r--r--. 1 root root 0 Jan 25 14:43 12c34776689 -rw-r--r--. 1 root root 0 Jan 25 14:43 12d34762509 -rw-------. 1 root root 1547 Feb 19 11:49 .bash_history36303874 -rw-r--r--. 1 root root 18 Aug 11 2021 .bash_logout[rootlocalhost ~]# ls -ila /
total 28128 dr-xr-xr-x. 18 root root 235 Oct 15 19:36 .128 dr-xr-xr-x. 18 root root 235 Oct 15 19:36 ..67157107 dr-xr-xr-x. 2 root root 6 Aug 10 2021 afs13383 lrwxrwxrwx. 1 root root 7 Aug 10 2021 bin - usr/bin128 dr-xr-xr-x. 5 root root 4096 Oct 15 19:45 boot我们在使用 ls 命令查看时我们发现目录下都有两个特殊的目录. 和 ..它们分别表示当前目录和上一级目录。如果不是根目录那么当前目录和上一级目录的 inode 值是不一样的这说明它们是两个不同的目录如果是根目录则当前目录和上一级目录的 inode 值是一样的这说明它们两个是同一个目录因为根目录没有上一级目录为了程序的完整所以所根目录的上一级目录指向了它自身。
8. 路径问题
在 linux 中路径分为绝对路径和相对路径。
绝对路径是以 / 开头的路径而相对路径是以当前路径为起点到目标路径。
路径的切换我们可以使用 cd 命令来实现 [rootlocalhost etc]# cd --help
cd: cd [-L|[-P [-e]] [-]] [dir]Change the shell working directory.Change the current directory to DIR. The default DIR is the value of theHOME shell variable.The variable CDPATH defines the search path for the directory containingDIR. Alternative directory names in CDPATH are separated by a colon (:).A null directory name is the same as the current directory. If DIR beginswith a slash (/), then CDPATH is not used.If the directory is not found, and the shell option cdable_vars is set,the word is assumed to be a variable name. If that variable has a value,its value is used for DIR.Options:-L force symbolic links to be followed: resolve symboliclinks in DIR after processing instances of ..-P use the physical directory structure without followingsymbolic links: resolve symbolic links in DIR beforeprocessing instances of ..-e if the -P option is supplied, and the current workingdirectory cannot be determined successfully, exit witha non-zero status- on systems that support it, present a file with extendedattributes as a directory containing the file attributesThe default is to follow symbolic links, as if -L were specified... is processed by removing the immediately previous pathname componentback to a slash or the beginning of DIR.Exit Status:Returns 0 if the directory is changed, and if $PWD is set successfully when-P is used; non-zero otherwise.使用示例 [rootlocalhost usr]# cd /etc
[rootlocalhost etc]# cd /root
[rootlocalhost ~]# cd - # 使用 - 来表示上一次的切换目录位置它会把上一次的目录路径保存在一个变量中
/etc
[rootlocalhost etc]# cd -
/root# 如果希望快速回到家目录则需要使用 ~
[rootlocalhost etc]# cd ~
[rootlocalhost ~]# [rootlocalhost ~]# cd .
[rootlocalhost ~]# cd ..
[rootlocalhost /]# 9. 文件管理
9.1 目录管理
9.1.1 创建目录
创建目录我们需要 mkdir 命令。 在 Linux 中所有的操作都是通过命令的来完成的这些命令如何记意 根据英文单词来记意如catsort 根据单词的组合或简写来记意如lslist、cdchange directory、mkdirmake directory、pwdprint working directory 通过给这些命令分类如文件操作命令目录操作命令权限操作命令磁盘操作命令。。。。。 mkdir 命令的格式 [rootlocalhost /]# mkdir --helpUsage: mkdir [OPTION]... DIRECTORY...Create the DIRECTORY(ies), if they do not already exist.Mandatory arguments to long options are mandatory for short options too.-m, --modeMODE set file mode (as in chmod), not arwx - umask-p, --parents no error if existing, make parent directories as needed-v, --verbose print a message for each created directory-Z set SELinux security context of each created directoryto the default type--context[CTX] like -Z, or if CTX is specified then set the SELinuxor SMACK security context to CTX--help display this help and exit--version output version information and exit 使用示例 # 以绝对路径的方式来创建[rootlocalhost ~]# mkdir /root/dir1# 验证创建结果[rootlocalhost ~]# lsdir1# 创建多级目录[rootlocalhost ~]# mkdir -p dir1/dir11/dir111[rootlocalhost ~]# tree dir1dir1└── dir11└── dir1112 directories, 0 files# 创建多个目录[rootlocalhost ~]# mkdir dir{2,3,4}[rootlocalhost ~]# lsdir1 dir2 dir3 dir4[rootlocalhost ~]# mkdir dir{11..15}[rootlocalhost ~]# lsdir1 dir11 dir12 dir13 dir14 dir15 dir2 dir3 dir4 9.1.2 查看目录
还是使用 ls 命令来查看但是我们要使用 -d 选项这样才能够查看目录本身而不是进入到目录中。 [rootlocalhost ~]# ls -l dir1total 0drwxr-xr-x. 3 root root 20 Feb 21 11:15 dir11[rootlocalhost ~]# ls -dl dir1drwxr-xr-x. 3 root root 19 Feb 21 11:15 dir1# 查看多个目录[rootlocalhost ~]# ls -ld dir{1,2,3}drwxr-xr-x. 3 root root 19 Feb 21 11:15 dir1drwxr-xr-x. 2 root root 6 Feb 21 11:15 dir2drwxr-xr-x. 2 root root 6 Feb 21 11:15 dir3[rootlocalhost ~]# ls -ld dir{1..4}drwxr-xr-x. 3 root root 19 Feb 21 11:15 dir1drwxr-xr-x. 2 root root 6 Feb 21 11:15 dir2drwxr-xr-x. 2 root root 6 Feb 21 11:15 dir3drwxr-xr-x. 2 root root 6 Feb 21 11:15 dir4# 还可以使用通配符[rootlocalhost ~]# ls -ld dir* # * 表示匹配多个字符drwxr-xr-x. 3 root root 19 Feb 21 11:15 dir1drwxr-xr-x. 2 root root 6 Feb 21 11:16 dir11drwxr-xr-x. 2 root root 6 Feb 21 11:16 dir12drwxr-xr-x. 2 root root 6 Feb 21 11:16 dir13drwxr-xr-x. 2 root root 6 Feb 21 11:16 dir14drwxr-xr-x. 2 root root 6 Feb 21 11:16 dir15drwxr-xr-x. 2 root root 6 Feb 21 11:15 dir2drwxr-xr-x. 2 root root 6 Feb 21 11:15 dir3drwxr-xr-x. 2 root root 6 Feb 21 11:15 dir4[rootlocalhost ~]# ls -ld dir? # ? 表示匹配一个字符drwxr-xr-x. 3 root root 19 Feb 21 11:15 dir1drwxr-xr-x. 2 root root 6 Feb 21 11:15 dir2drwxr-xr-x. 2 root root 6 Feb 21 11:15 dir3drwxr-xr-x. 2 root root 6 Feb 21 11:15 dir4 我们可以使用 du 命令来查看文件或目录占用的磁盘空间大小。 [rootlocalhost ~]# du --helpUsage: du [OPTION]... [FILE]...or: du [OPTION]... --files0-fromFSummarize disk usage of the set of FILEs, recursively for directories. 使用示例 [rootlocalhost ~]# du -h .0 ./.ssh0 ./.cache/ibus0 ./.cache/evolution/addressbook/trash0 ./.cache/evolution/addressbook0 ./.cache/evolution/calendar/trash0 ./.cache/evolution/calendar0 ./.cache/evolution/mail/trash0 ./.cache/evolution/mail0 ./.cache/evolution/memos/trash0 ./.cache/evolution/memos0 ./.cache/evolution/sources/trash0 ./.cache/evolution/sources0 ./.cache/evolution/tasks/trash0 ./.cache/evolution/tasks0 ./.cache/evolution2.0M ./.cache/gnome-software/appstream4.0K ./.cache/gnome-software/flatpak-system-default4.0K ./.cache/gnome-software/flatpak-user-user1.5M ./.cache/gnome-software/odrs3.5M ./.cache/gnome-software500K ./.cache/gstreamer-1.04.0M ./.cache4.0K ./.config/ibus/bus4.0K ./.config/ibus4.0K ./.config/dconf4.0K ./.config/evolution/sources4.0K ./.config/evolution0 ./.config/goa-1.04.0K ./.config/gtk-3.00 ./.config/gnome-session/saved-session0 ./.config/gnome-session32K ./.config........... 9.1.3 删除目录
删除目录我们要使用 rmremove 命令来实现它的使用格式为 [rootlocalhost ~]# rm --helpUsage: rm [OPTION]... [FILE]...Remove (unlink) the FILE(s).-f, --force ignore nonexistent files and arguments, never prompt-i prompt before every removal-I prompt once before removing more than three files, orwhen removing recursively; less intrusive than -i,while still giving protection against most mistakes--interactive[WHEN] prompt according to WHEN: never, once (-I), oralways (-i); without WHEN, prompt always--one-file-system when removing a hierarchy recursively, skip anydirectory that is on a file system different fromthat of the corresponding command line argument--no-preserve-root do not treat / specially--preserve-root[all] do not remove / (default);with all, reject any command line argumenton a separate device from its parent-r, -R, --recursive remove directories and their contents recursively-d, --dir remove empty directories-v, --verbose explain what is being done--help display this help and exit--version output version information and exitBy default, rm does not remove directories. Use the --recursive (-r or -R)option to remove each listed directory, too, along with all of its contents.To remove a file whose name starts with a -, for example -foo,use one of these commands:rm -- -foorm ./-foo 使用示例 [rootlocalhost ~]# lsdir1 dir11 dir12 dir13 dir14 dir15 dir2 dir3 dir4# 如果删除的是目录是不会成功的[rootlocalhost ~]# rm dir2rm: cannot remove dir2: Is a directory# 如果删除的目录是空目录则需要带 -d 选项[rootlocalhost ~]# rm -d dir2rm: remove directory dir2? yes [rootlocalhost ~]# lsdir1 dir11 dir12 dir13 dir14 dir15 dir3 dir4# 当删除的目录不为空时需要带上 -r 选项[rootlocalhost ~]# rm -d dir1rm: cannot remove dir1: Directory not empty[rootlocalhost ~]# rm -r dir1rm: descend into directory dir1? yesrm: descend into directory dir1/dir11? yesrm: remove directory dir1/dir11/dir111? yesrm: remove directory dir1/dir11? yesrm: remove directory dir1? yes[rootlocalhost ~]# lsdir11 dir12 dir13 dir14 dir15 dir3 dir4# 删除多个目录[rootlocalhost ~]# rm -rf dir{3,4}[rootlocalhost ~]# lsdir11 dir12 dir13 dir14 dir15[rootlocalhost ~]# rm -rf dir{11..13}[rootlocalhost ~]# lsdir14 dir15# 删除所有[rootlocalhost ~]# rm -rf *[rootlocalhost ~]# ls[rootlocalhost ~]# 9.2 普通文件管理
9.2.1 创建文件
创建文件可以有以下几种方式 使用 touch 命令 使用重定向符 vi或vim命令
9.2.1.1 touch
这个命令是用于创建一个空的文件如果创建的文件不存在则创建如果存在则更新文件的元数据但是创建时间不变。 [rootlocalhost ~]# touch a.txt# 查看这个文件的元数据信息使用 stat 命令来查看[rootlocalhost ~]# stat a.txtFile: a.txtSize: 0 Blocks: 0 IO Block: 4096 regular empty fileDevice: fd00h/64768d Inode: 34762516 Links: 1Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)Context: unconfined_u:object_r:admin_home_t:s0Access: 2024-02-21 11:42:25.552932073 0800Modify: 2024-02-21 11:42:25.552932073 0800Change: 2024-02-21 11:42:25.552932073 0800Birth: 2024-02-21 11:42:25.552932073 0800#重新创建已有文件发现元数据除了创建时间都进行了更新[rootlocalhost ~]# touch a.txt[rootlocalhost ~]# stat a.txtFile: a.txtSize: 0 Blocks: 0 IO Block: 4096 regular empty fileDevice: fd00h/64768d Inode: 34762516 Links: 1Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)Context: unconfined_u:object_r:admin_home_t:s0Access: 2024-02-21 11:45:43.123726570 0800Modify: 2024-02-21 11:45:43.123726570 0800Change: 2024-02-21 11:45:43.123726570 0800Birth: 2024-02-21 11:42:25.552932073 0800 Access: 2024-02-21 11:42:25.552932073 0800 表示访问文件的时间 Modify: 2024-02-21 11:42:25.552932073 0800 表示文件内容的修改时间 Change: 2024-02-21 11:42:25.552932073 0800 表示文件元数据的修改时间 Birth: 2024-02-21 11:42:25.552932073 0800 表示文件的创建时间 创建多个文件 [rootlocalhost ~]# touch file{1..4}[rootlocalhost ~]# lsa.txt file1 file2 file3 file4[rootlocalhost ~]# touch file{11,12,13}[rootlocalhost ~]# lsa.txt file1 file11 file12 file13 file2 file3 file4 9.2.1.2 echo
这个命令的作用是将它的参数显示在终端上。 # 显示123[rootlocalhost ~]# echo 123123# 显示 aaa[rootlocalhost ~]# echo aaaa# 显示变量$USER的内容[rootlocalhost ~]# echo $USERroot# 显示变量$PWD的内容[rootlocalhost ~]# echo $PWD/root 9.2.1.3 重定向
数据流向输入和输出。
标准输入在默认情况下标准输入是从键盘或鼠标中获取输入的内容。
标准输出在默认情况下标准输出是将命令执行后的正确结果传到终端屏幕上。
标准错误输出将命令执行后的错误信息输出到屏幕或文件中。
输入是使用 来表示而输出是使用 或 来表示。
表示覆盖的形式来输出内容
表示是追加的形式来输出内容
标准输入的数字代码为 0标准输出的数字代码为 1标准错误输出的数字代码为 2。
通过 echo 加上重定向来创建文件。 # 将当前日期写入到 date.txt 文件中[rootlocalhost ~]# date 1 date.txt # 注意1和之间不要有空格它表示将命令执行后的结果输出到指定文件中[rootlocalhost ~]# cat date.txtWed Feb 21 02:25:36 PM CST 2024[rootlocalhost ~]# echo hello date.txt[rootlocalhost ~]# cat date.txtWed Feb 21 02:27:10 PM CST 2024hello 使用 或者 时如果文件不存在则会自动把文件创建起来如果文件存在则覆盖或追加内容到指定的文件中。 [rootlocalhost ~]# ls /etc/passwd
/etc/passwd
[rootlocalhost ~]# ls /etc/passwd date.txt
[rootlocalhost ~]# cat date.txt
Wed Feb 21 02:27:10 PM CST 2024
hello
/etc/passwd# 查看一个不存在的文件或目录
[rootlocalhost ~]# ls /aaa
ls: cannot access /aaa: No such file or directory# 将正确执行的结果写入到 success.txt 文件中将执行过程中发生的错误信息写入到 error.txt 文件中
[rootlocalhost ~]# ls /aaa success.txt 2 error.txt
[rootlocalhost ~]# ls
a.txt date.txt error.txt file1 file11 file12 file13 file2 file3 file4 success.txt
[rootlocalhost ~]# cat success.txt
[rootlocalhost ~]# cat error.txt
ls: cannot access /aaa: No such file or directory 9.2.2 查看文件
9.2.2.1 cat
这个命令用于查看文件的所有内容。 [rootlocalhost ~]# cat a.txt
hello
redhat
world
hadoop
python
java
linux
# 查看时输出行号
[rootlocalhost ~]# cat -n a.txt1 hello2 redhat3 world4 hadoop5 python6 java7 linux# 内容中加了空行
[rootlocalhost ~]# cat a.txt
helloredhat
worldhadoop
python
java
linux
# 使用 -n 时空行也会标上行号
[rootlocalhost ~]# cat -n a.txt1 hello2 3 redhat4 world5 6 hadoop7 python8 java9 linux
# 使用 -b 时空行不会标上行号
[rootlocalhost ~]# cat -b a.txt1 hello2 redhat3 world4 hadoop5 python6 java7 linux 9.2.2.2 head
这个命令是默认读取文件的前 10 行内容。命令和使用格式 [rootlocalhost ~]# head --help
Usage: head [OPTION]... [FILE]...
Print the first 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.With no FILE, or when FILE is -, read standard input.Mandatory arguments to long options are mandatory for short options too.-c, --bytes[-]NUM print the first NUM bytes of each file;with the leading -, print all but the lastNUM bytes of each file-n, --lines[-]NUM print the first NUM lines instead of the first 10;with the leading -, print all but the lastNUM lines of each file-q, --quiet, --silent never print headers giving file names-v, --verbose always print headers giving file names-z, --zero-terminated line delimiter is NUL, not newline--help display this help and exit--version output version information and exit 使用示例 # 默认显示文件的前 10 行
[rootlocalhost ~]# head /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
# 指定显示文件前 5 行
[rootlocalhost ~]# head -n 5 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
# 指定显示文件的前 10 个字符
[rootlocalhost ~]# head -c 10 /etc/passwd
root:x:0:0# 显示前面10行并且带行号
[rootlocalhost ~]# head /etc/passwd | cat -n1 root:x:0:0:root:/root:/bin/bash2 bin:x:1:1:bin:/bin:/sbin/nologin3 daemon:x:2:2:daemon:/sbin:/sbin/nologin4 adm:x:3:4:adm:/var/adm:/sbin/nologin5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin6 sync:x:5:0:sync:/sbin:/bin/sync7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown8 halt:x:7:0:halt:/sbin:/sbin/halt9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin10 operator:x:11:0:operator:/root:/sbin/nologin 9.2.2.3 tail
这个命令用于默认显示文件的最后 10 行内容。它的使用格式为 [rootlocalhost ~]# tail --help
Usage: tail [OPTION]... [FILE]...
Print the last 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.With no FILE, or when FILE is -, read standard input.Mandatory arguments to long options are mandatory for short options too.-c, --bytes[]NUM output the last NUM bytes; or use -c NUM tooutput starting with byte NUM of each file-f, --follow[{name|descriptor}]output appended data as the file grows;an absent option argument means descriptor-F same as --followname --retry-n, --lines[]NUM output the last NUM lines, instead of the last 10;or use -n NUM to output starting with line NUM--max-unchanged-statsNwith --followname, reopen a FILE which has notchanged size after N (default 5) iterationsto see if it has been unlinked or renamed(this is the usual case of rotated log files);with inotify, this option is rarely useful--pidPID with -f, terminate after process ID, PID dies-q, --quiet, --silent never output headers giving file names--retry keep trying to open a file if it is inaccessible-s, --sleep-intervalN with -f, sleep for approximately N seconds(default 1.0) between iterations;with inotify and --pidP, check process P atleast once every N seconds-v, --verbose always output headers giving file names-z, --zero-terminated line delimiter is NUL, not newline--help display this help and exit--version output version information and exit使用示例 # 查看密码文件的最后 10 行内容
[rootlocalhost ~]# tail /etc/passwd
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
cockpit-ws:x:983:982:User for cockpit web service:/nonexisting:/sbin/nologin
cockpit-wsinstance:x:982:981:User for cockpit-ws instances:/nonexisting:/sbin/nologin
gnome-initial-setup:x:981:980::/run/gnome-initial-setup/:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/usr/share/empty.sshd:/sbin/nologin
chrony:x:980:978:chrony system user:/var/lib/chrony:/sbin/nologin
dnsmasq:x:979:977:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
redhat:x:1000:1000:redhat:/home/redhat:/bin/bash# 查看密码文件最后 5 行内容
[rootlocalhost ~]# tail -n 5 /etc/passwd
sshd:x:74:74:Privilege-separated SSH:/usr/share/empty.sshd:/sbin/nologin
chrony:x:980:978:chrony system user:/var/lib/chrony:/sbin/nologin
dnsmasq:x:979:977:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
redhat:x:1000:1000:redhat:/home/redhat:/bin/bash 9.2.2.4 less
这个命令是用于分页查看文件的内容可以按 enter 键来向下逐行滚动显示也可以按空格键来向下翻一页还可以按 b 键向上翻一页如果要退出则按 q 键。 [rootlocalhost ~]# cat /etc/passwd passwd
[rootlocalhost ~]# cat /etc/passwd passwd
[rootlocalhost ~]# less passwd 9.2.2.5 more
它的作用与 less 的作用一样但是 more 显示到最后会自动退出。 [rootlocalhost ~]# more passwd 9.2.3 过滤文件内容
这个功能的实现需要使用到 grep 命令。它的作用是在指定的普通文件中查找并显示包含指定的字符串的行。
命令的使用格式为 [rootlocalhost ~]# grep --help
Usage: grep [OPTION]... PATTERNS [FILE]...
Search for PATTERNS in each FILE.
Example: grep -i hello world menu.h main.c
PATTERNS can contain multiple patterns separated by newlines.Pattern selection and interpretation:-E, --extended-regexp PATTERNS are extended regular expressions-F, --fixed-strings PATTERNS are strings-G, --basic-regexp PATTERNS are basic regular expressions-P, --perl-regexp PATTERNS are Perl regular expressions-e, --regexpPATTERNS use PATTERNS for matching-f, --fileFILE take PATTERNS from FILE-i, --ignore-case ignore case distinctions in patterns and data--no-ignore-case do not ignore case distinctions (default)-w, --word-regexp match only whole words-x, --line-regexp match only whole lines-z, --null-data a data line ends in 0 byte, not newlineMiscellaneous:-s, --no-messages suppress error messages-v, --invert-match select non-matching lines-V, --version display version information and exit--help display this help text and exitOutput control:-m, --max-countNUM stop after NUM selected lines-b, --byte-offset print the byte offset with output lines-n, --line-number print line number with output lines--line-buffered flush output on every line-H, --with-filename print file name with output lines-h, --no-filename suppress the file name prefix on output--labelLABEL use LABEL as the standard input file name prefix-o, --only-matching show only nonempty parts of lines that match-q, --quiet, --silent suppress all normal output--binary-filesTYPE assume that binary files are TYPE;TYPE is binary, text, or without-match-a, --text equivalent to --binary-filestext-I equivalent to --binary-fileswithout-match-d, --directoriesACTION how to handle directories;ACTION is read, recurse, or skip-D, --devicesACTION how to handle devices, FIFOs and sockets;ACTION is read or skip-r, --recursive like --directoriesrecurse-R, --dereference-recursivelikewise, but follow all symlinks--includeGLOB search only files that match GLOB (a file pattern)--excludeGLOB skip files that match GLOB--exclude-fromFILE skip files that match any file pattern from FILE--exclude-dirGLOB skip directories that match GLOB-L, --files-without-match print only names of FILEs with no selected lines-l, --files-with-matches print only names of FILEs with selected lines-c, --count print only a count of selected lines per FILE-T, --initial-tab make tabs line up (if needed)-Z, --null print 0 byte after FILE nameContext control:-B, --before-contextNUM print NUM lines of leading context-A, --after-contextNUM print NUM lines of trailing context-C, --contextNUM print NUM lines of output context-NUM same as --contextNUM--group-separatorSEP use SEP as a group separator--no-group-separator use empty string as a group separator--color[WHEN],--colour[WHEN] use markers to highlight the matching strings;WHEN is always, never, or auto-U, --binary do not strip CR characters at EOL (MSDOS/Windows) 在使用过程中常用的选项有
-c 仅显示找到的行数
-i 忽悠大小写
-n 显示行号
-v 反向选择
使用示例
1我们在 /root/passwd 文件中查找包含所有 root 的行 [rootlocalhost ~]# grep root passwd
Root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
# 忽悠查找内容的大小写匹配
[rootlocalhost ~]# grep -i root passwd
Root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin 2使用特殊符号来进行字符串的匹配 # 在 /root/passwd 文件中查找以 a 开头的所有行
[rootlocalhost ~]# grep ^a passwd
adm:x:3:4:adm:/var/adm:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin# 在 /root/passwd 文件中查找以 h 结尾的所有行
[rootlocalhost ~]# grep h$ passwd
Root:x:0:0:root:/root:/bin/bash
redhat:x:1000:1000:redhat:/home/redhat:/bin/bash
root:x:0:0:root:/root:/bin/bash
redhat:x:1000:1000:redhat:/home/redhat:/bin/bash# 在 /etc/ssh/sshd_config 查找所有带有数字的行
[rootlocalhost ~]# grep -n [0-9] /etc/ssh/sshd_config
1:# $OpenBSD: sshd_config,v 1.104 2021/07/02 05:11:21 dtucker Exp $
4:# sshd_config(5) for more information.
21:#Port 22
23:#ListenAddress 0.0.0.0
28:#HostKey /etc/ssh/ssh_host_ed25519_key
39:#LoginGraceTime 2m
42:#MaxAuthTries 6
43:#MaxSessions 10
47:# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
83:#GSSAPIEnablek5users no
101:#X11Forwarding no
102:#X11DisplayOffset 10
103:#X11UseLocalhost yes
110:#ClientAliveInterval 0
111:#ClientAliveCountMax 3
114:#MaxStartups 10:30:100
127:# X11Forwarding no 9.2.4 文本内容统计
这个命令的作用是将指定文件内容进行统计可以统计字符也可以统计行数。
wcword count命令的格式 [rootlocalhost ~]# wc --help
Usage: wc [OPTION]... [FILE]...or: wc [OPTION]... --files0-fromF
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified. A word is a non-zero-length sequence of
characters delimited by white space.With no FILE, or when FILE is -, read standard input.The options below may be used to select which counts are printed, always in
the following order: newline, word, character, byte, maximum line length.-c, --bytes print the byte counts-m, --chars print the character counts-l, --lines print the newline counts--files0-fromF read input from the files specified byNUL-terminated names in file F;If F is - then read names from standard input-L, --max-line-length print the maximum display width-w, --words print the word counts--help display this help and exit--version output version information and exit使用示例 # 统计 /etc/passwd 文件的行数
[rootlocalhost ~]# wc -l /etc/passwd
39 /etc/passwd9.2.5 切割文件内容
这个功能需要使用 cut 命令主来实现它功能就是用于按列提取文件的内容。它的语法格式为 [rootlocalhost ~]# cut --help
Usage: cut OPTION... [FILE]...
Print selected parts of lines from each FILE to standard output.With no FILE, or when FILE is -, read standard input.Mandatory arguments to long options are mandatory for short options too.-b, --bytesLIST select only these bytes-c, --charactersLIST select only these characters-d, --delimiterDELIM use DELIM instead of TAB for field delimiter-f, --fieldsLIST select only these fields; also print any linethat contains no delimiter character, unlessthe -s option is specified-n with -b: dont split multibyte characters--complement complement the set of selected bytes, charactersor fields-s, --only-delimited do not print lines not containing delimiters--output-delimiterSTRING use STRING as the output delimiterthe default is to use the input delimiter-z, --zero-terminated line delimiter is NUL, not newline--help display this help and exit--version output version information and exit使用示例 # 查看 /etc/passwd 文件的第一列内容
[rootlocalhost ~]# cut -d : -f 1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
tss
systemd-coredump
.....# 查看 /etc/passwd 文件中家目录
[rootlocalhost ~]# cut -d : -f 6 /etc/passwd
/root
/bin
/sbin
/var/adm
/var/spool/lpd
/sbin
/sbin
/sbin
/var/spool/mail
/root
/usr/games
.....# 查看 /etc/passwd 文件中第一列和第六列的内容
[rootlocalhost ~]# cut -d : -f 1,6 /etc/passwd
root:/root
bin:/bin
daemon:/sbin
adm:/var/adm
lp:/var/spool/lpd
sync:/sbin
shutdown:/sbin
halt:/sbin
mail:/var/spool/mail
operator:/root
.....# 查看 /etc/passwd 文件的第一列到第三列的内容
[rootlocalhost ~]# cut -d : -f 1-3 /etc/passwd
root:x:0
bin:x:1
daemon:x:2
adm:x:3
lp:x:4
sync:x:5
shutdown:x:6
halt:x:7
mail:x:8
operator:x:11
......9.2.6 排序显示内容
排序显示需要使用到 sort 命令它的功能就是将文件的内容进行排序再显示。语法格式如下 [rootlocalhost ~]# sort --help
Usage: sort [OPTION]... [FILE]...or: sort [OPTION]... --files0-fromF
Write sorted concatenation of all FILE(s) to standard output.With no FILE, or when FILE is -, read standard input.Mandatory arguments to long options are mandatory for short options too.
Ordering options:-b, --ignore-leading-blanks ignore leading blanks-d, --dictionary-order consider only blanks and alphanumeric characters-f, --ignore-case fold lower case to upper case characters-g, --general-numeric-sort compare according to general numerical value-i, --ignore-nonprinting consider only printable characters-M, --month-sort compare (unknown) JAN ... DEC-h, --human-numeric-sort compare human readable numbers (e.g., 2K 1G)-n, --numeric-sort compare according to string numerical value-R, --random-sort shuffle, but group identical keys. See shuf(1)--random-sourceFILE get random bytes from FILE-r, --reverse reverse the result of comparisons--sortWORD sort according to WORD:general-numeric -g, human-numeric -h, month -M,numeric -n, random -R, version -V-V, --version-sort natural sort of (version) numbers within textOther options:--batch-sizeNMERGE merge at most NMERGE inputs at once;for more use temp files-c, --check, --checkdiagnose-first check for sorted input; do not sort-C, --checkquiet, --checksilent like -c, but do not report first bad line--compress-programPROG compress temporaries with PROG;decompress them with PROG -d--debug annotate the part of the line used to sort,and warn about questionable usage to stderr--files0-fromF read input from the files specified byNUL-terminated names in file F;If F is - then read names from standard input-k, --keyKEYDEF sort via a key; KEYDEF gives location and type-m, --merge merge already sorted files; do not sort-o, --outputFILE write result to FILE instead of standard output-s, --stable stabilize sort by disabling last-resort comparison-S, --buffer-sizeSIZE use SIZE for main memory buffer-t, --field-separatorSEP use SEP instead of non-blank to blank transition-T, --temporary-directoryDIR use DIR for temporaries, not $TMPDIR or /tmp;multiple options specify multiple directories--parallelN change the number of sorts run concurrently to N-u, --unique with -c, check for strict ordering;without -c, output only the first of an equal run-z, --zero-terminated line delimiter is NUL, not newline--help display this help and exit--version output version information and exit在使用过程中常用的选项有
-f 忽悠大小写
-b 忽悠缩进与空格
-n 以数值进行排序
-r 反向排序
-u 去除重复的行
-t 指定间隔符
-k 设置字段的范围
使用示例 # 查看 /etc/passwd 文件第 3 列并排序
# 接合 cut 来实现
[rootlocalhost ~]# cut -d : -f 3 /etc/passwd | sort # 得到的是自然顺序
[rootlocalhost ~]# cut -d : -f 3 /etc/passwd | sort -n # 得到的是数值顺序
[rootlocalhost ~]# cut -d : -f 3 /etc/passwd | sort -nr # 得到反序# 使用 sort 独自实现
[rootlocalhost ~]# sort -t : -k 3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
redhat:x:1000:1000:redhat:/home/redhat:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
[rootlocalhost ~]# sort -t : -k 3 -n /etc/passwd# 也可以对文件的内容进行排序
[rootlocalhost ~]# sort /etc/passwd
adm:x:3:4:adm:/var/adm:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin9.2.7 去重显示内容
要实现这个功能我们可以使用 uniq 命令它的功能就是用于去除文本中连续的重复的行。语法如下 [rootlocalhost ~]# uniq --help
Usage: uniq [OPTION]... [INPUT [OUTPUT]]
Filter adjacent matching lines from INPUT (or standard input),
writing to OUTPUT (or standard output).With no options, matching lines are merged to the first occurrence.Mandatory arguments to long options are mandatory for short options too.-c, --count prefix lines by the number of occurrences-d, --repeated only print duplicate lines, one for each group-D print all duplicate lines--all-repeated[METHOD] like -D, but allow separating groupswith an empty line;METHOD{none(default),prepend,separate}-f, --skip-fieldsN avoid comparing the first N fields--group[METHOD] show all items, separating groups with an empty line;METHOD{separate(default),prepend,append,both}-i, --ignore-case ignore differences in case when comparing-s, --skip-charsN avoid comparing the first N characters-u, --unique only print unique lines-z, --zero-terminated line delimiter is NUL, not newline-w, --check-charsN compare no more than N characters in lines--help display this help and exit--version output version information and exit使用示例 # 案例准备
[rootlocalhost ~]# vim file1
[rootlocalhost ~]# cat file1
hello 9
hello 9
world 7
hello 9
world 7
world 7# 去除 file1 文件中重复的行
[rootlocalhost ~]# uniq file1
hello 9
world 7
hello 9
world 7[rootlocalhost ~]# uniq -c file12 hello 91 world 71 hello 92 world 7 9.2.8 编辑文件
使用 vim 来进行文件的编辑包括创建、修改。
在Vim编辑器中设置了三种模式命令模式、末行模式和编辑模式 命令模式 这是 vim 进入后的默认模式。在这个模式下可以执行如下的命令- 光标跳转1. 通过上下左右键来进行移动2. gg 用于回到文件开头连续按两次 g 键3. G 用于回到文件的结尾shift g4. 将当前光标所在位置移动到行首和行尾按^行首和$行尾5. 按单词进行向后移动按 w如果希望按单词几前则按 b- 复制1. yy表示复制nyy表示复制 n 行从光标所以位置向后计算2. p表示粘贴所复制的内容- 删除1. u表示撤销当前的操作2. dd表示删除光标所在行ndd表示删除从光标所在行向后计算 n 行dG 表示从当前光标删除后最后dgg表示从当前光标删除到最前 编辑模式 编辑模式也叫插入模式当我们在命令模式下按 aios 或者 AIOS 这几个键时就会从命令模式转换为编辑模式。i表示在将前插入a表示在光标后插入I表示在非空行前插入A表示在非空行后插入o表示在光标所在行的下一行插入O表示在光标所在行的上一行插入s表示删除插入S表示删除行插入 末行模式 - 文档保存并退出 :wq shiftzz
- 文件保存 :w
- 退出 :q :q!
- 另存为 :w 文件路径
- 将指定的文件写入到该文件中 :r 文件路径
- 执行关键字搜索 /输入要搜索的关键字 找到后按 n 向后查找按 N 向前查找
- 搜索替换 :%s/被替换的关键字/替换的内容/gi g表示全局替换i表示不区分大小写
- 只替换某一行 :3,12s/root/admin/g 表示替换3到 12 行所有 root 为 admin
- 显示行号 :set nu
- 关闭行号 :set nonu
- 取消高亮 :noh
9.2.9 删除文件
删除文件也是使用 rm 命令来完成。操作方式与删除目录一样只是在删除文件是不需要带 -r 选项。 [rootlocalhost ~]# rm -f aa a.txt
[rootlocalhost ~]# ls
date.txt error.txt file1 file11 file12 file13 file2 file3 file4 passwd success.txt
[rootlocalhost ~]# rm -f *.txt
[rootlocalhost ~]# ll
total 12
-rw-r--r--. 1 root root 48 Feb 21 15:39 file1
-rw-r--r--. 1 root root 0 Feb 21 11:48 file11
-rw-r--r--. 1 root root 0 Feb 21 11:48 file12
-rw-r--r--. 1 root root 0 Feb 21 11:48 file13
-rw-r--r--. 1 root root 0 Feb 21 11:48 file2
-rw-r--r--. 1 root root 0 Feb 21 11:48 file3
-rw-r--r--. 1 root root 0 Feb 21 11:48 file4
-rw-r--r--. 1 root root 4456 Feb 21 16:18 passwd 9.3 链接文件管理
在 Linux 中链接文件分为如下两种 软件链接文件它也叫符号链接 硬链接文件
创建链接文件的命令是 ln它的语法格式为 [rootlocalhost ~]# ln --help
Usage: ln [OPTION]... [-T] TARGET LINK_NAMEor: ln [OPTION]... TARGETor: ln [OPTION]... TARGET... DIRECTORYor: ln [OPTION]... -t DIRECTORY TARGET...
In the 1st form, create a link to TARGET with the name LINK_NAME.
In the 2nd form, create a link to TARGET in the current directory.
In the 3rd and 4th forms, create links to each TARGET in DIRECTORY.
Create hard links by default, symbolic links with --symbolic.
By default, each destination (name of new link) should not already exist.
When creating hard links, each TARGET must exist. Symbolic links
can hold arbitrary text; if later resolved, a relative link is
interpreted in relation to its parent directory.Mandatory arguments to long options are mandatory for short options too.--backup[CONTROL] make a backup of each existing destination file-b like --backup but does not accept an argument-d, -F, --directory allow the superuser to attempt to hard linkdirectories (note: will probably fail due tosystem restrictions, even for the superuser)-f, --force remove existing destination files-i, --interactive prompt whether to remove destinations-L, --logical dereference TARGETs that are symbolic links-n, --no-dereference treat LINK_NAME as a normal file ifit is a symbolic link to a directory-P, --physical make hard links directly to symbolic links-r, --relative create symbolic links relative to link location-s, --symbolic make symbolic links instead of hard links-S, --suffixSUFFIX override the usual backup suffix-t, --target-directoryDIRECTORY specify the DIRECTORY in which to createthe links-T, --no-target-directory treat LINK_NAME as a normal file always-v, --verbose print name of each linked file--help display this help and exit--version output version information and exit使用示例 # 我们为/root/passwd 文件创建一个符号链接名称为 passwdln
[rootlocalhost ~]# ln -s /root/passwd passwdln
[rootlocalhost ~]# ll
total 12
-rw-r--r--. 1 root root 48 Feb 21 15:39 file1
-rw-r--r--. 1 root root 0 Feb 21 11:48 file11
-rw-r--r--. 1 root root 0 Feb 21 11:48 file12
-rw-r--r--. 1 root root 0 Feb 21 11:48 file13
-rw-r--r--. 1 root root 0 Feb 21 11:48 file2
-rw-r--r--. 1 root root 0 Feb 21 11:48 file3
-rw-r--r--. 1 root root 0 Feb 21 11:48 file4
-rw-r--r--. 1 root root 4456 Feb 21 16:18 passwd
lrwxrwxrwx. 1 root root 12 Feb 21 16:23 passwdln - /root/passwd# 符号链接文件和原文件的inode 值不一样
[rootlocalhost ~]# ll -i
total 12
34763139 -rw-r--r--. 1 root root 48 Feb 21 15:39 file1
34762525 -rw-r--r--. 1 root root 0 Feb 21 11:48 file11
34762529 -rw-r--r--. 1 root root 0 Feb 21 11:48 file12
34762530 -rw-r--r--. 1 root root 0 Feb 21 11:48 file13
34762521 -rw-r--r--. 1 root root 0 Feb 21 11:48 file2
34762523 -rw-r--r--. 1 root root 0 Feb 21 11:48 file3
34762524 -rw-r--r--. 1 root root 0 Feb 21 11:48 file4
34762544 -rw-r--r--. 1 root root 4456 Feb 21 16:18 passwd
34762516 lrwxrwxrwx. 1 root root 12 Feb 21 16:23 passwdln - /root/passwd# 删除源文件后符号链接文件不可用
[rootlocalhost ~]# rm -f passwd
[rootlocalhost ~]# ll
total 4
-rw-r--r--. 1 root root 48 Feb 21 15:39 file1
-rw-r--r--. 1 root root 0 Feb 21 11:48 file11
-rw-r--r--. 1 root root 0 Feb 21 11:48 file12
-rw-r--r--. 1 root root 0 Feb 21 11:48 file13
-rw-r--r--. 1 root root 0 Feb 21 11:48 file2
-rw-r--r--. 1 root root 0 Feb 21 11:48 file3
-rw-r--r--. 1 root root 0 Feb 21 11:48 file4
lrwxrwxrwx. 1 root root 12 Feb 21 16:23 passwdln - /root/passwd
[rootlocalhost ~]# cat passwdln
cat: passwdln: No such file or directory 创建硬链接文件 [rootlocalhost ~]# ln /root/passwd passwd1
[rootlocalhost ~]# ll
total 12
-rw-r--r--. 1 root root 48 Feb 21 15:39 file1
-rw-r--r--. 1 root root 0 Feb 21 11:48 file11
-rw-r--r--. 1 root root 0 Feb 21 11:48 file12
-rw-r--r--. 1 root root 0 Feb 21 11:48 file13
-rw-r--r--. 1 root root 0 Feb 21 11:48 file2
-rw-r--r--. 1 root root 0 Feb 21 11:48 file3
-rw-r--r--. 1 root root 0 Feb 21 11:48 file4
-rw-r--r--. 2 root root 2224 Feb 21 16:26 passwd
-rw-r--r--. 2 root root 2224 Feb 21 16:26 passwd1
# 硬链接文件与源文件的 inode 值相同
[rootlocalhost ~]# ll -i
total 12
34763139 -rw-r--r--. 1 root root 48 Feb 21 15:39 file1
34762525 -rw-r--r--. 1 root root 0 Feb 21 11:48 file11
34762529 -rw-r--r--. 1 root root 0 Feb 21 11:48 file12
34762530 -rw-r--r--. 1 root root 0 Feb 21 11:48 file13
34762521 -rw-r--r--. 1 root root 0 Feb 21 11:48 file2
34762523 -rw-r--r--. 1 root root 0 Feb 21 11:48 file3
34762524 -rw-r--r--. 1 root root 0 Feb 21 11:48 file4
34762516 -rw-r--r--. 2 root root 2224 Feb 21 16:26 passwd
34762516 -rw-r--r--. 2 root root 2224 Feb 21 16:26 passwd1# 删除原文件链接文件也可以用只是链接数减少
[rootlocalhost ~]# rm -f passwd
[rootlocalhost ~]# ll
total 8
-rw-r--r--. 1 root root 48 Feb 21 15:39 file1
-rw-r--r--. 1 root root 0 Feb 21 11:48 file11
-rw-r--r--. 1 root root 0 Feb 21 11:48 file12
-rw-r--r--. 1 root root 0 Feb 21 11:48 file13
-rw-r--r--. 1 root root 0 Feb 21 11:48 file2
-rw-r--r--. 1 root root 0 Feb 21 11:48 file3
-rw-r--r--. 1 root root 0 Feb 21 11:48 file4
-rw-r--r--. 1 root root 2224 Feb 21 16:26 passwd1
[rootlocalhost ~]# cat passwd1 9.4 块设备管理
查看块设备所使用的命令是 lsblk。使用语法格式 [rootjock ~]# lsblk --helpUsage:lsblk [options] [device ...]List information about block devices.Options:-D, --discard print discard capabilities-E, --dedup column de-duplicate output by column-I, --include list show only devices with specified major numbers-J, --json use JSON output format-O, --output-all output all columns-P, --pairs use keyvalue output format-S, --scsi output info about SCSI devices-T, --tree[column] use tree format output-a, --all print all devices-b, --bytes print SIZE in bytes rather than in human readable format-d, --nodeps dont print slaves or holders-e, --exclude list exclude devices by major number (default: RAM disks)-f, --fs output info about filesystems-i, --ascii use ascii characters only-l, --list use list format output-M, --merge group parents of sub-trees (usable for RAIDs, Multi-path)-m, --perms output info about permissions-n, --noheadings dont print headings-o, --output list output columns-p, --paths print complete device path-r, --raw use raw output format-s, --inverse inverse dependencies-t, --topology output info about topology-w, --width num specifies output width as number of characters-x, --sort column sort output by column-z, --zoned print zone model--sysroot dir use specified directory as system root-h, --help display this help-V, --version display version使用示例 [rootjock ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 8.9G 0 rom
nvme0n1 259:0 0 50G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 49G 0 part ├─rhel_bogon-root 253:0 0 44G 0 lvm /└─rhel_bogon-swap 253:1 0 5G 0 lvm [SWAP][rootjock ~]# lsblk /dev/sr0
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 8.9G 0 rom 9.5 复制和移动文件
9.5.1 复制文件
复制文件有两种方式 本地文件复制cp 远程文件复制scp
9.5.1.1本地复制
使用的命令是 cp copy它的使用语法格式为 [rootjock ~]# cp --help
Usage: cp [OPTION]... [-T] SOURCE DESTor: cp [OPTION]... SOURCE... DIRECTORYor: cp [OPTION]... -t DIRECTORY SOURCE...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.Mandatory arguments to long options are mandatory for short options too.-a, --archive same as -dR --preserveall--attributes-only dont copy the file data, just the attributes--backup[CONTROL] make a backup of each existing destination file-b like --backup but does not accept an argument--copy-contents copy contents of special files when recursive-d same as --no-dereference --preservelinks-f, --force if an existing destination file cannot beopened, remove it and try again (this optionis ignored when the -n option is also used)-i, --interactive prompt before overwrite (overrides a previous -noption)-H follow command-line symbolic links in SOURCE-l, --link hard link files instead of copying-L, --dereference always follow symbolic links in SOURCE-n, --no-clobber do not overwrite an existing file (overridesa previous -i option)-P, --no-dereference never follow symbolic links in SOURCE-p same as --preservemode,ownership,timestamps--preserve[ATTR_LIST] preserve the specified attributes (default:mode,ownership,timestamps), if possibleadditional attributes: context, links, xattr,all-c deprecated, same as --preservecontext--no-preserveATTR_LIST dont preserve the specified attributes--parents use full source file name under DIRECTORY-R, -r, --recursive copy directories recursively--reflink[WHEN] control clone/CoW copies. See below--remove-destination remove each existing destination file beforeattempting to open it (contrast with --force)--sparseWHEN control creation of sparse files. See below--strip-trailing-slashes remove any trailing slashes from each SOURCEargument-s, --symbolic-link make symbolic links instead of copying-S, --suffixSUFFIX override the usual backup suffix-t, --target-directoryDIRECTORY copy all SOURCE arguments into DIRECTORY-T, --no-target-directory treat DEST as a normal file-u, --update copy only when the SOURCE file is newerthan the destination file or when thedestination file is missing-v, --verbose explain what is being done-x, --one-file-system stay on this file system-Z set SELinux security context of destinationfile to default type--context[CTX] like -Z, or if CTX is specified then set theSELinux or SMACK security context to CTX--help display this help and exit--version output version information and exit在工作中常用的选项有如下
-a 通过在拷贝目录时使用它会保留链接、文件属性
-d 拷贝时保留链接
-f 当复制文件时如果已经存在则强制覆盖而不会有提示信息
-i 当复制文件时如果已经存在时给出提示信息
-p 除复制源文件的内容外还会将其修改的时间和访问权限也复制到新的文件中
-r 递归复制目录中所有内容
使用示例 # 复制普通文件
[rootjock ~]# mkdir dir1
[rootjock ~]# ll
total 8
drwxr-xr-x. 2 root root 6 Feb 22 09:44 dir1
-rw-r--r--. 1 root root 48 Feb 21 15:39 file1
-rw-r--r--. 1 root root 0 Feb 21 11:48 file11
-rw-r--r--. 1 root root 0 Feb 21 11:48 file12
-rw-r--r--. 1 root root 0 Feb 21 11:48 file13
-rw-r--r--. 1 root root 0 Feb 21 11:48 file2
-rw-r--r--. 1 root root 0 Feb 21 11:48 file3
-rw-r--r--. 1 root root 0 Feb 21 11:48 file4
-rw-r--r--. 1 root root 2224 Feb 21 16:26 passwd1
[rootjock ~]# cp file1 dir1/file1
[rootjock ~]# ll dir1/file1
-rw-r--r--. 1 root root 48 Feb 22 09:44 dir1/file1# 复制目录
[rootjock ~]# cp -ar dir1 dir2
[rootjock ~]# ll
total 8
drwxr-xr-x. 2 root root 19 Feb 22 09:44 dir1
drwxr-xr-x. 2 root root 19 Feb 22 09:44 dir2
-rw-r--r--. 1 root root 48 Feb 21 15:39 file1
-rw-r--r--. 1 root root 0 Feb 21 11:48 file11
-rw-r--r--. 1 root root 0 Feb 21 11:48 file12
-rw-r--r--. 1 root root 0 Feb 21 11:48 file13
-rw-r--r--. 1 root root 0 Feb 21 11:48 file2
-rw-r--r--. 1 root root 0 Feb 21 11:48 file3
-rw-r--r--. 1 root root 0 Feb 21 11:48 file4
-rw-r--r--. 1 root root 2224 Feb 21 16:26 passwd1 9.5.1.2 远程复制
要实现这个功能我们需要使用 scp 命令它的语法格式如下 [rootjock ~]# scp --help
usage: scp [-346ABCOpqRrTv] [-c cipher] [-D sftp_server_path] [-F ssh_config][-i identity_file] [-J destination] [-l limit][-o ssh_option] [-P port] [-S program] source ... targetscp 源文件路径 目标文件路径 使用示例 [rootjock ~]# scp root192.168.72.120:/root/file1 redhat192.168.72.120:/home/redhat/
redhat192.168.72.120s password:
root192.168.72.120s password:
file1 100% 48 29.4KB/s 00:00
[rootjock ~]# 命令的使用解析
1. scp 表示远程拷贝的命令
2. root192.168.72.120:/root/file1 表示要拷贝的文件路径它由以下几个部分组成2.1 root 表示当前登录的用户2.2 192.168.72.120 表示当前登录的主机名它和用户名之间使用 符号来连接2.3 :/root/file1 表示要复制的文件的绝对路径它和主机名之间有一个冒号
3. redhat192.168.72.120:/home/redhat/ 表示要拷贝文件的目标路径它的组成与第2部分相同
如果端口不一样我们就需要使用 -P 选项来指定端口 scp -P 6666 root192.168.72.120:/root/file1 redhat192.168.72.120:/home/redhat/ 9.5.2 移动文件
移动文件我们使用 mv 命令它的语法格式为 [rootjock ~]# mv --help
Usage: mv [OPTION]... [-T] SOURCE DESTor: mv [OPTION]... SOURCE... DIRECTORYor: mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.Mandatory arguments to long options are mandatory for short options too.--backup[CONTROL] make a backup of each existing destination file-b like --backup but does not accept an argument-f, --force do not prompt before overwriting-i, --interactive prompt before overwrite-n, --no-clobber do not overwrite an existing file
If you specify more than one of -i, -f, -n, only the final one takes effect.--strip-trailing-slashes remove any trailing slashes from each SOURCEargument-S, --suffixSUFFIX override the usual backup suffix-t, --target-directoryDIRECTORY move all SOURCE arguments into DIRECTORY-T, --no-target-directory treat DEST as a normal file-u, --update move only when the SOURCE file is newerthan the destination file or when thedestination file is missing-v, --verbose explain what is being done-Z, --context set SELinux security context of destinationfile to default type--help display this help and exit--version output version information and exit使用示例 # 移动文件
[rootjock ~]# mv file2 dir2
[rootjock ~]# ll dir2
total 4
drwxr-xr-x. 2 root root 19 Feb 22 09:44 dir1
-rw-r--r--. 1 root root 48 Feb 22 09:44 file1
-rw-r--r--. 1 root root 0 Feb 21 11:48 file2# 移动文件并重命名
[rootjock ~]# mv file3 dir2/file33
[rootjock ~]# ll dir2
total 4
drwxr-xr-x. 2 root root 19 Feb 22 09:44 dir1
-rw-r--r--. 1 root root 48 Feb 22 09:44 file1
-rw-r--r--. 1 root root 0 Feb 21 11:48 file2
-rw-r--r--. 1 root root 0 Feb 21 11:48 file33# 对文件得命名
[rootjock ~]# mv file4 file
[rootjock ~]# ll
total 8
drwxr-xr-x. 2 root root 19 Feb 22 09:44 dir1
drwxr-xr-x. 3 root root 58 Feb 22 10:05 dir2
drwxr-xr-x. 3 root root 31 Feb 22 09:46 dir3
-rw-r--r--. 1 root root 0 Feb 21 11:48 file
-rw-r--r--. 1 root root 48 Feb 21 15:39 file1
-rw-r--r--. 1 root root 0 Feb 21 11:48 file11
-rw-r--r--. 1 root root 0 Feb 21 11:48 file12
-rw-r--r--. 1 root root 0 Feb 21 11:48 file13
-rw-r--r--. 1 root root 2224 Feb 21 16:26 passwd1# 也可以对目录进行操作
[rootjock ~]# mv dir3 dir
[rootjock ~]# ls
dir dir1 dir2 file file1 file11 file12 file13 passwd1 9.6 查找文件
9.6.1 whereis
这个命令是用于定位某个“命令”的二进制文件、源文件和手册文件的路径。 [rootjock ~]# whereis --helpUsage:whereis [options] [-BMS dir... -f] nameLocate the binary, source, and manual-page files for a command.Options:-b search only for binaries-B dirs define binaries lookup path-m search only for manuals and infos-M dirs define man and info lookup path-s search only for sources-S dirs define sources lookup path-f terminate dirs argument list-u search for unusual entries-l output effective lookup paths-h, --help display this help-V, --version display version使用示例 [rootjock ~]# whereis cp
cp: /usr/bin/cp /usr/share/man/man1/cp.1.gz /usr/share/man/man1p/cp.1p.gz
[rootjock ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[rootjock ~]# whereis cd
cd: /usr/bin/cd /usr/share/man/man1/cd.1.gz /usr/share/man/man1p/cd.1p.gz 9.6.2 which
用于查找并显示给定的“命令”的绝对路径它会在环境变量 $PATH 设置的目录里查找符合条件的文件。 [rootjock ~]# which --help
Usage: /usr/bin/which [options] [--] COMMAND [...]
Write the full path of COMMAND(s) to standard output.--version, -[vV] Print version and exit successfully.--help, Print this help and exit successfully.--skip-dot Skip directories in PATH that start with a dot.--skip-tilde Skip directories in PATH that start with a tilde.--show-dot Dont expand a dot to current directory in output.--show-tilde Output a tilde for HOME directory for non-root.--tty-only Stop processing options on the right if not on tty.--all, -a Print all matches in PATH, not just the first--read-alias, -i Read list of aliases from stdin.--skip-alias Ignore option --read-alias; dont read stdin.--read-functions Read shell functions from stdin.--skip-functions Ignore option --read-functions; dont read stdin.使用示例 [rootjock ~]# echo $PATH
/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin[rootjock ~]# which cd
/usr/bin/cd
[rootjock ~]# which ls
alias lsls --colorauto/usr/bin/ls
[rootjock ~]# which find
/usr/bin/find 9.6.3 locate
这个命令是在系统中按照文件名称查找符合条件的文件当执行搜索时它会通过搜索 /var/lib/mlocate/mlocate.db 这个数据库文件中进行查找。 [rootjock mlocate]# locate --help
Usage: locate [OPTION]... [PATTERN]...
Search for entries in a mlocate database.-A, --all only print entries that match all patterns-b, --basename match only the base name of path names-c, --count only print number of found entries-d, --database DBPATH use DBPATH instead of default database (which is/var/lib/mlocate/mlocate.db)-e, --existing only print entries for currently existing files-L, --follow follow trailing symbolic links when checking fileexistence (default)-h, --help print this help-i, --ignore-case ignore case distinctions when matching patterns-l, --limit, -n LIMIT limit output (or counting) to LIMIT entries-m, --mmap ignored, for backward compatibility-P, --nofollow, -H dont follow trailing symbolic links when checking fileexistence-0, --null separate entries with NUL on output-S, --statistics dont search for entries, print statistics about eachused database-q, --quiet report no error messages about reading databases-r, --regexp REGEXP search for basic regexp REGEXP instead of patterns--regex patterns are extended regexps-s, --stdio ignored, for backward compatibility-V, --version print version information-w, --wholename match whole path name (default)使用示例 [rootjock ~]# cd /var/lib/mlocate/
[rootjock mlocate]# ll
total 3412
-rw-r-----. 1 root slocate 3491805 Feb 22 08:58 mlocate.db# 要使用这个命令之前我们最好更新一个这个数据库文件
[rootjock ~]# updatedb[rootjock ~]# locate file2
/root/dir2/file2[rootjock ~]# locate -i *.conf# 限制显示的条数
[rootjock ~]# locate -l 3 *.conf
/boot/loader/entries/5c5bb57619564f628a3f3f51cb63f773-0-rescue.conf
/boot/loader/entries/5c5bb57619564f628a3f3f51cb63f773-5.14.0-284.11.1.el9_2.x86_64.conf
/etc/anthy-unicode.conf 9.6.4 find
这个命令是用于通过条件匹配在指定目录下查找对应文件或目录。可以按文件大小、时间、权限、类型以及属者/组来搜索。 [rootjock ~]# find --help
Usage: find [-H] [-L] [-P] [-Olevel] [-D debugopts] [path...] [expression]default path is the current directory; default expression is -print
expression may consist of: operators, options, tests, and actions:
operators (decreasing precedence; -and is implicit where no others are given):( EXPR ) ! EXPR -not EXPR EXPR1 -a EXPR2 EXPR1 -and EXPR2EXPR1 -o EXPR2 EXPR1 -or EXPR2 EXPR1 , EXPR2
positional options (always true): -daystart -follow -regextypenormal options (always true, specified before other expressions):-depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf--version -xautofs -xdev -ignore_readdir_race -noignore_readdir_race
tests (N can be N or -N or N): -amin N -anewer FILE -atime N -cmin N-cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME-ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN-links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE-nouser -nogroup -path PATTERN -perm [-/]MODE -regex PATTERN-readable -writable -executable-wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N-used N -user NAME -xtype [bcdpfls] -context CONTEXTactions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print -fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit-exec COMMAND ; -exec COMMAND {} -ok COMMAND ;-execdir COMMAND ; -execdir COMMAND {} -okdir COMMAND ;Valid arguments for -D:
exec, opt, rates, search, stat, time, tree, all, help
Use -D help for a description of the options, or see find(1) 在工作中常用的选项有如下
1. -name根据文件名称来进行搜索
2. -path根据文件路径来进行搜索
3. -size根据文件大小来进行搜索50K表示超过50K的文件-50K表示小于50K的文件
4. -mtime[|-]n 表示匹配修改内容的时间
5. -newer f1 !f2匹配比文件f1新但比f2旧的文件
6. -perm匹配权限
7. -user匹配所属者
8. -group匹配所属组
9. -type匹配文件类型
使用示例 # 查找以 .txt 结尾的文件
[rootjock ~]# find / -name *.txt# 可以指定查找的最大深度为2
[rootjock ~]# find / -maxdepth 2 -name *.txt 9.7 压缩
zip、unzip、gzip、gunzip、bzip2、bunzip2、xz、unxz、tar # zip unzip
[rootjock ~]# mkdir test
[rootjock ~]# mv file file1 file11 test/
[rootjock ~]# ll
total 4
drwxr-xr-x. 3 root root 31 Feb 22 09:46 dir
drwxr-xr-x. 2 root root 19 Feb 22 09:44 dir1
drwxr-xr-x. 3 root root 58 Feb 22 10:05 dir2
-rw-r--r--. 1 root root 0 Feb 21 11:48 file12
-rw-r--r--. 1 root root 0 Feb 21 11:48 file13
-rw-r--r--. 1 root root 2224 Feb 21 16:26 passwd1
drwxr-xr-x. 2 root root 45 Feb 22 10:53 test
[rootjock ~]# ls test
file file1 file11
[rootjock ~]# zip test.zip test/adding: test/ (stored 0%)
[rootjock ~]# ll
total 8
drwxr-xr-x. 3 root root 31 Feb 22 09:46 dir
drwxr-xr-x. 2 root root 19 Feb 22 09:44 dir1
drwxr-xr-x. 3 root root 58 Feb 22 10:05 dir2
-rw-r--r--. 1 root root 0 Feb 21 11:48 file12
-rw-r--r--. 1 root root 0 Feb 21 11:48 file13
-rw-r--r--. 1 root root 2224 Feb 21 16:26 passwd1
drwxr-xr-x. 2 root root 45 Feb 22 10:53 test
-rw-r--r--. 1 root root 160 Feb 22 10:54 test.zip[rootjock ~]# unzip -v test.zip
Archive: test.zipLength Method Size Cmpr Date Time CRC-32 Name
-------- ------ ------- ---- ---------- ----- -------- ----0 Stored 0 0% 02-22-2024 10:53 00000000 test/
-------- ------- --- -------0 0 0% 1 file# gzip gunzip
[rootjock ~]# ll
total 8
drwxr-xr-x. 3 root root 31 Feb 22 09:46 dir
drwxr-xr-x. 2 root root 19 Feb 22 09:44 dir1
drwxr-xr-x. 3 root root 58 Feb 22 10:05 dir2
-rw-r--r--. 1 root root 0 Feb 21 11:48 file12
-rw-r--r--. 1 root root 0 Feb 21 11:48 file13
-rw-r--r--. 1 root root 2224 Feb 21 16:26 passwd1
drwxr-xr-x. 2 root root 45 Feb 22 10:53 test
-rw-r--r--. 1 root root 160 Feb 22 10:54 test.zip
[rootjock ~]# gzip file12
[rootjock ~]# ll
total 12
drwxr-xr-x. 3 root root 31 Feb 22 09:46 dir
drwxr-xr-x. 2 root root 19 Feb 22 09:44 dir1
drwxr-xr-x. 3 root root 58 Feb 22 10:05 dir2
-rw-r--r--. 1 root root 27 Feb 21 11:48 file12.gz
-rw-r--r--. 1 root root 0 Feb 21 11:48 file13
-rw-r--r--. 1 root root 2224 Feb 21 16:26 passwd1
drwxr-xr-x. 2 root root 45 Feb 22 10:53 test
-rw-r--r--. 1 root root 160 Feb 22 10:54 test.zip
[rootjock ~]# gunzip file12.gz
[rootjock ~]# ll
total 8
drwxr-xr-x. 3 root root 31 Feb 22 09:46 dir
drwxr-xr-x. 2 root root 19 Feb 22 09:44 dir1
drwxr-xr-x. 3 root root 58 Feb 22 10:05 dir2
-rw-r--r--. 1 root root 0 Feb 21 11:48 file12
-rw-r--r--. 1 root root 0 Feb 21 11:48 file13
-rw-r--r--. 1 root root 2224 Feb 21 16:26 passwd1
drwxr-xr-x. 2 root root 45 Feb 22 10:53 test
-rw-r--r--. 1 root root 160 Feb 22 10:54 test.zip# bzip2 bunzip2
[rootjock ~]# ll
total 8
drwxr-xr-x. 3 root root 31 Feb 22 09:46 dir
drwxr-xr-x. 2 root root 19 Feb 22 09:44 dir1
drwxr-xr-x. 3 root root 58 Feb 22 10:05 dir2
-rw-r--r--. 1 root root 0 Feb 21 11:48 file12
-rw-r--r--. 1 root root 0 Feb 21 11:48 file13
-rw-r--r--. 1 root root 2224 Feb 21 16:26 passwd1
drwxr-xr-x. 2 root root 45 Feb 22 10:53 test
-rw-r--r--. 1 root root 160 Feb 22 10:54 test.zip
[rootjock ~]# bzip2 file13
[rootjock ~]# ll
total 12
drwxr-xr-x. 3 root root 31 Feb 22 09:46 dir
drwxr-xr-x. 2 root root 19 Feb 22 09:44 dir1
drwxr-xr-x. 3 root root 58 Feb 22 10:05 dir2
-rw-r--r--. 1 root root 0 Feb 21 11:48 file12
-rw-r--r--. 1 root root 14 Feb 21 11:48 file13.bz2
-rw-r--r--. 1 root root 2224 Feb 21 16:26 passwd1
drwxr-xr-x. 2 root root 45 Feb 22 10:53 test
-rw-r--r--. 1 root root 160 Feb 22 10:54 test.zip
[rootjock ~]# bunzip2 file13.bz2
[rootjock ~]# ll
total 8
drwxr-xr-x. 3 root root 31 Feb 22 09:46 dir
drwxr-xr-x. 2 root root 19 Feb 22 09:44 dir1
drwxr-xr-x. 3 root root 58 Feb 22 10:05 dir2
-rw-r--r--. 1 root root 0 Feb 21 11:48 file12
-rw-r--r--. 1 root root 0 Feb 21 11:48 file13
-rw-r--r--. 1 root root 2224 Feb 21 16:26 passwd1
drwxr-xr-x. 2 root root 45 Feb 22 10:53 test
-rw-r--r--. 1 root root 160 Feb 22 10:54 test.zip# tar
[rootjock ~]#
[rootjock ~]# tar -cvf hah.tar file12 file13
file12
file13
[rootjock ~]# ls
dir dir1 dir2 file12 file13 hah.tar passwd1 test test.zip
[rootjock ~]# tar -xvf hah.tar -C dir2/
file12
file1310. 用户和组
10.1 分类
在 linux 中用户分为三类 超级用户0 系统用户1~999 普通用户1000~65535
这些信息是保存在 /etc/login.defs 文件中的。
对于组来说也分为以下两类 基本组也叫私有组它只能有一个 附加组也叫公有组它是可以有多个的多个之间用逗号分隔
对于用户和组来说它们的信息是存储在如下的文件中的 用户账号信息/etc/passwd 用户的密码/etc/shadow 用户组信息/etc/group 用户组密码/etc/gshadow 家目录下的隐藏文件/etc/skel 用户默认信息/etc/default/useradd
对 /etc/passwd 文件内容说明 [rootjock ~]# vim /etc/passwd
root:x:0:0:root:/root:/bin/bash账号名称:密码占位符:uid:gid:个人信息:家目录:可执行的shell 对 /etc/shadow 文件内容说明 [rootjock ~]# vim /etc/shadow
root:$6$w.4NX9m.wQ/s9qV7$x4a39kSlTgm5jITx9RV46nNib1c/d3Qn3nRc69g8KaIJR3Qkl65E1S3yDV2rczu71tS.uLwJRQf3dj7t8w3a01::0:99999:7:::用户名称:加密密码:最后一次修改时间:最小时间间隔:最大时间间隔:警告时间:不活动时间宽限期:失效时间:标志 对 /etc/group 文件内容说明 [rootjock ~]# vim /etc/group
root:x:0:用户名:组密码占位符:gid:附加组信息 对 /etc/gshadow 文件内容说明 [rootjock ~]# vim /etc/gshadow
root:::用户名:组密码:组管理员的用户名:组中附加用户 10.2 用户管理 # 添加用户
[rootjock ~]# useradd zhangsan
[rootjock ~]# id zhangsan
uid1001(zhangsan) gid1001(zhangsan) groups1001(zhangsan)# 给创建好的用户设置密码
[rootjock ~]# passwd zhangsan
Changing password for user zhangsan.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.# 使用下面的方式来设置密码也是可以的
[rootjock ~]# echo redhat | passwd --stdin zhangsan# 删除用户
[rootjock ~]# userdel -r zhangsan 10.3 组管理 # 创建组
[rootjock ~]# groupadd lisi
[rootjock ~]# useradd xiaowang -G lisi
[rootjock ~]# id xiaowang
uid1001(xiaowang) gid1002(xiaowang) groups1002(xiaowang),1001(lisi)# 修改组
[rootjock ~]# groupmod -n haha lisi# 删除组
[rootjock ~]# groupdel lisi
groupdel: group lisi does not exist
[rootjock ~]# groupdel haha10.4 查看用户 # 查看当前登录用户
[rootjock ~]# users
root# 列表当前与过去登录的系统用户信息
[rootjock ~]# last
root pts/1 192.168.72.1 Thu Feb 22 11:15 still logged in
redhat pts/1 192.168.72.120 Thu Feb 22 10:03 - 10:04 (00:00)
root pts/0 192.168.72.1 Thu Feb 22 08:58 - 11:15 (02:16)
reboot system boot 5.14.0-284.11.1. Thu Feb 22 08:58 still running
root pts/1 192.168.72.1 Wed Feb 21 14:13 - 14:24 (00:11)
root pts/0 192.168.72.1 Wed Feb 21 09:42 - 16:30 (06:48)
reboot system boot 5.14.0-284.11.1. Wed Feb 21 09:41 - 16:30 (06:49)
root pts/0 192.168.72.1 Mon Feb 19 10:17 - 11:49 (01:32)
root pts/0 192.168.72.1 Mon Feb 19 09:16 - 09:19 (00:03)
root tty2 tty2 Mon Feb 19 09:15 - down (02:34)
root seat0 login screen Mon Feb 19 09:15 - down (02:34)
reboot system boot 5.14.0-284.11.1. Mon Feb 19 09:15 - 11:49 (02:34)
root pts/0 192.168.72.1 Mon Feb 19 09:10 - 09:15 (00:04)
root tty2 tty2 Mon Feb 19 09:08 - down (00:07)
root seat0 login screen Mon Feb 19 09:08 - down (00:07)
reboot system boot 5.14.0-284.11.1. Mon Feb 19 09:05 - 09:15 (00:09)
root pts/0 192.168.72.1 Thu Jan 25 14:31 - 16:28 (01:56)
reboot system boot 5.14.0-284.11.1. Thu Jan 25 14:31 - 16:28 (01:57)
redhat tty2 tty2 Sun Oct 15 19:53 - down (00:02)
redhat seat0 login screen Sun Oct 15 19:53 - down (00:02)
reboot system boot 5.14.0-284.11.1. Sun Oct 15 19:45 - 19:55 (00:10)wtmp begins Sun Oct 15 19:45:11 2023# 查看账号用户信息
[rootjock ~]# w11:37:42 up 2:39, 1 user, load average: 0.00, 0.00, 0.00
USER TTY LOGIN IDLE JCPU PCPU WHAT
root pts/1 11:15 0.00s 0.09s 0.01s w[rootjock ~]# who
root pts/1 2024-02-22 11:15 (192.168.72.1)11. 文件系统权限
11.1 普通权限 [rootjock ~]# ls -ltotal 20drwxr-xr-x. 3 root root 31 Feb 22 09:46 dirdrwxr-xr-x. 2 root root 19 Feb 22 09:44 dir1drwxr-xr-x. 3 root root 86 Feb 22 11:04 dir2-rw-r--r--. 1 root root 0 Feb 21 11:48 file12-rw-r--r--. 1 root root 0 Feb 21 11:48 file13-rw-r--r--. 1 root root 10240 Feb 22 11:03 hah.tar-rw-r--r--. 1 root root 2224 Feb 21 16:26 passwd1drwxr-xr-x. 2 root root 45 Feb 22 10:53 test-rw-r--r--. 1 root root 160 Feb 22 10:54 test.ziprwx r-x r-x所属用户权限(u) 所属组权限(g) 其他用户权限(o)- 表示没有权限 对应的数值 0x 表示可执行权限 对应的数值 1w 表示可写权限 对应的数值 2r 表示可读权限 对应的数值 4--- 0--x 1-w- 2-wx 3r-- 4r-x 5rw- 6rwx 7 要修改文件或目录的权限我们需要使用 chmod 命令这个命令的格式为 [rootjock ~]# chmod --help
Usage: chmod [OPTION]... MODE[,MODE]... FILE...or: chmod [OPTION]... OCTAL-MODE FILE...or: chmod [OPTION]... --referenceRFILE FILE...
Change the mode of each FILE to MODE.
With --reference, change the mode of each FILE to that of RFILE.-c, --changes like verbose but report only when a change is made-f, --silent, --quiet suppress most error messages-v, --verbose output a diagnostic for every file processed--no-preserve-root do not treat / specially (the default)--preserve-root fail to operate recursively on /--referenceRFILE use RFILEs mode instead of MODE values-R, --recursive change files and directories recursively--help display this help and exit--version output version information and exitEach MODE is of the form [ugoa]*([-]([rwxXst]*|[ugo]))|[-][0-7].ugoa 表示设置权限会针对所有的即包括所属者(u)、所属组(g)以及其他用户(o) a ugo- 表示设置权限的动作 表示添加某个权限- 表示取消某个权限 表示只赋予给定的权限rwxXst 表示用字符形式来设置权限0-7 表示用三位数字来设置权限 r4 w2 x1 -0 使用示例 [rootjock ~]# ll
total 20
drwxr-xr-x. 3 root root 31 Feb 22 09:46 dir
drwxr-xr-x. 2 root root 45 Feb 22 10:53 test
# 给所属者取消w权限
[rootjock ~]# chmod u-w test
[rootjock ~]# ll
total 20
drwxr-xr-x. 3 root root 31 Feb 22 09:46 dir
dr-xr-xr-x. 2 root root 45 Feb 22 10:53 test# 给所属组添加w权限
[rootjock ~]# chmod gw test
[rootjock ~]# ll
total 20
drwxr-xr-x. 3 root root 31 Feb 22 09:46 dir
dr-xrwxr-x. 2 root root 45 Feb 22 10:53 test# 给所属者取消x权限所属组赋予r权限其他用户添加w权限
[rootjock ~]# chmod u-x,gr,ow test
[rootjock ~]# ll
total 20
drwxr-xr-x. 3 root root 31 Feb 22 09:46 dir
-rw-r--r--. 1 root root 10240 Feb 22 11:03 hah.tar# 使用数字的形式来设置权限
[rootjock ~]# ll -d test
dr--r--rwx. 2 root root 45 Feb 22 10:53 test
[rootjock ~]# chmod 644 test
[rootjock ~]# ll -d test
drw-r--r--. 2 root root 45 Feb 22 10:53 test 11.2 特殊权限
11.2.1 SUID权限
SUID 特殊权限仅适用于可执行文件所具有的功能是只要用户对设有 SUID 的文件有执行权限那么当用户执行此文件时会以文件所有者的身份去执行此文件一旦文件执行结束身份的切换也随之消失。 [rootjock ~]# which passwd/usr/bin/passwd[rootjock ~]# ll /usr/bin/passwd-rwsr-xr-x. 1 root root 32648 Aug 10 2021 /usr/bin/passwd[rootjock ~]# chmod u-s /usr/bin/passwd[rootjock ~]# ll /usr/bin/passwd-rwxr-xr-x. 1 root root 32648 Aug 10 2021 /usr/bin/passwd[rootjock ~]# su - redhat[redhatjock ~]$ passwdChanging password for user redhat.Current password: Current Password: passwd: Authentication token manipulation error 11.2.2 SGID权限
与 SUID 不同的是SGID 既可以对文件进行配置也可以对目录进行配置。 对于文件SGID 只针对可执行文件有效换句话说只有可执行文件才可以被赋予 SGID 权限普通文件赋予 SGID 没有意义。同时用户需要对此可执行文件有 x 权限。用户在执行具有 SGID 权限的可执行文件时用户的群组身份会变为文件所属群组。SGID 权限赋予用户改变组身份的效果只在可执行文件运行过程中有效。 其实SGID 和 SUID 的不同之处就在于SUID 赋予用户的是文件所有者的权限而 SGID 赋予用户的是文件所属组的权限就这么简单。 对于目录当一个目录被赋予 SGID 权限后进入此目录的普通用户其有效群组会变为该目录的所属组这就使得用户在创建文件或目录时该文件或目录的所属组将不再是用户的所属组而使用的是目录的所属组。 也就是说只有当普通用户对具有 SGID 权限的目录有 rwx 权限时SGID 的功能才能完全发挥。比如说如果用户对该目录仅有 rx 权限则用户进入此目录后虽然其有效群组变为此目录的所属组但由于没有 x 权限用户无法在目录中创建文件或目录SGID 权限也就无法发挥它的作用。 [rootjock ~]# cd /tmp
[rootjock tmp]# mkdir dtest
[rootjock tmp]# ll
total 0
srwxrwxrwx. 1 gdm gdm 0 Feb 22 08:58 dbus-gSR0hvnLMy
drwxr-xr-x. 2 root root 6 Feb 22 14:03 dtest
[rootjock tmp]# chmod gs dtest
[rootjock tmp]# ll
total 0
srwxrwxrwx. 1 gdm gdm 0 Feb 22 08:58 dbus-gSR0hvnLMy
drwxr-sr-x. 2 root root 6 Feb 22 14:03 dtest[rootjock tmp]# chmod gw,os dtest
[rootjock tmp]# ll -d test
ls: cannot access test: No such file or directory
[rootjock tmp]# ll -d dtest
drwxrwsr-x. 2 root root 6 Feb 22 14:03 dtest
[rootjock tmp]# chmod ow dtest
[rootjock tmp]# ll -d dtest
drwxrwsrwx. 2 root root 6 Feb 22 14:03 dtest
[rootjock tmp]# su - redhat
[redhatjock ~]$ cd /tmp/dtest
[redhatjock dtest]$ touch ab
[redhatjock dtest]$ ll
total 0
-rw-r--r--. 1 redhat root 0 Feb 22 14:08 ab
[redhatjock dtest]$ mkdir haha
[redhatjock dtest]$ ll
total 0
-rw-r--r--. 1 redhat root 0 Feb 22 14:08 ab
drwxr-sr-x. 2 redhat root 6 Feb 22 14:09 haha 11.2.3 SBIT权限
Sticky Bit (SBit) 当前只针对目录有效 对文件没有效果。其对目录的作用是在具有 SBit 的目录下 用户若在该目录下具有 w 及 x 权限 则当用户在该目录下建立文件或目录时 只有文件拥有者与 root 才有权力删除。
也就是说当甲用户以目录所属组或其他人的身份进入 A 目录时如果甲对该目录有 w 权限则表示对于 A 目录中任何用户创建的文件或子目录甲都可以进行修改甚至删除等操作。但是如果 A 目录设定有 SBIT 权限那就大不一样啦甲用户只能操作自己创建的文件或目录而无法修改甚至删除其他用户创建的文件或目录。 [redhatjock tmp]$ ll -d /tmp
drwxrwxrwt. 19 root root 4096 Feb 22 14:09 /tmp# 创建 data 目录
[rootjock /]# mkdir /data[rootjock /]# ll -d /data
drwxr-xr-x. 2 root root 6 Feb 22 14:14 /data
# 设置可写可执行权限
[rootjock /]# chmod 777 /data
[rootjock /]# ll -d /data
drwxrwxrwx. 2 root root 6 Feb 22 14:14 /data# 创建两个用户
[rootjock /]# useradd tmp1
[rootjock /]# useradd tmp2# 切换用户并创建文件
[rootjock /]# su - tmp1
[tmp1jock ~]$ cd /data
[tmp1jock data]$ touch a
[tmp1jock data]$ touch b
[tmp1jock data]$ mkdir c
[tmp1jock data]$ ll
total 0
-rw-r--r--. 1 tmp1 tmp1 0 Feb 22 14:16 a
-rw-r--r--. 1 tmp1 tmp1 0 Feb 22 14:16 b
drwxr-xr-x. 2 tmp1 tmp1 6 Feb 22 14:16 c# 再切换到另一个用户
[rootjock /]# su - tmp2
[tmp2jock ~]$ cd /data
[tmp2jock data]$ ll
total 0
-rw-r--r--. 1 tmp1 tmp1 0 Feb 22 14:16 a
-rw-r--r--. 1 tmp1 tmp1 0 Feb 22 14:16 b
drwxr-xr-x. 2 tmp1 tmp1 6 Feb 22 14:16 c
[tmp2jock data]$ rm -f b
[tmp2jock data]$ ll
total 0
-rw-r--r--. 1 tmp1 tmp1 0 Feb 22 14:16 a
drwxr-xr-x. 2 tmp1 tmp1 6 Feb 22 14:16 c# 发现 tmp2 用户是可以删除 tmp1 用户所创建的文件的。但是我们不希望 tmp2 用户能够删除 tmp1 用户所创建的文件如何实现
# 这时就需要给 /data 目录设置 sbit 权限。
[rootjock /]# chmod ot /data
[rootjock /]# ll -d /data
drwxrwxrwt. 3 root root 24 Feb 22 14:17 /data# 两次切换用户然后进行删除发现就不能删除别人创建的文件了只能删除自己创建的文件
[rootjock /]# su - tmp2
[tmp2jock ~]$ cd /data
[tmp2jock data]$ ll
total 0
-rw-r--r--. 1 tmp1 tmp1 0 Feb 22 14:16 a
drwxr-xr-x. 2 tmp1 tmp1 6 Feb 22 14:16 c
[tmp2jock data]$ rm -f a
rm: cannot remove a: Operation not permitted
[tmp2jock data]$ touch dd
[tmp2jock data]$ ll
total 0
-rw-r--r--. 1 tmp1 tmp1 0 Feb 22 14:16 a
drwxr-xr-x. 2 tmp1 tmp1 6 Feb 22 14:16 c
-rw-r--r--. 1 tmp2 tmp2 0 Feb 22 14:21 dd
[tmp2jock data]$ rm -f dd
[tmp2jock data]$ ll
total 0
-rw-r--r--. 1 tmp1 tmp1 0 Feb 22 14:16 a
drwxr-xr-x. 2 tmp1 tmp1 6 Feb 22 14:16 c 11.3 ACL权限
ACL 权限分配给指定的用户指定目录分配指定的权限。
11.3.1 设置ACL权限
这需要使用 setfacl 命令它的语法如下 [rootjock data]# setfacl --help
setfacl 2.3.1 -- set file access control lists
Usage: setfacl [-bkndRLP] { -m|-M|-x|-X ... } file ...-m, --modifyacl modify the current ACL(s) of file(s)-M, --modify-filefile read ACL entries to modify from file-x, --removeacl remove entries from the ACL(s) of file(s)-X, --remove-filefile read ACL entries to remove from file-b, --remove-all remove all extended ACL entries-k, --remove-default remove the default ACL--setacl set the ACL of file(s), replacing the current ACL--set-filefile read ACL entries to set from file--mask do recalculate the effective rights mask-n, --no-mask dont recalculate the effective rights mask-d, --default operations apply to the default ACL-R, --recursive recurse into subdirectories-L, --logical logical walk, follow symbolic links-P, --physical physical walk, do not follow symbolic links--restorefile restore ACLs (inverse of getfacl -R)--test test mode (ACLs are not modified)-v, --version print version and exit-h, --help this help text工作中常用的选项
-m 设置acl权限设置的格式为 u:用户名:权限或者 g:组名:权限
-x 删除指定的 acl 权限
-b 删除所有的 acl 权限
-d 设置默认的 acl 权限只对目录生效
-k 删除默认的 acl 权限
-R 递归设置 acl 权限
使用示例 # 创建目录
[rootjock /]# mkdir /openlab
[rootjock /]# ll -d /openlab
drwxr-xr-x. 2 root root 6 Feb 22 14:28 /openlab# 设置普通权限
[rootjock /]# chmod 770 /openlab/
[rootjock /]# ll -d /openlab/
drwxrwx---. 2 root root 6 Feb 22 14:28 /openlab/# 创建两个用户和一个组
[rootjock /]# useradd zs
[rootjock /]# useradd lisi
[rootjock /]# groupadd hehe# 给这个组设置用户列表
[rootjock /]# gpasswd -M zs,lisi hehe# 修改目录的所属组
[rootjock /]# chown root:hehe /openlab/
[rootjock /]# ll -d /openlab/
drwxrwx---. 2 root hehe 6 Feb 22 14:28 /openlab/# 再次创建一个用户并设置密码
[rootjock /]# useradd ww
[rootjock /]# passwd ww
Changing password for user ww.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[rootjock /]# ll -d /openlab/
drwxrwx---. 2 root hehe 6 Feb 22 14:28 /openlab/# 为用户设置 acl 权限
[rootjock /]# setfacl -m u:ww:rx /openlab
[rootjock /]# ll -d /openlab/
drwxrwx--- 2 root hehe 6 Feb 22 14:28 /openlab/[rootjock /]# su - ww
[wwjock ~]$ cd /openlab/
[wwjock openlab]$ touch aa
touch: cannot touch aa: Permission denied# 为了演示我们再创建了一个 xixi 组
[rootjock /]# groupadd xixi
# 然后为这个 xixi 组设置 acl 权限
[rootjock /]# setfacl -m g:xixi:rwx /openlab/
[rootjock /]# ll -d /openlab/
drwxrwx--- 2 root hehe 6 Feb 22 14:28 /openlab/11.3.2 查看ACL权限
查看 ACL 权限是通过 getfacl 命令来实现的。语法 [rootjock /]# getfacl --help
getfacl 2.3.1 -- get file access control lists
Usage: getfacl [-aceEsRLPtpndvh] file ...-a, --access display the file access control list only-d, --default display the default access control list only-c, --omit-header do not display the comment header-e, --all-effective print all effective rights-E, --no-effective print no effective rights-s, --skip-base skip files that only have the base entries-R, --recursive recurse into subdirectories-L, --logical logical walk, follow symbolic links-P, --physical physical walk, do not follow symbolic links-t, --tabular use tabular output format-n, --numeric print numeric user/group identifiers--one-file-system skip files on different filesystems-p, --absolute-names dont strip leading / in pathnames-v, --version print version and exit-h, --help this help text使用示例 [rootjock /]# getfacl /openlab/
getfacl: Removing leading / from absolute path names
# file: openlab/
# owner: root
# group: hehe
user::rwx
user:ww:r-x
group::rwx
group:xixi:rwx
mask::rwx
other::---11.3.3 删除ACL权限
实现这个功能还是使用 setfacl 命令只是通过 -x 或 -b 选项来实现 [rootjock /]# setfacl -x u:ww /openlab/
[rootjock /]# getfacl /openlab/
getfacl: Removing leading / from absolute path names
# file: openlab/
# owner: root
# group: hehe
user::rwx
group::rwx
group:xixi:rwx
mask::rwx
other::---[rootjock /]# setfacl -b /openlab/
[rootjock /]# getfacl /openlab/
getfacl: Removing leading / from absolute path names
# file: openlab/
# owner: root
# group: hehe
user::rwx
group::rwx
other::---12. 网络管理
12.1 nmcli
网络管理我们只需要记住 nmcli 命令就好。查看命令的格式 [rootjock /]# nmcli --help
Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }OPTIONS-a, --ask ask for missing parameters-c, --colors auto|yes|no whether to use colors in output-e, --escape yes|no escape columns separators in values-f, --fields field,...|all|common specify fields to output-g, --get-values field,...|all|common shortcut for -m tabular -t -f-h, --help print this help-m, --mode tabular|multiline output mode-o, --overview overview mode-p, --pretty pretty output-s, --show-secrets allow displaying passwords-t, --terse terse output-v, --version show program version-w, --wait seconds set timeout waiting for finishing operationsOBJECTg[eneral] NetworkManagers general status and operationsn[etworking] overall networking controlr[adio] NetworkManager radio switchesc[onnection] NetworkManagers connectionsd[evice] devices managed by NetworkManagera[gent] NetworkManager secret agent or polkit agentm[onitor] monitor NetworkManager changes使用示例 # 查看网络信息
[rootjock /]# ip a
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: ens160: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc mq state UP group default qlen 1000link/ether 00:0c:29:08:da:ba brd ff:ff:ff:ff:ff:ffaltname enp3s0inet 192.168.72.120/24 brd 192.168.72.255 scope global noprefixroute ens160valid_lft forever preferred_lft foreverinet 192.168.72.128/24 brd 192.168.72.255 scope global secondary dynamic noprefixroute ens160valid_lft 1501sec preferred_lft 1501secinet6 fe80::20c:29ff:fe08:daba/64 scope link noprefixroute valid_lft forever preferred_lft forever# 设置IP
nmcli c modify ens160 ipv4.addresses 192.168.72.120/24
# 设置为手动方式
nmcli c modify ens160 ipv4.method manual
# 配置dns
nmcli c modify ens160 ipv4.dns 114.114.114.114
# 配置网关
nmcli c modify ens160 ipv4.gateway 192.168.72.2
# 设置自动连接
nmcli c modify ens160 connection.autoconnect yes
# 更新会话
nmcli c up ens160# 增加一个新的 IP 地址
[rootjock ~]# nmcli c modify ens160 ipv4.addresses 192.168.72.121/24
[rootjock ~]# nmcli c up ens160
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
[rootjock ~]# ip a
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: ens160: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc mq state UP group default qlen 1000link/ether 00:0c:29:08:da:ba brd ff:ff:ff:ff:ff:ffaltname enp3s0inet 192.168.72.120/24 brd 192.168.72.255 scope global noprefixroute ens160valid_lft forever preferred_lft foreverinet 192.168.72.121/24 brd 192.168.72.255 scope global secondary noprefixroute ens160valid_lft forever preferred_lft foreverinet6 fe80::20c:29ff:fe08:daba/64 scope link noprefixroute valid_lft forever preferred_lft forever# 删除一个 IP
[rootjock ~]# nmcli c modify ens160 -ipv4.addresses 192.168.72.121/24
[rootjock ~]# nmcli c up ens160
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)
[rootjock ~]# ip a
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: ens160: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc mq state UP group default qlen 1000link/ether 00:0c:29:08:da:ba brd ff:ff:ff:ff:ff:ffaltname enp3s0inet 192.168.72.120/24 brd 192.168.72.255 scope global noprefixroute ens160valid_lft forever preferred_lft foreverinet6 fe80::20c:29ff:fe08:daba/64 scope link noprefixroute valid_lft forever preferred_lft forever 12.2 主机映射 [rootjock ~]# ping jock
^C
[rootjock ~]# ping 192.168.72.120
PING 192.168.72.120 (192.168.72.120) 56(84) bytes of data.
64 bytes from 192.168.72.120: icmp_seq1 ttl64 time0.074 ms
64 bytes from 192.168.72.120: icmp_seq2 ttl64 time0.071 ms
64 bytes from 192.168.72.120: icmp_seq3 ttl64 time0.076 ms
^C
--- 192.168.72.120 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2039ms
rtt min/avg/max/mdev 0.071/0.073/0.076/0.002 ms我们如果希望通过 ping 主机名称也能够 ping 通则需要对主机名和 IP 地址进行映射。好处是我们在后续使用时就通过主机名来使用而不是通过 IP 地址。
要想两者可以进行映射我们就需要修改 /etc/hosts 文件在这个文件中去添加映射关系。 [rootjock ~]# vim /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.72.120 jock 映射好后就可以 ping 通过了 [rootjock ~]# ping jock
PING jock (192.168.72.120) 56(84) bytes of data.
64 bytes from jock (192.168.72.120): icmp_seq1 ttl64 time0.047 ms
64 bytes from jock (192.168.72.120): icmp_seq2 ttl64 time0.073 ms
64 bytes from jock (192.168.72.120): icmp_seq3 ttl64 time0.073 ms
64 bytes from jock (192.168.72.120): icmp_seq4 ttl64 time0.067 ms 13. 磁盘管理 [rootjock ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 8.9G 0 rom
nvme0n1 259:0 0 50G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 49G 0 part ├─rhel_bogon-root 253:0 0 44G 0 lvm /└─rhel_bogon-swap 253:1 0 5G 0 lvm [SWAP]
nvme0n2 259:3 0 20G 0 disk 要使用一个新的磁盘我们需要做如下的步骤
1查看磁盘 lsblk
2对磁盘进行分区 fdisk 格式为 fdisk 设置名称如/dev/nvme0n2 [rootjock ~]# fdisk /dev/nvme0n2Command (m for help): n # 输入n 来创建新分区
Partition typep primary (0 primary, 0 extended, 4 free)e extended (container for logical partitions)
Select (default p): p # 创建主分区
Partition number (1-4, default 1): # 第一个主分区
First sector (2048-41943039, default 2048): # 指定扇区大小默认就好
Last sector, /-sectors or /-size{K,M,G,T,P} (2048-41943039, default 41943039): 5G # 指定分区大小Created a new partition 1 of type Linux and of size 5 GiB.Command (m for help): n # 再创建一个新分区
Partition typep primary (1 primary, 0 extended, 3 free)e extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2):
First sector (10487808-41943039, default 10487808): 15G
Value out of range.
First sector (10487808-41943039, default 10487808):
Last sector, /-sectors or /-size{K,M,G,T,P} (10487808-41943039, default 41943039): Created a new partition 2 of type Linux and of size 15 GiB.Command (m for help): w # 保存分区
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.3对分区进行格式化使用 mkfs.xfs 命令 # 查看分区号
[rootjock ~]# blkid
/dev/mapper/rhel_bogon-swap: UUIDc40a1d4e-8acc-4aa6-af02-773c878f5588 TYPEswap
/dev/nvme0n1p1: UUID98e55bfe-116c-411f-956c-d3e925b515c5 TYPExfs PARTUUIDd366e00e-01
/dev/nvme0n1p2: UUIDgklf44-wXFJ-G8dL-qAtd-eo50-onCK-5qz24M TYPELVM2_member PARTUUIDd366e00e-02
/dev/sr0: UUID2023-04-13-16-58-02-00 LABELRHEL-9-2-0-BaseOS-x86_64 TYPEiso9660 PTUUIDd3d1f9a5 PTTYPEdos
/dev/nvme0n2p1: PARTUUID6bf53a97-01
/dev/nvme0n2p2: PARTUUID6bf53a97-02
/dev/mapper/rhel_bogon-root: UUIDa673e989-57a6-4257-be10-bb807f7153d7 TYPExfs# 对分区号为 /dev/nvme0n2p1 进行格式化
[rootjock ~]# mkfs.xfs /dev/nvme0n2p1
meta-data/dev/nvme0n2p1 isize512 agcount4, agsize327680 blks sectsz512 attr2, projid32bit1 crc1 finobt1, sparse1, rmapbt0 reflink1 bigtime1 inobtcount1
data bsize4096 blocks1310720, imaxpct25 sunit0 swidth0 blks
naming version 2 bsize4096 ascii-ci0, ftype1
log internal log bsize4096 blocks2560, version2 sectsz512 sunit0 blks, lazy-count1
realtime none extsz4096 blocks0, rtextents0# 对分区号为 /dev/nvme0n2p2 进行格式化
[rootjock ~]# mkfs.xfs /dev/nvme0n2p2
meta-data/dev/nvme0n2p2 isize512 agcount4, agsize982976 blks sectsz512 attr2, projid32bit1 crc1 finobt1, sparse1, rmapbt0 reflink1 bigtime1 inobtcount1
data bsize4096 blocks3931904, imaxpct25 sunit0 swidth0 blks
naming version 2 bsize4096 ascii-ci0, ftype1
log internal log bsize4096 blocks2560, version2 sectsz512 sunit0 blks, lazy-count1
realtime none extsz4096 blocks0, rtextents0# 再次查看分区信息
[rootjock ~]# blkid
/dev/mapper/rhel_bogon-swap: UUIDc40a1d4e-8acc-4aa6-af02-773c878f5588 TYPEswap
/dev/nvme0n1p1: UUID98e55bfe-116c-411f-956c-d3e925b515c5 TYPExfs PARTUUIDd366e00e-01
/dev/nvme0n1p2: UUIDgklf44-wXFJ-G8dL-qAtd-eo50-onCK-5qz24M TYPELVM2_member PARTUUIDd366e00e-02
/dev/sr0: UUID2023-04-13-16-58-02-00 LABELRHEL-9-2-0-BaseOS-x86_64 TYPEiso9660 PTUUIDd3d1f9a5 PTTYPEdos
/dev/mapper/rhel_bogon-root: UUIDa673e989-57a6-4257-be10-bb807f7153d7 TYPExfs
/dev/nvme0n2p1: UUID8a3f6aff-dafe-4257-9046-454f0ee8eca1 TYPExfs PARTUUID6bf53a97-01
/dev/nvme0n2p2: UUIDf8ba088d-3816-4e05-9172-6577fb6155a5 TYPExfs PARTUUID6bf53a97-02 4挂载使用 mount 命令
命令格式
#mount [-t 文件系统类型] 设备名 挂载点目录
mount [-t vfstype] [-o options] device dir1、-t vfstype 指定文件系统的类型通常不必指定mount 会自动选择正确的类型。光盘或光盘镜像iso9660DOS fat16文件系统msdosWindows 9x fat32文件系统vfatWindows NT ntfs文件系统ntfsMount Windows文件网络共享smbfsUNIX(LINUX) 文件网络共享nfs
2、-o options 主要用来描述设备或档案的挂接方式。loop用来把一个文件当成硬盘分区挂接上系统ro采用只读方式挂接设备rw采用读写方式挂接设备iocharset指定访问文件系统所用字符集
3、device 要挂接(mount)的设备。
4、dir设备在系统上的挂接点(mount point)。
使用示例 # 创建挂载点
[rootjock ~]# mkdir /opt/mydisk# 将格式化好的磁盘挂载到挂载点上
[rootjock ~]# mount /dev/nvme0n2p1 /opt/mydisk/
# 查看磁盘信息
[rootjock ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 8.9G 0 rom
nvme0n1 259:0 0 50G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 49G 0 part ├─rhel_bogon-root 253:0 0 44G 0 lvm /└─rhel_bogon-swap 253:1 0 5G 0 lvm [SWAP]
nvme0n2 259:3 0 20G 0 disk
├─nvme0n2p1 259:6 0 5G 0 part /opt/mydisk
└─nvme0n2p2 259:7 0 15G 0 part 以上的挂载方式是临时的当系统启动后这个挂载就会失效要想使用我们还需要重新挂载。如果希望永久生效则需要修改 /etc/fstab 文件并在这个文件中添加内容 [rootjock ~]# vim /etc/fstab# 添加如下内容/dev/mapper/rhel_bogon-root / xfs defaults 0 0
UUID98e55bfe-116c-411f-956c-d3e925b515c5 /boot xfs defaults 0 0
/dev/mapper/rhel_bogon-swap none swap defaults 0 0/dev/nvme0n2p2 /opt/mydisk2 xfs defaults 0 0
# 磁盘名称 挂载点 文件类型 [rootjock ~]# mkdir /opt/mydisk2[rootjock ~]# mount /dev/nvme0n2p2 /opt/mydisk2
[rootjock ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 8.9G 0 rom
nvme0n1 259:0 0 50G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 49G 0 part ├─rhel_bogon-root 253:0 0 44G 0 lvm /└─rhel_bogon-swap 253:1 0 5G 0 lvm [SWAP]
nvme0n2 259:3 0 20G 0 disk
├─nvme0n2p1 259:6 0 5G 0 part /opt/mydisk
└─nvme0n2p2 259:7 0 15G 0 part /opt/mydisk2