前言
对于服务器的操作系统而言,大多数公司都会采用Linux,而我个人接触到的多数是红帽系的CentOS,虽然已经停更了,但应用还是很广泛。此篇以CentOS为基础介绍Linux的一些基本知识。

主目录及其作用
登录系统后,在终端的/
根目录下输入ls
命令,可以显示根目录下的所有目录
# ls
bin boot dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
下面介绍一下这些目录的作用:
- /bin: bin是Binaries(二进制文件)的缩写,这个目录存放着最经常使用的命令;
- /boot: 这里存放的是启动Linux时使用的一些核心文件,包括一些连接文件以及镜像文件;
- /dev: dev是Device(设备)的缩写,该目录下存放的是Linux的外部设备,在Linux中访问设备的方式和访问文件的方式是相同的;
- __/etc__: etc是Etcetera(等等)的缩写,这个目录用例存放所有的系统管理所需要的配置文件和子目录;
- /home: 用户的主目录,在Linux中,每个用户都有一个自己的目录,一般该目录名是以用户的账号命名的;
- /lib及/lib64: lib是Library(库)的缩写,这个目录里存放着系统最基本的动态链接共享库,几乎所有的应用程序都需要用到这些共享库;分成32位/lib和64位/lib64两个目录;
- /lost+found: 这个目录一般是空的,当系统非法关机后,这里就存放了一些文件;
- /media: Linux系统会自动识别一些设备,例如U盘、光驱等,识别后,Linux会吧识别的设备挂载到这个目录下;
- /mnt: 系统提供该目录是为了让用户临时挂载别的文件系统的,可以将光驱挂载到/mnt/下,然后进入目录就可以查看光盘里的内容了;
- /opt: opt是optional(可选)的缩写,这是给主机额外安装软件所设置的目录,一般是空的;
- /proc: proc是Processes(进程)的缩写,/proc是一个伪文件系统(也即虚拟文件系统),存储的是当前内核运行状态的一系列特殊文件,这个目录是一个虚拟的目录,它是系统内容的映射,我们可以通过直接访问这个目录来获取系统信息;
- /root: 该目录为系统管理员root的主目录;
- /run: 是一个临时文件系统,存放系统启动以来的信息。当系统重启时,这个目录下的文件会被删掉或清除。
- /sbin: s是Super User的意思,即SuperUser Binaries(超级用户的二进制文件)的缩写,这里存放的是系统管理员使用的系统管理程序;
- /srv: 该目录存放一些服务启动之后需要提取的数据;
- /sys: 这里存放着系统内核文件等信息;
- /tmp: tmp是temporary(临时)的缩写,这个目录是用来存放一些临时文件的;
- /usr: usr是unix shared resources(共享资源)的缩写,这是一个非常重要的目录,用户的很多应用程序和文件都放在这个目录下;
- /usr/bin: 系统用户使用的应用程序;
- /usr/local/bin: 用户放置自己的可执行程序的地方;
- /usr/sbin: 超级用户使用的比较高级的管理程序和系统守护程序;
- /usr/src: 内核源代码默认的放置目录;
- /var: var是variable(变量)的缩写,这个目录中存放着在不断扩充着的东西,一般讲那些经常被修改的目录放在这个目录下,包括各种日志文件;
上面是Linux系统根目录的文件夹,其中/bin
、/home
、/etc
、/usr
、/usr/bin
、/usr/local/bin
非常重要。
文件及目录的操作
命令行提示符
[root@laobai-vm-centos ~]#
- 当前用户名: @前的部分,这里是
root
; - 主机名: @后的部分,到空格处位置,这里是
laobai-vm-centos
; - 当前命令行所在目录: 这里是
~
,~
这里表示家目录,root账号的家目录为/root
; - 标识符:
#
表示有管理权限的账号,这里是root,$
表示没有管理权限的普通账号;
处理目录的常用命令
常见的目录展示形式有两种:绝对路径和相对路径
绝对路径,是由根目录/
写起,例如/usr/python3/bin
相对路径,不是由根目录/
写起,例如从目录/usr/python3/bin
切换到目录/usr/python3/lib
可以用cd ../lib
man
- 作用:查看各个linux命令的使用文档
- 实例:
man ls
LS(1) BSD General Commands Manual LS(1)
NAME
ls -- list directory contents
SYNOPSIS
ls [-ABCFGHLOPRSTUW@abcdefghiklmnopqrstuwx1] [file ...]
DESCRIPTION
For each operand that names a file of a type other than directory,
ls displays its name as well as any requested, associated informa-
tion. For each operand that names a file of type directory, ls
displays the names of files contained within that directory, as
well as any requested, associated information.
...
man cd
BASH_BUILTINS(1) BASH_BUILTINS(1)
NAME
bash, :, ., [, alias, bg, bind, break, builtin, caller, cd, command, compgen, complete, compopt, con-
tinue, declare, dirs, disown, echo, enable, eval, exec, exit, export, false, fc, fg, getopts, hash, help,
history, jobs, kill, let, local, logout, mapfile, popd, printf, pushd, pwd, read, readonly, return, set,
shift, shopt, source, suspend, test, times, trap, true, type, typeset, ulimit, umask, unalias, unset,
wait - bash built-in commands, see bash(1)
BASH BUILTIN COMMANDS
Unless otherwise noted, each builtin command documented in this section as accepting options preceded by
- accepts -- to signify the end of the options. The :, true, false, and test builtins do not accept
options and do not treat -- specially.
使用文档篇幅过大,这里只摘取了一部分内容,就不再贴了。
ls
作用:列出目录及文件名
实例:
ls anaconda-ks.cfg install.log install.log.syslog 公共的 模板 视频 图片 文档 下载 音乐 桌面
加
-a
参数,显示隐藏文件及目录ls -a . .bash_history .cache .esd_auth .gnupg .imsettings.log .nautilus .tcshrc 图片 桌面 .. .bash_logout .config .gconf .gtk-bookmarks install.log .pulse 公共的 文档 .abrt .bash_profile .cshrc .gnome2 .gvfs install.log.syslog .pulse-cookie 模板 下载 anaconda-ks.cfg .bashrc .dbus .gnote .ICEauthority .local .ssh 视频 音乐
加
-l
参数,显示文件属性及权限等数据ls -l 总用量 100 -rw-------. 1 root root 1651 10月 3 2022 anaconda-ks.cfg -rw-r--r--. 1 root root 46832 10月 3 2022 install.log -rw-r--r--. 1 root root 11504 10月 3 2022 install.log.syslog drwxr-xr-x. 2 root root 4096 10月 3 2022 公共的 drwxr-xr-x. 2 root root 4096 10月 3 2022 模板 drwxr-xr-x. 2 root root 4096 10月 3 2022 视频 drwxr-xr-x. 2 root root 4096 10月 3 2022 图片 drwxr-xr-x. 2 root root 4096 10月 3 2022 文档 drwxr-xr-x. 2 root root 4096 10月 3 2022 下载 drwxr-xr-x. 2 root root 4096 10月 3 2022 音乐 drwxr-xr-x. 2 root root 4096 10月 3 2022 桌面
同时加
-a
和-l
两个参数ls -al 总用量 204 dr-xr-x---. 23 root root 4096 10月 3 2022 . dr-xr-xr-x. 25 root root 4096 10月 3 2022 .. drwxr-xr-x. 2 root root 4096 10月 3 2022 .abrt -rw-------. 1 root root 1651 10月 3 2022 anaconda-ks.cfg -rw-------. 1 root root 106 10月 3 2022 .bash_history -rw-r--r--. 1 root root 18 5月 20 2009 .bash_logout -rw-r--r--. 1 root root 176 5月 20 2009 .bash_profile -rw-r--r--. 1 root root 176 9月 23 2004 .bashrc drwxr-xr-x. 3 root root 4096 10月 3 2022 .cache drwxr-xr-x. 5 root root 4096 10月 3 2022 .config -rw-r--r--. 1 root root 100 9月 23 2004 .cshrc drwx------. 3 root root 4096 10月 3 2022 .dbus -rw-------. 1 root root 16 10月 3 2022 .esd_auth drwx------. 4 root root 4096 10月 3 2022 .gconf drwx------. 5 root root 4096 10月 3 2022 .gnome2 drwxr-xr-x. 3 root root 4096 10月 3 2022 .gnote drwx------. 2 root root 4096 10月 3 2022 .gnupg -rw-r--r--. 1 root root 160 10月 3 2022 .gtk-bookmarks drwx------. 2 root root 4096 10月 3 2022 .gvfs -rw-------. 1 root root 310 10月 3 2022 .ICEauthority -rw-r--r--. 1 root root 860 10月 3 2022 .imsettings.log -rw-r--r--. 1 root root 46832 10月 3 2022 install.log -rw-r--r--. 1 root root 11504 10月 3 2022 install.log.syslog drwxr-xr-x. 3 root root 4096 10月 3 2022 .local drwxr-xr-x. 2 root root 4096 10月 3 2022 .nautilus drwx------. 2 root root 4096 10月 3 2022 .pulse -rw-------. 1 root root 256 10月 3 2022 .pulse-cookie drwx------. 2 root root 4096 10月 3 2022 .ssh -rw-r--r--. 1 root root 129 12月 4 2004 .tcshrc drwxr-xr-x. 2 root root 4096 10月 3 2022 公共的 drwxr-xr-x. 2 root root 4096 10月 3 2022 模板 drwxr-xr-x. 2 root root 4096 10月 3 2022 视频 drwxr-xr-x. 2 root root 4096 10月 3 2022 图片 drwxr-xr-x. 2 root root 4096 10月 3 2022 文档 drwxr-xr-x. 2 root root 4096 10月 3 2022 下载 drwxr-xr-x. 2 root root 4096 10月 3 2022 音乐 drwxr-xr-x. 2 root root 4096 10月 3 2022 桌面
加
S
参数,按照文件大小降序排列ls -lS 总用量 128 -rw-r--r--. 1 root root 46832 10月 3 2022 install.log -rw-r--r--. 1 root root 25578 10月 3 15:41 test1.py -rw-r--r--. 1 root root 11504 10月 3 2022 install.log.syslog drwxr-xr-x. 2 root root 4096 10月 3 2022 公共的 drwxr-xr-x. 2 root root 4096 10月 3 2022 模板 drwxr-xr-x. 2 root root 4096 10月 3 2022 视频 drwxr-xr-x. 2 root root 4096 10月 3 2022 图片 drwxr-xr-x. 2 root root 4096 10月 3 2022 文档 drwxr-xr-x. 2 root root 4096 10月 3 2022 下载 drwxr-xr-x. 2 root root 4096 10月 3 2022 音乐 drwxr-xr-x. 2 root root 4096 10月 3 2022 桌面 -rw-------. 1 root root 1651 10月 3 2022 anaconda-ks.cfg
加
t
参数,按照文件的创建时间降序排列dr-xr-x---. 23 root root 4096 10月 3 15:43 . -rw-------. 1 root root 2928 10月 3 15:43 .viminfo -rw-r--r--. 1 root root 34 10月 3 15:43 test2.py -rw-r--r--. 1 root root 25578 10月 3 15:41 test1.py
cd
作用:切换目录
实例:
切换到某个绝对路径,如
/usr/bin
[root@laobai-vm-centos ~]# cd /usr/bin [root@laobai-vm-centos bin]#
切换到某个相对路径,如
/usr/bin
切换到/usr/lib
切换到家目录,root账号的家目录为
/root
[root@laobai-vm-centos lib]# cd ~ [root@laobai-vm-centos ~]#
切换到上一级目录
[root@laobai-vm-centos bin]# cd .. [root@laobai-vm-centos usr]#
pwd
- 作用:显示当前所在目录
- 实例:
[root@laobai-vm-centos ~]# pwd
/root
[root@laobai-vm-centos bin]# pwd
/usr/bin
mkdir
作用:创建一个新的目录
实例:
创建一个新目录
[root@laobai-vm-centos temp]# ls [root@laobai-vm-centos temp]# mkdir test [root@laobai-vm-centos temp]# ls test
一次性创建多层目录
[root@laobai-vm-centos temp]# mkdir -p test1/test2/test3/test4 [root@laobai-vm-centos temp]# ls test test1 [root@laobai-vm-centos temp]# cd test1 [root@laobai-vm-centos test1]# ls test2 [root@laobai-vm-centos test1]# cd test2 [root@laobai-vm-centos test2]# ls test3 [root@laobai-vm-centos test2]# cd test3 [root@laobai-vm-centos test3]# ls test4 [root@laobai-vm-centos test3]# cd test4 [root@laobai-vm-centos test4]# ls
rmdir
作用:删除一个空的目录
实例:
删除一个空目录
[root@laobai-vm-centos temp]# ls test test1 [root@laobai-vm-centos temp]# rmdir test [root@laobai-vm-centos temp]# ls test1
删除多层级空目录
[root@laobai-vm-centos temp]# ls test1 [root@laobai-vm-centos temp]# rmdir -p test1/test2/test3/test4 [root@laobai-vm-centos temp]# ls
cp
作用:复制文件或目录
实例:
复制,将
/home/laobai/temp/1.py
复制到/home/laobai/temp/test1/test2/test3/test4
目录下cp /home/laobai/temp/1.py /home/laobai/temp/test1/test2/test3/test4 [root@laobai-vm-centos test4]# ls 1.py
改名复制
cp /home/laobai/temp/1.py /home/laobai/temp/test1/test2/test3/test4/2.py [root@laobai-vm-centos test4]# ls 2.py
加
-f
参数,强制覆盖,覆盖已经存在的目标文件而不给出提示;cp /home/laobai/temp/1.py /home/laobai/temp/test1/test2/test3/test4 cp:是否覆盖"/home/laobai/temp/test1/test2/test3/test4/1.py"?
cp -f /home/laobai/temp/1.py /home/laobai/temp/test1/test2/test3/test4 cp:是否覆盖"/home/laobai/temp/test1/test2/test3/test4/1.py"?
这里发现加上
-f
参数还是会提示文件存在是否覆盖。这是Linux系统默认给cp
的命令增加了-i
参数,进行交互式的信息提示。which cp alias cp='cp -i' /bin/cp
加
-r
参数,递归复制,用于目录层级文件的复制[root@laobaiecs test]# tree . └── test1 ├── test1.py └── test2 ├── test2.py └── test3 ├── test3.py └── test4 └── test4.py
[root@laobaiecs temp]# cp -r /root/test /root/temp [root@laobaiecs temp]# tree /root/temp /root/temp └── test └── test1 ├── test1.py └── test2 ├── test2.py └── test3 ├── test3.py └── test4 └── test4.py 5 directories, 4 files
rm
作用:删除文件或目录
实例:
删除
[root@laobaiecs test4]# ls test4.py [root@laobaiecs test4]# rm test4.py rm: remove regular file ‘test4.py’? yes [root@laobaiecs test4]# ls [root@laobaiecs test4]#
加
-f
参数,强制删除,不会进行交互式确认rm -f test4.py
加
-r
参数,递归删除rm -r /root/temp/
加
-r
和-f
两个参数,强制递归删除rm -rf /root/test/
mv
作用:移动文件与目录,或修改文件与目录的名称
实例:
移动文件及目录
[root@laobaiecs temp]# ls temp test [root@laobaiecs temp]# mv temp/ /root/test [root@laobaiecs temp]# ls test [root@laobaiecs temp]# cd test [root@laobaiecs test]# ls test1
改名移动
[root@laobaiecs test1]# ls test1.py test2 [root@laobaiecs test1]# mv test1.py ./test2/1test.yp [root@laobaiecs test1]# ls test2 [root@laobaiecs test1]# cd test2 [root@laobaiecs test2]# ls 1test.yp test2.py test3
用户和组管理
创建用户
[root@laobai-vm-centos home]# ls
laobai
[root@laobai-vm-centos home]# useradd plscript
[root@laobai-vm-centos home]# ls
laobai plscript
另外可以通过/etc/passwd
文件查看到当前系统中的所有用户信息
cat /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
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rtkit:x:499:499:RealtimeKit:/proc:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
saslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
laobai:x:500:500::/home/laobai:/bin/bash
plscript:x:501:501::/home/plscript:/bin/bash
为用户创建密码
# 为上一步中新建的用户plscript创建密码
passwd plscript
更改用户 plscript 的密码 。
新的 密码:
无效的密码: 它基于字典单词
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。
创建用户组
groupadd pls
另外可以通过/etc/group
文件查看到当前系统中的所有用户组信息
cat /etc/group
root:x:0:
bin:x:1:bin,daemon
daemon:x:2:bin,daemon
sys:x:3:bin,adm
adm:x:4:adm,daemon
tty:x:5:
disk:x:6:
lp:x:7:daemon
mem:x:8:
kmem:x:9:
wheel:x:10:
mail:x:12:mail,postfix
uucp:x:14:
man:x:15:
games:x:20:
gopher:x:30:
video:x:39:
dip:x:40:
ftp:x:50:
lock:x:54:
audio:x:63:
nobody:x:99:
users:x:100:
dbus:x:81:
usbmuxd:x:113:
rpc:x:32:
utmp:x:22:
utempter:x:35:
rtkit:x:499:
avahi-autoipd:x:170:
desktop_admin_r:x:498:
desktop_user_r:x:497:
floppy:x:19:
vcsa:x:69:
abrt:x:173:
cdrom:x:11:
tape:x:33:
dialout:x:18:
wbpriv:x:88:
rpcuser:x:29:
nfsnobody:x:65534:
haldaemon:x:68:haldaemon
ntp:x:38:
apache:x:48:
saslauth:x:76:
postdrop:x:90:
postfix:x:89:
gdm:x:42:
pulse:x:496:
pulse-access:x:495:
fuse:x:494:
sshd:x:74:
slocate:x:21:
stapusr:x:156:
stapsys:x:157:
stapdev:x:158:
tcpdump:x:72:
laobai:x:500:
plscript:x:501:
pls:x:502:
创建用户时自动添加到组
useradd -g pls plsgroup
文件权限
文件/目录列表显示说明
# ll -a
dr-xr-x---. 23 root root 4096 10月 3 13:11 .
dr-xr-xr-x. 25 root root 4096 10月 3 2022 ..
drwxr-xr-x. 2 root root 4096 10月 3 2022 .abrt
-rw-------. 1 root root 1651 10月 3 2022 anaconda-ks.cfg
-rw-r--r--. 1 root root 46832 10月 3 2022 install.log
-rw-r--r--. 1 root root 11504 10月 3 2022 install.log.syslog
drwxr-xr-x. 3 root root 4096 10月 3 2022 .local
drwxr-xr-x. 2 root root 4096 10月 3 2022 .nautilus
drwx------. 2 root root 4096 10月 3 2022 .pulse
drwxr-xr-x. 2 root root 4096 10月 3 2022 公共的
查询结果有7列,这里说明一下每一列的含义:
文件的类型及权限
文件的类型及权限,用11位字符表示,即上面图示中的10位字符及第11位.
r - read 对应权限数字为4
w - write 对应权限数字为2
x - execute 对应权限数字为1
a. 第0位,表示文件或目录类型
d
表示目录-
表示文件l
表示为链接文档(link)b. 第1~3位,文件或目录所有者权限
r
表示读权限w
表示写权限x
表示执行权限-
表示没有权限c. 第4~6位,文件或目录所有者所在用户组的权限
r
表示读权限w
表示写权限x
表示执行权限-
表示没有权限d. 第7~9位,其他用户及用户组权限
r
表示读权限w
表示写权限x
表示执行权限-
表示没有权限e. 第11位,. 表示被selinux的安全机制所保护。若关闭了selinux安全机制,新建的文件或目录不会显示.,但之前被管理的文件或目录还是会显示.
磁盘上的节点ID
所属文件/目录的用户名
所属用户在的用户组
文件大小
创建时间
文件名
修改文件属性
chgrp
作用:修改文件属组
实例:
a. 将1.py文件的属组由root
改为pls
[root@laobai-vm-centos temp]# ll
总用量 16
-rw-r--r--. 1 root root 14 10月 3 16:44 1.py
-rw-r--r--. 1 root root 56 10月 3 20:04 2.py
-rw-r--r--. 1 root root 5 10月 3 20:04 3.py
drwxr-xr-x. 3 root root 4096 10月 3 17:02 test1
[root@laobai-vm-centos temp]# chgrp pls 1.py
[root@laobai-vm-centos temp]# ll
总用量 16
-rw-r--r--. 1 root pls 14 10月 3 16:44 1.py
-rw-r--r--. 1 root root 56 10月 3 20:04 2.py
-rw-r--r--. 1 root root 5 10月 3 20:04 3.py
drwxr-xr-x. 3 root root 4096 10月 3 17:02 test1
b.
[root@laobai-vm-centos temp]# ll
总用量 16
-rw-r--r--. 1 root pls 14 10月 3 16:44 1.py
-rw-r--r--. 1 root root 56 10月 3 20:04 2.py
-rw-r--r--. 1 root root 5 10月 3 20:04 3.py
drwxr-xr-x. 3 root root 4096 10月 3 17:02 test1
[root@laobai-vm-centos temp]# chgrp -R pls test1/
[root@laobai-vm-centos temp]# ll
总用量 16
-rw-r--r--. 1 root pls 14 10月 3 16:44 1.py
-rw-r--r--. 1 root root 56 10月 3 20:04 2.py
-rw-r--r--. 1 root root 5 10月 3 20:04 3.py
drwxr-xr-x. 3 root pls 4096 10月 3 17:02 test1
chown
作用:修改文件属主,可以可以同时修改文件属组
实例:
a. 将1.py的属主由root
改为plscript
[root@laobai-vm-centos temp]# ll
总用量 16
-rw-r--r--. 1 root pls 14 10月 3 16:44 1.py
-rw-r--r--. 1 root root 56 10月 3 20:04 2.py
-rw-r--r--. 1 root root 5 10月 3 20:04 3.py
drwxr-xr-x. 3 root pls 4096 10月 3 17:02 test1
[root@laobai-vm-centos temp]# chown plscript 1.py
[root@laobai-vm-centos temp]# ll
总用量 16
-rw-r--r--. 1 plscript pls 14 10月 3 16:44 1.py
-rw-r--r--. 1 root root 56 10月 3 20:04 2.py
-rw-r--r--. 1 root root 5 10月 3 20:04 3.py
drwxr-xr-x. 3 root pls 4096 10月 3 17:02 test1
b. 将1.py的属主和属组均root
[root@laobai-vm-centos temp]# ll
总用量 16
-rw-r--r--. 1 plscript pls 14 10月 3 16:44 1.py
-rw-r--r--. 1 root root 56 10月 3 20:04 2.py
-rw-r--r--. 1 root root 5 10月 3 20:04 3.py
drwxr-xr-x. 3 root pls 4096 10月 3 17:02 test1
[root@laobai-vm-centos temp]# chown root:root 1.py
[root@laobai-vm-centos temp]# chown plscript:pls 2.py
[root@laobai-vm-centos temp]# ll
总用量 16
-rw-r--r--. 1 root root 14 10月 3 16:44 1.py
-rw-r--r--. 1 plscript pls 56 10月 3 20:04 2.py
-rw-r--r--. 1 root root 5 10月 3 20:04 3.py
drwxr-xr-x. 3 root pls 4096 10月 3 17:02 test1
chmod
作用:修改文件属性
实例:
a. 采取数字对1.py增加所有权限
[root@laobai-vm-centos temp]# ll
总用量 16
-rw-r--r--. 1 root root 14 10月 3 16:44 1.py
-rw-rw-rw-. 1 plscript pls 56 10月 3 20:04 2.py
-rw-r--r--. 1 root root 5 10月 3 20:04 3.py
drw-r--r--. 3 root pls 4096 10月 3 17:02 test1
[root@laobai-vm-centos temp]# chmod 777 3.py
[root@laobai-vm-centos temp]# ll
总用量 16
-rw-r--r--. 1 root root 14 10月 3 16:44 1.py
-rw-rw-rw-. 1 plscript pls 56 10月 3 20:04 2.py
-rwxrwxrwx. 1 root root 5 10月 3 20:04 3.py
drw-r--r--. 3 root pls 4096 10月 3 17:02 test1
文件和目录的权限分成三组:用户、群组、其他。每个组都有r、w、x三种权限,其中r用4表示,w用2表示,x用1表示,用r+w+x作为1位,三组即为3位数字表示三个权限组,4+2+1,每个组的最大权限数字为7,没有任何权限即为-,777表示所有用户都有rwx的权限。
b. 增加所有人的可写权限
[root@laobai-vm-centos temp]# ll
总用量 16
-rw-r--r--. 1 root root 14 10月 3 16:44 1.py
-rw-r--r--. 1 plscript pls 56 10月 3 20:04 2.py
-rw-r--r--. 1 root root 5 10月 3 20:04 3.py
drwxr-xr-x. 3 root pls 4096 10月 3 17:02 test1
[root@laobai-vm-centos temp]# chmod a+w 2.py
[root@laobai-vm-centos temp]# ll
总用量 16
-rw-r--r--. 1 root root 14 10月 3 16:44 1.py
-rw-rw-rw-. 1 plscript pls 56 10月 3 20:04 2.py
-rw-r--r--. 1 root root 5 10月 3 20:04 3.py
drwxr-xr-x. 3 root pls 4096 10月 3 17:02 test1
c. 去掉所有人的可执行权限
[root@laobai-vm-centos temp]# ll
总用量 16
-rw-r--r--. 1 root root 14 10月 3 16:44 1.py
-rw-rw-rw-. 1 plscript pls 56 10月 3 20:04 2.py
-rw-r--r--. 1 root root 5 10月 3 20:04 3.py
drwxr-xr-x. 3 root pls 4096 10月 3 17:02 test1
[root@laobai-vm-centos temp]# chmod a-x test1/
[root@laobai-vm-centos temp]# ll
总用量 16
-rw-r--r--. 1 root root 14 10月 3 16:44 1.py
-rw-rw-rw-. 1 plscript pls 56 10月 3 20:04 2.py
-rw-r--r--. 1 root root 5 10月 3 20:04 3.py
drw-r--r--. 3 root pls 4096 10月 3 17:02 test1
其中b和c中例子中的a表all所有人,u表示user用户,g表示group群组,o表示others其他人。
常用命令
查看当前系统版本
cat /etc/redhat-release
CentOS release 6.10 (Final)
查看指定程序是否在运行
ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 11:29 ? 00:00:01 /sbin/init
root 2 0 0 11:29 ? 00:00:00 [kthreadd]
root 3 2 0 11:29 ? 00:00:00 [migration/0]
root 4 2 0 11:29 ? 00:00:00 [ksoftirqd/0]
root 5 2 0 11:29 ? 00:00:00 [stopper/0]
root 6 2 0 11:29 ? 00:00:00 [watchdog/0]
root 7 2 0 11:29 ? 00:00:00 [migration/1]
root 8 2 0 11:29 ? 00:00:00 [stopper/1]
root 9 2 0 11:29 ? 00:00:00 [ksoftirqd/1]
root 10 2 0 11:29 ? 00:00:00 [watchdog/1]
root 11 2 0 11:29 ? 00:00:00 [migration/2]
root 12 2 0 11:29 ? 00:00:00 [stopper/2]
root 13 2 0 11:29 ? 00:00:00 [ksoftirqd/2]
root 14 2 0 11:29 ? 00:00:00 [watchdog/2]
root 15 2 0 11:29 ? 00:00:00 [migration/3]
root 16 2 0 11:29 ? 00:00:00 [stopper/3]
root 17 2 0 11:29 ? 00:00:00 [ksoftirqd/3]
root 18 2 0 11:29 ? 00:00:00 [watchdog/3]
.............
laobai 3345 3339 0 11:30 ? 00:00:00 python /usr/share/ibus/ui/gtk/main.py
laobai 3347 1 0 11:30 ? 00:00:00 /usr/libexec/ibus-x11 --kill-daemon
laobai 3349 3339 0 11:30 ? 00:00:00 /usr/libexec/ibus-engine-pinyin --ibus
laobai 3353 1 0 11:30 ? 00:00:00 /usr/libexec/gvfsd-metadata
laobai 3359 1 0 11:30 ? 00:00:00 /usr/libexec/gvfsd-burn --spawner :1.7 /org/gtk/gvfs/exec_spaw/1
laobai 3461 1 0 11:35 ? 00:00:00 /usr/bin/gnome-terminal -x /bin/sh -c cd '/home/laobai/桌面' && exec $SHELL
laobai 3462 3461 0 11:35 ? 00:00:00 gnome-pty-helper
laobai 3463 3461 0 11:35 pts/0 00:00:00 /bin/bash
root 3478 2238 0 11:35 ? 00:00:00 /sbin/dhclient -d -4 -sf /usr/libexec/nm-dhcp-client.action -pf /var/run/dh
root 3530 2471 0 11:36 ? 00:00:01 sshd: root@pts/1
root 3534 3530 0 11:36 pts/1 00:00:00 -bash
postfix 28424 2603 0 21:10 ? 00:00:00 pickup -l -t fifo -u
root 28439 3534 8 21:19 pts/1 00:00:00 ps -ef
还有一个类似的命令
ps -aux
终止进程
首先用上面的两个命令之一,如ps -ef
查找到要终止进程或程序的进程ID,再用kill
命令进行终止。kill
命令是向指定程序传递指令的命令,它能够传递的指令如下:
kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
终止某个进程的命令如下:
kill -9 3345
解、压缩文件
压缩文件
tar -czf laobai.tar.gz /home/laobai/test1 /home/laobai/test2
解压缩文件
tar -xvzf laobai.tar.gz
查看压缩包中的文件
tar -tzf laobai.tar.gz
通过端口号查进程
这个通常用在端口占用的排查
lsof -i:22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 2471 root 3u IPv4 16665 0t0 TCP *:ssh (LISTEN)
sshd 2471 root 4u IPv6 16671 0t0 TCP *:ssh (LISTEN)
sshd 3530 root 3r IPv4 24065 0t0 TCP laobai-vm-centos.lan:ssh->LaobaiMBP.lan:60801 (ESTABLISHED)
ssh远程登录
语法格式:ssh username@ip -p 端口号
ssh root@192.168.1.2 -p 22
ssh默认端口为22,若使用默认端口,参数-p 22
可以省略
远程拷贝文件
将远程文件拷贝到本地
scp laobai@192.168.1.2:/home/laobai/test/1.py .
将本地文件拷贝到远程服务器
scp /home/laobai/test/test1/1.py pls@192.168.1.2:/home/pls
创建文件链接
相当于windows的快捷方式
ln -s /root/temp/test/test1/test2/test3/test3.py pls.py
ll
drwx------ 5 root root 4096 Apr 19 2019 mailbox
lrwxrwxrwx 1 root root 42 Oct 3 23:17 pls.py -> /root/temp/test/test1/test2/test3/test3.py
drwxr-xr-x 19 501 501 4096 Apr 12 2019 Python-3.7.1
-rw-r--r-- 1 root root 16960060 Oct 20 2018 Python-3.7.1.tar.xz
drwxr-xr-x 3 root root 4096 Oct 3 18:50 temp
drwxr-xr-x 4 root root 4096 Oct 3 18:50 test
软件包管理 yum
Yum,全称Yellow dog Updater,Modified,是一个在Fedora和RedHat以及CentOS中的shell软件包管理工具。基于RPM包管理,能够从指定的服务器自动下载RPM包并安装,可以自动处理依赖性关系,并且一次安装所有依赖的软件包。
yum install tree
安装命令可以带参数,有2个比较常用:-y
安装过程提示选择全部为yes,-q
不显示安装过程
yum install -q -y tree
查找搜索文件
按名称查找
find -name 1.py
./home/laobai/temp/test1/1.py
./home/laobai/temp/test1/test2/1.py
./home/laobai/temp/test1/test2/test3/1.py
./home/laobai/temp/test1/test2/test3/test4/1.py
./home/laobai/temp/1.py
[root@laobai-vm-centos /]#
按照文件内容关键词查收
find -name *.py | xargs grep "selenium"
./home/laobai/temp/1.py:# from selenium.webdriver import *
./home/laobai/temp/1.py:from selenium.webdriver import Chrome, ChromeOptions, Firefox, FirefoxOptions, Edge, EdgeOptions, Safari, \
./home/laobai/temp/1.py: # 解决selenium无法访问https的问题
./root/test1.py:# from selenium.webdriver import *
./root/test1.py:from selenium.webdriver import Chrome, ChromeOptions, Firefox, FirefoxOptions, Edge, EdgeOptions, Safari, \
./root/test1.py: # 解决selenium无法访问https的问题
# 查找当前目录下包括子目录中含有“晚上使用深色模式”关键字的js文件
find . -name *.js | xargs grep "晚上使用深色模式"
grep: ./node_modules/decimal.js: Is a directory
grep: ./node_modules/highlight.js: Is a directory
./public/js/matery.js:// let toastHTML = '<span style="color:#97b8b2;border-radius: 10px;>' + '<i class="fa fa-bellaria-hidden="true"></i>晚上使用深色模式阅读更好哦。(゚▽゚)</span>'
./themes/hexo-theme-matery/source/js/matery.js:// let toastHTML = '<span style="color:#97b8b2;border-radius: 10px;>' + '<i class="fa fa-bellaria-hidden="true"></i>晚上使用深色模式阅读更好哦。(゚▽゚)</span>'
查看文件
cat
查看文件的全部内容
cat install.log
head
指定查看文件的前n行
head -n 10 install.log
tail
a. 查看文件的全部内容
tail install.log
b. 实时查看文件的新内容
tail -f install.log
c. 查看文件的最后n行内容
tail -10f install.log
c+. 查看文件并进行关键字匹配
tail -10f install.log | grep "finished"