目录
一、部分基础操作
1. 关机
halt
poweroff
shutdown -h now
init 0
2. 重启
reboot
shutdown -r now
init 6
3. man
用于查看帮助文档,info和help也用同样的作用
man [command] #查看命令的帮助
eg:
man ehco
ECHO(1) User Commands ECHO(1)
NAME
echo - display a line of text
SYNOPSIS
echo [SHORT-OPTION]... [STRING]...
echo LONG-OPTION
DESCRIPTION
Echo the STRING(s) to standard output.
-n do not output the trailing newline
-e enable interpretation of backslash escapes
-E disable interpretation of backslash escapes (default)
--help display this help and exit
--version
output version information and exit
4.tty
用于显示终端机连接标准输入设备的文件名称
tty [OPTION]...
5.who
显示系统中有哪些使用者
who - [husfV] [user]
6.hostname
常用于查看或设置主机名
SYNOPSIS
hostname [-a|--alias] [-d|--domain] [-f|--fqdn|--long] [-A|--all-
fqdns] [-i|--ip-address] [-I|--all-ip-addresses] [-s|--short]
[-y|--yp|--nis]
hostname [-b|--boot] [-F|--file filename] [hostname]
hostname [-h|--help] [-V|--version]
7.cd
用于改变当前工作目录的命令,切换到指定的路径。
cd / #切换到根目录
cd - #切换到上次访问的目录
cd ~ #切换到家目录
cd .. #切换到当前目录的父目录
cd xxx #切换到当前目录下的特定目录
cd /xxx/xx/x #切换到绝对路径指定的目录
8.和文件/目录有关的一些命令
ls #查看当前目录下的文件和目录
ls -a #查看当前目录下的所有文件和目录
ls -l #查看当前目录下的文件和文件夹的权限
ll -d #查看目录的权限
ls xxx #查看特定目录下的文件和目录
mkdir xxx #创建目录
mkdir /a/b/c/ -p #如果父目录不存在则自动创建父目录
touch a.txt #创建空文件a.txt
rm #删除文件
rm -f #删除文件(不询问)
rm -rf #递归删除文件(不询问)
mv [OPTION]... SOURCE... DIRECTORY #移动目录/文件
mv aaa bbb #将aaa重命名为bbb
mv -r /tmp/* /mnt #递归移动文件和目录
cp [options] source dest #复制文件或目录
cp -p aaa bbb #将aaa复制为bbb,并保留aaa的权限
cp -r aaa bbb #递归复制
find #查找文件或目录
SYNOPSIS
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...]
[expression]
find / -name *.sh -type f #查找根目录下所有扩展名为sh的文件
二、Linux文件系统
1.linux下的目录结构
![](https://i-blog.csdnimg.cn/blog_migrate/46f7fe3a79ff379aa3d8c02f4221bf2b.jpeg)
以下是对这些目录的解释(转载于Linux 系统目录结构):
-
/bin:
bin 是 Binaries (二进制文件) 的缩写, 这个目录存放着最经常使用的命令。 -
/boot:
这里存放的是启动 Linux 时使用的一些核心文件,包括一些连接文件以及镜像文件。 -
/dev :
dev 是 Device(设备) 的缩写, 该目录下存放的是 Linux 的外部设备,在 Linux 中访问设备的方式和访问文件的方式是相同的。 -
/etc:
etc 是 Etcetera(等等) 的缩写,这个目录用来存放所有的系统管理所需要的配置文件和子目录。 -
/home:
用户的主目录,在 Linux 中,每个用户都有一个自己的目录,一般该目录名是以用户的账号命名的,如上图中的 alice、bob 和 eve。 -
/lib:
lib 是 Library(库) 的缩写这个目录里存放着系统最基本的动态连接共享库,其作用类似于 Windows 里的 DLL 文件。几乎所有的应用程序都需要用到这些共享库。 -
/lost+found:
这个目录一般情况下是空的,当系统非法关机后,这里就存放了一些文件。 -
/media:
linux 系统会自动识别一些设备,例如U盘、光驱等等,当识别后,Linux 会把识别的设备挂载到这个目录下。 -
/mnt:
系统提供该目录是为了让用户临时挂载别的文件系统的,我们可以将光驱挂载在 /mnt/ 上,然后进入该目录就可以查看光驱里的内容了。 -
/opt:
opt 是 optional(可选) 的缩写,这是给主机额外安装软件所摆放的目录。比如你安装一个ORACLE数据库则就可以放到这个目录下。默认是空的。 -
/proc:
proc 是 Processes(进程) 的缩写,/proc 是一种伪文件系统(也即虚拟文件系统),存储的是当前内核运行状态的一系列特殊文件,这个目录是一个虚拟的目录,它是系统内存的映射,我们可以通过直接访问这个目录来获取系统信息。 -
/root:
该目录为系统管理员,也称作超级权限者的用户主目录。 -
/sbin:
s 就是 Super User 的意思,是 Superuser Binaries (超级用户的二进制文件) 的缩写,这里存放的是系统管理员使用的系统管理程序。 -
/selinux:
这个目录是 Redhat/CentOS 所特有的目录,Selinux 是一个安全机制,类似于 windows 的防火墙,但是这套机制比较复杂,这个目录就是存放selinux相关的文件的。 -
/srv:
该目录存放一些服务启动之后需要提取的数据。 -
/sys:
sysfs 文件系统集成了下面3种文件系统的信息:针对进程信息的 proc 文件系统、针对设备的 devfs 文件系统以及针对伪终端的 devpts 文件系统。
-
/tmp:
tmp 是 temporary(临时) 的缩写这个目录是用来存放一些临时文件的。 -
/usr:
usr 是 unix shared resources(共享资源) 的缩写,这是一个非常重要的目录,用户的很多应用程序和文件都放在这个目录下,类似于 windows 下的 program files 目录。 -
/usr/bin:
系统用户使用的应用程序。 -
/usr/sbin:
超级用户使用的比较高级的管理程序和系统守护程序。 -
/usr/src:
内核源代码默认的放置目录。 -
/var:
var 是 variable(变量) 的缩写,这个目录中存放着在不断扩充着的东西,我们习惯将那些经常被修改的目录放在这个目录下。包括各种日志文件。 -
/run:
是一个临时文件系统,存储系统启动以来的信息。当系统重启时,这个目录下的文件应该被删掉或清除。如果你的系统上有 /var/run 目录,应该让它指向 run。
2.linux文件属性
执行ls -al命令后会得到这些
[root@localhost ~]# ls -al
total 32
dr-xr-x---. 2 root root 151 Feb 20 08:58 .
dr-xr-xr-x. 17 root root 224 Feb 20 07:40 ..
-rw-------. 1 root root 1214 Feb 20 07:47 anaconda-ks.cfg
-rw-------. 1 root root 117 Feb 20 08:58 .bash_history
-rw-r--r--. 1 root root 18 Mar 14 2021 .bash_logout
-rw-r--r--. 1 root root 176 Mar 14 2021 .bash_profile
-rw-r--r--. 1 root root 176 Mar 14 2021 .bashrc
-rw-r--r--. 1 root root 100 Mar 14 2021 .cshrc
-rw-r--r--. 1 root root 129 Mar 14 2021 .tcshrc
-rw-------. 1 root root 1036 Feb 20 08:04 .viminfo
[root@localhost ~]#
这七个字段的的含义如下:
![](https://i-blog.csdnimg.cn/blog_migrate/210dd43d11c78da391318c9f62756266.jpeg)
文件类型权限的第一个字段表示文件的类型:
- - 普通文件
- d 目录文件director
- l 符号链接文件link
- b 块设备block
- c 字符设备character
- p 管道文件pipe
- s 套接字文件socket
接下来的字符三个为一组,分别为文件拥有者的权限、用户组的权限、其他用户的权限。
三、linux安全模型
1. 用户登录系统时必须提供用户名和密码。
2. 使用用户和用户组来控制使用者访问文件和其他资源的权限。
3. 系统上的每一个文件都一定属于一个用户(一般该用户就是文件的创建者)并与一个用户组相关。
4. 每一个进程(处理程序)也都会属于一个用户和用户组相关联。可以通过在所有的文件和资源上设定权限来只允许该文件的所有者或者某个用户组的成员访问它们。
四、文件权限
1. 文件权限对应的字符
- r (read) 可读
- w (write) 可写
- x (execute) 可执行
- - 没有权限
2. 修改文件属性与权限
以下是使用的命令:
- chgrp:修改文件所属用户组
- chown:修改文件拥有者
- chmod:修改文件权限,包括SUID、SGID、SBIT等
chgrp [-R] grpname dirname/filename #将文件或目录的所属组改为grpname
chown [-R] usrname dirname/filename #将文件或目录的所有者改为usrname
chown [-R] [usrname]:grpname dirname/filename #chown其实也有修改所属组的功能
chmod [-R] ??? dirname/filename #将文件或目录的权限改为???
3. 文件的默认权限
[root@localhost ~]# umask #与一般权限有关的是后三位
0022
[root@localhost ~]# umask -S
u=rwx,g=rx,o=rx
[root@localhost ~]# umask 002 #修改默认权限
4. 文件的影隐藏属性
4.1配置隐藏属性
[root@localhost ~]# chattr [-+=] [aAcdistu] files...
选项与参数:
A:存取文件或目录时,它的atime不会被修改
a:让文件或目录仅能增加数据,不能删改
b:不更新文件或目录的最后存取时间。
c:将文件或目录压缩后存放。
d:将文件或目录排除在倾倒操作之外。
i:不得任意更动文件或目录。
s:保密性删除文件或目录,如文件被删除,则完全的从磁盘删除。误删无法恢复
S:即时更新文件或目录。
u:预防意外删除,若文件被删除,则数据内容还在磁盘中,可以恢复
4.2查看隐藏属性
[root@localhost ~]# lsattr [ -RVadlpv ] [ files... ]
5. ACl
[root@localhost ~]# setfacl --help #添加ACl
setfacl 2.2.53 -- set file access control lists
Usage: setfacl [-bkndRLP] { -m|-M|-x|-X ... } file ...
-m, --modify=acl modify the current ACL(s) of file(s)
-M, --modify-file=file read ACL entries to modify from file
-x, --remove=acl remove entries from the ACL(s) of file(s)
-X, --remove-file=file read ACL entries to remove from file
-b, --remove-all remove all extended ACL entries
-k, --remove-default remove the default ACL
--set=acl set the ACL of file(s), replacing the current ACL
--set-file=file read ACL entries to set from file
--mask do recalculate the effective rights mask
-n, --no-mask don't 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
--restore=file restore ACLs (inverse of `getfacl -R')
--test test mode (ACLs are not modified)
-v, --version print version and exit
-h, --help this help text
[root@localhost ~]# getfacl --help #查看acl
getfacl 2.2.53 -- 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
-p, --absolute-names don't strip leading '/' in pathnames
-v, --version print version and exit
-h, --help this help text
五、vim的常见操作
vim有三种模式,即命令模式(Command Mode)、输入模式(Insert Mode)和命令行模式(Command-Line Mode)。
![](https://i-blog.csdnimg.cn/blog_migrate/8225edcf30bc31d2aa4495c6297b40ee.png)
Command Mode下的部分命令:
- i -- 切换到输入模式
- x -- 删除当前光标所在处的字符
- dd -- 删除当前行
- ndd -- 删除n行
- yy -- 复制当前行
- nyy -- 复制n行
- p -- 粘贴剪贴板内容到光标下方
- P -- 粘贴剪贴板内容到光标上方
- G -- 移动到文档的最后一行
- nG -- 移动到文档第n行
Command-Line Mode下的常用命令:
- :q -- 退出
- :w -- 保存
- :q! -- 不保存退出
- :wq -- 保存并退出
- : set num -- 显示行号