Linux 系统配置命令及重要文件
通过 上文 的学习,可以掌握到 Linux 的基本命令,实现对 Linux 系统的基本使用,如文件目录管理——增删改查。
本文着重介绍 Linux 系统中一些重要的配置文件及系统配置命令。
Linux 重要文件说明
下表给出一些常用的配置文件,及其作用
文件 | 作用 |
---|---|
/etc/hostname | 配置存放主机名 |
/etc/hosts | 本地DNS解析文件,访问域名时,系统先到该文件查询有无域名对应的IP地址 |
/etc/resolv.conf | DNS配置文件 |
/etc/service | 服务信息 |
/etc/profile | 全局变量文件,对全部用户生效 |
~/.profile | 用户变量文件,只对当前登录用户生效 |
/etc/bashrc | 全局变量文件,对全部用户生效 |
~/.bashrc | 用户变量文件,只对当前用户生效 |
/etc/issue | 开机显示的系统内核信息 |
/etc/issue.net | 开机显示的系统内核信息 |
/etc/rc.local | 开机启动执行文件 |
/etc/profile.d/* | 用户登录后执行该目录下的脚本 |
/etc/fstab | 开机执行挂载文件(不要随便修改) |
/etc/passwd | 用户配置文件 |
/etc/shadow | 用户密码配置文件 |
/etc/login.defs | 密码有效期控制文件 |
/etc/pam.d/system-auth | 密码复杂度控制文件 |
/etc/security/pwquality.conf | 密码复杂度控制文件 |
/etc/group | 组配置文件 |
/etc/gshadow | 组密码配置文件 |
/etc/sudoers | 用户权限配置文件 |
/etc/sysconfig/network-script/ifcfg-网卡名 | 网卡配置文件 |
/etc/yum.repos.d/* | yum 源配置文件 |
/var/log/messages | 系统日志文件,如用户登录,服务启动等记录 |
/var/log/secure | 安全日志文件 |
/proc/cpuinfo | 当前cpu信息文件 |
/proc/meminfo | 当前内存信息文件 |
/proc/mounts | 当前设备挂载列表信息文件 |
Linux 系统配置命令
- 网络地址查询命令
ip address
[root@centos7 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:06:72:9d brd ff:ff:ff:ff:ff:ff
inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::e022:f030:4bb1:bd0c/64 scope link noprefixroute
valid_lft forever preferred_lft forever
ifconfig
[root@centos7 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::e022:f030:4bb1:bd0c prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:06:72:9d txqueuelen 1000 (Ethernet)
RX packets 118 bytes 12432 (12.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 119 bytes 16918 (16.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
以上是两种查看网卡状态信息的命令,在网络中,若我们的主机需可以和其他主机通信,不光是需要插好网线,还需要给网卡配置正确的IP地址等信息(这里讲静态IP地址的配置,忽略dhcp方式)。
此时就需要查看配置网卡了:
[root@centos7 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet # 网络类型,一般都是以太网
BOOTPROTO=static # 获取IP地址的方式,该项有none表示无不指定,static静态地址,dhcp动态获取地址
DEVICE=eth0 # 指定网卡
NAME=eth0 # 网卡名
ONBOOT=yes # 是否开机启动
IPADDR=10.0.0.100 # 指定IP地址
PREFIX=24 # 指定网络掩码,也可以改为NETMASK=255.255.255.0
GATEWAY=10.0.0.254 # 指定网关
DNS1=223.5.5.5 # 指定DNS
DNS2=223.6.6.6 # 可以指定多个DNS
以上是网卡信息以及每个配置项的含义,具体的每项值,需要根据具体的网络环境来配置,具体配置参考 Linux 虚拟机网卡配置
- 软件安装命令
软件安装有4种方式:
- yum 安装,需要配置yum源,可以自动解决软件依赖
- rpm 安装,需要下载rpm安装包,不能自动解决软件依赖
- make 编译安装,可以自定义安装,需要可编译的软件包
- 绿色安装,安装包为可执行二进制文件,解压即可使用
- yum 安装
使用 yum 安装前需要配置 yum 源,yum 源是存放软件的的仓库,可以下载我们需要的软件进行安装,不过centos的官方yum源实在国外,可能存在下载慢的问题,所有这里给出更换yum源的方式,国内yum源也有很多,可以自行网上查找,这里给出阿里的源。
下载该yum源到/etc/yum.repos.d目录中。
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
除了yum源外还有一种epol源,有些软件在yum仓库中找不到时,可以到epol源中找到;不同的源软件仓库中的软件可能会存在差别。
下载该epol源到/etc/yum.repos.d目录中
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum 安装命令
yum install -y sl # 安装sl 软件,-y同意安装
yum reinstall -y sl # 若安装失败可以使用reinstall进行重新安装
yum install --installroot=/opt sl # --installroot=设置安装根目录
yum erase sl # 移除软件包
yum search sl # 查询某软件包
yum deplist sl # 查看sl软件包的依赖关系
yum repolist # 显示以配置yum源
- rpm 安装
rpm
- 常用命令:
- -qa 查询全部的已安装软件包,也可指定 -q 查询某软件包是否已安装
- -ivh 安装指定的软件包,以人可读的方式显示安装过程
- -ql 查询安装包文件信息
- make 安装
- 下载安装包到Linux
- 解压安装包,进入解压目录
- 执行编译,./configure 指定可配置参数进行编译,根据报错,解决依赖
- 编译安装,在目录中执行make install
- 二进制安装
由于二进制安装,下载的软件包为可执行的二进制文件,类似于/bin/ls 这类型文件,所有只要将其目录位置加入到环境变量$PATH中就可以在任意位置执行了。
例如二进制文件所在目录为/opt/app
# 临时生效,重启后需要再次执行
export PATH=$PATH:/opt/app
# 永久有效
#只对当前用户有效,将添加环境变量的命令加入到用户的环境变量文件中
echo "export PATH=$PATH:/opt/app" >> ~/.bash_profile
# 使配置立即生效
source ~/.bash_profile
# 对所有用户都有效,将添加环境变量的命令加入到全局的环境变量文件中
echo "export PATH=$PATH:/opt/app" >> /etc/profile
# 使配置立即生效
source /etc/profile
- 设备挂载命令
Linux 中一切皆文件,硬件设备也不例外;设备类文件存放目录一般都在/dev目录下。
Linux 硬盘在系统中的文件命名规则:
Linux 系统中,SATA接口的硬盘文件名格式为,第一块硬盘名为sda,第二块硬盘名为sdb,第三块硬盘名为sdc…依次类推,而每块硬盘上最多有4个主分区,若需要分配多于4个分区,则需要分配3个主分区,1个扩展分区,扩展分区中可以分配多个逻辑分区,而每个分区的文件名依次为sda1,sda2… , sda1-4是留给主分区使用的,即使没有分配足够的主分区,逻辑分区也是从5开始分配分区号,也就是逻辑分区都是从sda5开始命名的。
前面讲到yum 源的使用,其实还有一种yum源叫做本地镜像源,即使用Linux安装镜像作为yum 源,这时就需要我们把存放Linux的镜像挂载到系统中,这样才可以为yum配置本地源。
- 在虚拟机中为Linux虚拟机设置Linux镜像。
- 为虚拟机添加Linux镜像后,由于添加的是CD,所有显示在虚拟中文件名为/dev/cdrom ,通过ll查看可以看出它是sr0设备的一个链接文件。
[root@centos7 /dev]# ll cdrom
lrwxrwxrwx. 1 root root 3 9月 27 15:38 cdrom -> sr0
- 挂载准备,挂载设备就相当于给设备开一个门,可以让人在通过门去访问它,所以我们需要一个空目录来作为挂载点,通过该目录来访问设备。
# /mnt是一个临时挂载点,在里边创建cdrom目录准备挂载sr0设备
[root@centos7 /dev]# cd /mnt
[root@centos7 /mnt]# ls
hgfs temp
[root@centos7 /mnt]# mkdir cdrom
[root@centos7 /mnt]# tree
.
├── cdrom
├── hgfs
└── temp
- 挂载
[root@centos7 /mnt]# mount /dev/sr0 cdrom/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos7 /mnt]# tree -L 2
.
├── cdrom
│ ├── CentOS_BuildTag
│ ├── EFI
│ ├── EULA
│ ├── GPL
│ ├── images
│ ├── isolinux
│ ├── LiveOS
│ ├── Packages
│ ├── repodata
│ ├── RPM-GPG-KEY-CentOS-7
│ ├── RPM-GPG-KEY-CentOS-Testing-7
│ └── TRANS.TBL
├── hgfs
└── temp
9 directories, 6 files
以上,我们将/dev/sr0设备挂载到了/mnt/cdrom下,可以访问到该设备中的文件了。
- 配置yum本地源
[root@centos7 /etc/yum.repos.d]# cd /etc/yum.repos.d/
[root@centos7 /etc/yum.repos.d]# vim local.repo
[local]
name=local
baseurl=file:///mnt/cdrom
enabled=1
gpgcheck=0
# 清空缓存
[root@centos7 /etc/yum.repos.d]# yum clean all
# 建立元数据缓存,加快软件搜索安装速度
[root@centos7 /etc/yum.repos.d]# yum makecache
系统服务管理
- 启动服务
systemctl start httpd
- 停止服务
systemctl stop httpd
- 重启服务
systemctl restart httpd
- 查看服务状态
systemctl status httpd
- 开启服务开机自启
systemctl enable httpd
- 关闭服务开机自启
systemctl disable httpd
- 查看当前有哪些进程在运行
ps
- 常用参数:
- ps -ef 查看所有进程
- 结束进程
kill
结束指定pid进程,pid可通过ps -ef查询得到
- 常用参数:
- -p 指定pid号
pkill
结束指定名称的进程
- 结束当前运行任务,停止当前运行任务
ctrl + c 结束中断当前任务
ctrl + z 停止当前任务,将任务放到后台
- 管理后台任务
jobs
查看后台任务
- 常用参数:
-l 可以看到进程pid
[root@centos7 ~]# jobs
[1]+ 已停止 top
[root@centos7 ~]# jobs -l
[1]+ 20628 停止 (信号) top
结束后台任务的两种方式:
kill %1
或kill 20628
bg
移动任务到后台执行,相当于在命令后加了 &(后台运行)
bg %1
fg
将任务移动到前台
fg %1
实操命令:
[root@centos7 ~]# nmap -T4 192.168.18.0/24
Starting Nmap 6.40 ( http://nmap.org ) at 2020-09-27 21:27 CST
[2]+ 已停止 nmap -T4 192.168.18.0/24
[root@centos7 ~]# jobs
[1]- 已停止 top
[2]+ 已停止 nmap -T4 192.168.18.0/24
[root@centos7 ~]# bg
[2]+ nmap -T4 192.168.18.0/24 &
[root@centos7 ~]# jobs
[1]+ 已停止 top
[2]- 运行中 nmap -T4 192.168.18.0/24 &
[root@centos7 ~]# fg %2
nmap -T4 192.168.18.0/24
[root@centos7 ~]# jobs
[1]- 已停止 top
[2]+ 已停止 nmap -T4 192.168.18.0/24
[root@centos7 ~]# kill %1
[root@centos7 ~]# jobs
[1]- 完成 top
[2]+ 已停止 nmap -T4 192.168.18.0/24
[root@centos7 ~]# fg %1
-bash: fg: %1: 无此任务
[root@centos7 ~]# jobs
[2]+ 已停止 nmap -T4 192.168.18.0/24
[root@centos7 ~]# bg %2
[2]+ nmap -T4 192.168.18.0/24 &
[root@centos7 ~]# jobs
[2]+ 运行中 nmap -T4 192.168.18.0/24 &
[root@centos7 ~]# jobs -l
[2]+ 20684 运行中 nmap -T4 192.168.18.0/24 &
[root@centos7 ~]# kill 20684
[root@centos7 ~]# jobs
[2]+ 已终止 nmap -T4 192.168.18.0/24
[root@centos7 ~]# jobs
用户管理
- 查看当前登录服务器的所有用户
who
[root@centos7 ~]# who
root pts/0 2020-09-27 21:12 (10.0.0.1)
root pts/1 2020-09-27 17:51 (10.0.0.1)
w
[root@centos7 ~]# w
21:33:28 up 5:54, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 10.0.0.1 21:12 14:56 0.05s 0.04s -bash
root pts/1 10.0.0.1 17:51 0.00s 1.01s 0.00s w
- 查看当前登录用户信息
whoami
[root@centos7 ~]# whoami
root
id
环境后边出现的内容是和SELinux有关的
[root@centos7 ~]# id
uid=0(root) gid=0(root) 组=0(root) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[root@centos7 ~]# getenforce # 查看SELinux状态
Enforcing
[root@centos7 ~]# setenforce 0 # 临时关闭SELinux
[root@centos7 ~]# getenforce
Permissive
[root@centos7 ~]# vim /etc/selinux/config
# 将其中的SELINUX改为disabled,重启即可
[root@centos7 ~]# id
uid=0(root) gid=0(root) 组=0(root)
- 添加用户
useradd
添加用户
- 常用参数:
- -g 指定新用户主组id或名称
- -G 指定新用户的附加组
- -M 不创建新用户的主目录
- -m 创建新用户主目录
- -u 新用户的UID
- -s 指定新用户登录的shell
- -r 创建一个系统账户
- -p 指定密码(加密后的密码)
使用useradd test
来创建用户时,默认会给该用户在/home下创建同名的家目录,uid号重1001递增。
- 删除用户
userdel
删除指定用户
- 常用参数:
- -r 删除用户家目录
[root@centos7 ~]# useradd test
[root@centos7 ~]# cat /etc/passwd # 该文件内存放的是用户信息
...
用户名:密码占位符:UID:组ID:0::家目录:登录解释器位置
test:x:1001:0::/home/test:/bin/bash
...
- 为新建用户添加密码
passwd
为用户修改密码,默认为当前用户。
- 常用参数:
- -S 显示密码信息
- -g 修改组密码
- -d 删除指定用户密码
- - -stdin 指定可以从stdin(标准输入中读取密码,可以通过管道来设置)
chpasswd
批量修改密码,执行命令后以 “user:passwd” 的格式输入即可
也可使用管道来修改密码,echo "user:passwd" | chpasswd
[root@centos7 ~]# cat /etc/shadow # 存放用户密码的文件,只有root用户有读取权限
...
test::18533:0:99999:7:::
...
[root@centos7 ~]# echo "123456" | passwd --stdin test
更改用户 test 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@centos7 ~]# cat /etc/shadow
...
test:$6$UlrSpOhe$HWsQbZfr0v15Be.6f0EFDP0uLaV2In9BihDUqQj0ay9IRHWY1VwEXtBSsCOSCzgCZ1VNvbrqwTYCOPvkcbFED1:18533:0:99999:7:::
...
[root@centos7 ~]# passwd test # 交互式修改密码
更改用户 test 的密码 。
新的 密码:
- 修改用户属性信息
usermod
修改用户属性信息,如所属组
- 常用参数:
- -l 新的额登录名称
- -d 用户的新主目录
- -m 将家目录移动到新的位置,仅和-d参数一起使用
- -g 修改用户组
- -G 加入新的附加用户组
- -L 锁定用户账户
- -U 解锁用户账户
- 切换用户与用户提权(用户权限管理)
- 切换用户
Linux系统是一个多用户的操作系统,我们在操作中可能需要切换到其他用户进行操作,这时我们就需要执行切换用户的命令,否则还需要注销退出登录,比较不方便
su
切换用户命令
使用方法:
su - user
切换到指定用户
su -
切换到root用户
- 用户提权
在Linux系统中,root用户属于管理员,有且只有一个,在系统中他有绝对的权力,可以干任何事情,而其他的普通用户则在使用权限上,处处受限,当需要使用一些特殊的命令时,必须有 root 用户权限才可以执行,而root用户的账号密码又不能随意交给其他人,这时我们想要普通用户以root权限去执行的某命令时,就需要为该用户提权;
sudo
为提权命令,但是在使用前我们需要对 /etc/sudoers 文件进行配置,为目标用户授予以 root 权限执行某命令的权限。
sudo -l
查看已提升的权限
执行visudo命令修改 /etc/sudoers 配置文件,具有检查配置语法错误的功能,推荐使用该命令直接配置。
[root@centos7 /]# visudo
# 在文件中找到该行
root ALL=(ALL) ALL
① ② ③
① 指定赋予权限普通用户名称
② 表示可以实现权限信息集中管理
③ 设定特殊权利命令,需要指定命令的绝对路径,若不知道命令绝对路径可使用which 命令 来查询。
如我们为centos用户授予以root权限执行cat命令的权限:
[root@centos7 /]# visudo
# 在文件中找到该行
root ALL=(ALL) ALL
oldboy ALL=(ALL) /usr/bin/cat
# 未使用sudo提权,显示权限不够
[centos@centos7 /tmp]$ cat /etc/shadow
cat: /etc/shadow: 权限不够
# sudo提权执行cat命令,可以看到/etc/shadow文件内容
[centos@centos7 /tmp]$ sudo cat /etc/shadow
[sudo] centos 的密码:
root:$6$Kg7Q8eFR$jJskKQd7yQJv6GMAieNxqeWMcwAazcnLJ.i1uOKgAEuwvaXCVCJbznBd1:18506:0:99999:7:::
bin:*:17834:0:99999:7:::
daemon:*:17834:0:99999:7:::
...
为用户授予多个权限,或限定只能执行具体命令的权限。
# 如此,centos 用户就只能使用vim命令提权编辑网卡文件了,而不是所有文件都可以编辑
[root@centos7 /]# visudo
# 在文件中找到该行
root ALL=(ALL) ALL
centos ALL=(ALL) /usr/sbin/useradd, /usr/bin/vim /etc/sysconfig/network-scripts/ifcfg-eth0
由于执行 sudo 命令每间隔一段时间需要输入用户密码,可以通过配置 /etc/sudoers 文件来设置执行 sudo 提权时不需要输入用户密码。
# 在执行命令前添加NOPASSWD: 即可
[root@centos7 /]# visudo
# 在文件中找到该行
root ALL=(ALL) ALL
centos ALL=(ALL) NOPASSWD:/usr/sbin/useradd, /usr/bin/vim /etc/sysconfig/network-scripts/ifcfg-eth0
权限管理
Linux 文件和目录的权限有 r 可读 ,w 可写 和 x 可执行。
而文件和目录对于这三种权限又分别有不同的含义:
在介绍文件和目录权限前,需要了解一个修改文件目录权限的命令,chmod
chmod
修改文件及目录权限
- 常用参数:(r/w/x权限可一次添加赋予或取消多个):
- 修改所属主(user)权限:u+r/w/x 为所属主添加 r 或 w 或 x 权限;u-r/w/x 取消所属主的 r 或 w 或 x 权限;u=r/w/x 为所属主赋予 r 或 w 或 x 权限
- 修改所属组(group)权限:g+r/w/x 为所属组添加 r 或 w 或 x 权限;g-r/w/x 取消所属组的 r 或 w 或 x 权限;g=r/w/x 为所属组赋予 r 或 w 或 x 权限
- 修改其他用户(other)权限:o+r/w/x 为其他用户添加 r 或 w 或 x 权限;o-r/w/x 取消其他用户的 r 或 w 或 x 权限;o=r/w/x 为其他用户赋予 r 或 w 或 x 权限
- 可以同时给多个权限,
chmod u=rwx,g=r,o=r test.txt
- 除了指定 rwx 来赋予权限,rwx还有一种数值表示方式,r = 4,w = 2,x = 1,若同时赋予多种权限,可以3个为一组对对应的权限做加法,如 drwxr-xr-x 2 centos root 6 9月 28 16:21 test 使用数值表示为 755,此时你和别人说该文目录权限为 755 时,别人就知道,该目录的权限为:所属主拥有rwx权限,所属组拥有rx权限,其他用户拥有rx权限了。使用数值来修改权限:
chmod 775 test
[centos@centos7 ~]$ ll
总用量 4
drwxr-xr-x 2 centos root 6 9月 28 16:21 test
-rw-r----- 1 centos root 10 9月 28 16:31 test.txt
ls -l 命令输出结果
第一列 -rw-r–r–. 中,第一个字符,- 表示文件,d表示目录,l表示链接文件(类似于windows中的快捷方式),后面的字符表示权限,以3个字符一组,分别表示,所属主权限,所属组权限,其他权限,r表示读权限,w表示写权限,x表示执行权限
第三列test表示所属主
第四列root表示所属组
第五列表示文件大小
第六列表示文件内容修改时间
最后一列表示文件名
由于root用户对Linux系统有绝对的控制权,所以在测试了解这些权限的特性前需要创建一个普通用户,由普通用户来进行测试。
- 文件的 r w x 权限
- 为防止干扰,只给文件的所属主一个读权限
[root@centos7 /home/centos]# chmod 400 test.txt
[root@centos7 /home/centos]# ll test.txt
-r-------- 1 centos root 19 9月 28 17:02 test.txt
[root@centos7 /home/centos]# cat test.txt
echo "Hello world"
# 使用echo向文件中写入内容提示没有权限
[centos@centos7 ~]$ echo asd > test.txt
-bash: test.txt: 权限不够
# 使用vim可写入内容强制保存退出,是可以写入文件的
[centos@centos7 ~]$ vim test.txt
[centos@centos7 ~]$ cat test.txt
abc
# 执行该文件,显示没有权限
[centos@centos7 ~]$ ./test.txt
-bash: ./test.txt: 权限不够
- 只给文件所属主写权限
[centos@centos7 ~]$ chmod u=w test.txt
[centos@centos7 ~]$ ll test.txt
--w------- 1 centos root 15 9月 28 17:29 test.txt
# 使用cat查看文件,显示为没有权限
[centos@centos7 ~]$ cat test.txt
cat: test.txt: 权限不够
# 执行该文件,仍无权限
[centos@centos7 ~]$ ./test.txt
-bash: ./test.txt: 权限不够
[centos@centos7 ~]$ vim test.txt
尝试使用vim编辑文件,但是看不到原文件内容,向其中写入一些内容,强制保存退出。
# 将文件权限改为可读,查看是否写入内容
[centos@centos7 ~]$ chmod u=r test.txt
-r-------- 1 centos centos 4 9月 28 17:37 test.txt
# 可以看到vim写入的内容存在,但是原文件内容不存在了
[centos@centos7 ~]$ cat test.txt
ABC
[centos@centos7 ~]$ chmod u=w test.txt
[centos@centos7 ~]$ ll test.txt
--w------- 1 centos centos 4 9月 28 17:37 test.txt
# 尝试echo 追加文件内容
[centos@centos7 ~]$ echo abc >> test.txt
[centos@centos7 ~]$ chmod u=r test.txt
[centos@centos7 ~]$ ll test.txt
-r-------- 1 centos centos 8 9月 28 17:42 test.txt
# 可以看到,使用echo追加是可以保留原文件内容并追加
[centos@centos7 ~]$ cat test.txt
ABC
abc
- 只给文件所属主写权限
# 将Linux命令写入到test.sh中
[centos@centos7 ~]$ echo "echo 'hello world'" > test.sh
[centos@centos7 ~]$ cat test.sh
echo 'hello world'
# 只给test.sh 可执行权限
[centos@centos7 ~]$ chmod u=x test.sh
[centos@centos7 ~]$ ll test.sh
---x------ 1 centos centos 19 9月 28 17:47 test.sh
# 执行test.sh文件显示权限不够
[centos@centos7 ~]$ ./test.sh
bash: ./test.sh: 权限不够
# 由于执行文件需要读取文件内容,所以我们必须给他一个r权限才可以执行
[centos@centos7 ~]$ chmod u+r test.sh
[centos@centos7 ~]$ ll test.sh
-r-x------ 1 centos centos 19 9月 28 17:47 test.sh
[centos@centos7 ~]$ ./test.sh
hello world
- 总结
- 文件的读权限是指,加载读取文件的能力
- 文件的写权限是指,向文件写入数据的能力
- 文件的执行权限是指,执行文件内容,但是需要加载文件内容,所以至少还需要有读权限才可以执行。
- 目录的 r w x 权限
- 只给目录所属主读权限
[centos@centos7 ~]$ ll
总用量 0
dr-------- 2 centos root 6 9月 28 16:21 test
# 无法切换到目录中
[centos@centos7 ~]$ cd test
-bash: cd: test: 权限不够
# 当目录只有读权限时,无法查看目录内文件的属性,只能看到文件名,也不能查看文件内容
[centos@centos7 ~]$ ll test
ls: 无法访问test/test.txt: 权限不够
总用量 0
-????????? ? ? ? ? ? test.txt
[centos@centos7 ~]$ cat test/test.txt
cat: test/test.txt: 权限不够
# 无法在目录中创建文件和目录
[centos@centos7 ~]$ touch test/test1
touch: 无法创建"test/test1": 权限不够
[centos@centos7 ~]$ mkdir test/test1
mkdir: 无法创建目录"test/test1": 权限不够
# 无法删除目录
[centos@centos7 ~]$ rm test -rf
rm: 无法删除"test/test.txt": 权限不够
- 只给目录所属主写权限
[centos@centos7 ~]$ chmod u=w test
[centos@centos7 ~]$ ll
总用量 0
d-w------- 2 centos root 22 9月 28 19:02 test
# 可以看到,当目录只有读权限时,什么时区都不能干
[centos@centos7 ~]$ cd test
-bash: cd: test: 权限不够
[centos@centos7 ~]$ ls test
ls: 无法打开目录test: 权限不够
[centos@centos7 ~]$ touch test/test1
touch: 无法创建"test/test1": 权限不够
[centos@centos7 ~]$ mkdir test/test1
mkdir: 无法创建目录"test/test1": 权限不够
[centos@centos7 ~]$ rm -rf test
rm: 无法删除"test": 权限不够
- 只给目录所属主执行权限
[centos@centos7 ~]$ chmod u=x test
[centos@centos7 ~]$ ll
总用量 0
d--x------ 2 centos root 22 9月 28 19:02 test
# 当目录拥有可执行权限时,可以切换到目录中
[centos@centos7 ~]$ cd test
[centos@centos7 ~/test]$ pwd
/home/centos/test
# 无法查看目录中有哪些文件
[centos@centos7 ~/test]$ ls
ls: 无法打开目录.: 权限不够
# 没有权限在目录中创建文件及目录
[centos@centos7 ~/test]$ touch test1
touch: 无法创建"test1": 权限不够
[centos@centos7 ~/test]$ mkdir test1
mkdir: 无法创建目录"test1": 权限不够
- 只给目录所属主可执行和可读权限
[centos@centos7 ~]$ chmod u=rx test
[centos@centos7 ~]$ ll
总用量 0
dr-x------ 2 centos root 22 9月 28 19:02 test
[centos@centos7 ~]$ cd test
# 有可执行权限后,可以看到文件的属性信息
[centos@centos7 ~/test]$ ll
总用量 0
-rw-r--r-- 1 root root 0 9月 28 19:02 test.txt
# 无法创建文件及目录
[centos@centos7 ~/test]$ touch test1
touch: 无法创建"test1": 权限不够
[centos@centos7 ~/test]$ mkdir test1
mkdir: 无法创建目录"test1": 权限不够
# 无法给目录中的文件重命名
[centos@centos7 ~/test]$ mv test.txt test.txt.bak
mv: 无法将"test.txt" 移动至"test.txt.bak": 权限不够
[centos@centos7 ~/test]$ rm test1 -f
rm: 无法删除"test1": 权限不够
- 只给目录所属主可执行和可写权限
[centos@centos7 ~]$ chmod u=wx test
[centos@centos7 ~]$ ll
总用量 0
d-wx------ 2 centos root 22 9月 28 19:02 test
[centos@centos7 ~]$ cd test
# 无法读取目录中的内容
[centos@centos7 ~/test]$ ll
ls: 无法打开目录.: 权限不够
# 可以创建文件和目录
[centos@centos7 ~/test]$ touch test1
[centos@centos7 ~/test]$ mkdir test2
[centos@centos7 ~/test]$ ll
ls: 无法打开目录.: 权限不够
# 即使我知道有哪些文件,也无法对这些文件操作
[centos@centos7 ~]$ mv test1 test11
mv: 无法获取"test1" 的文件状态(stat): 没有那个文件或目录
# 若已知目录中的文件名,可以删除文件
[centos@centos7 ~/test]$ rm test1
[centos@centos7 ~/test]$ chmod u+r ../test
[centos@centos7 ~/test]$ ls
test2 test.txt
- 只给目录所属主可写和可读权限
[centos@centos7 ~]$ chmod u=rw test
[centos@centos7 ~]$ ll
总用量 0
drw------- 3 centos root 48 9月 28 19:24 test
# 可以查看目录中的内容的名称,不能显示属性信息
[centos@centos7 ~]$ ll test
ls: 无法访问test/test.txt: 权限不够
ls: 无法访问test/test1: 权限不够
ls: 无法访问test/test2: 权限不够
总用量 0
-????????? ? ? ? ? ? test1
d????????? ? ? ? ? ? test2
-????????? ? ? ? ? ? test.txt
# 无法
[centos@centos7 ~]$ touch test/test3
touch: 无法创建"test/test3": 权限不够
[centos@centos7 ~]$ mkdir test/test3
mkdir: 无法创建目录"test/test3": 权限不够
- 总结
- 若要读取目录中内容的名称,只需要给目标目录读权限;
- 若要读取目录中内容的属性信息则需要给文件可执行权限;
- 若要切换到目录中,至少要给目录一个可执行权限
- 若要在目录中执行删除操作,只需要知道目录中的文件名并给该目录可执行和可写权限
- 修改文件目录的所属主及所属组
- 修改所属主(需要root权限)
chown
修改文件目录的所属主或所属组
- 常用参数:
- R 递归设置目录下所有文件所属主
命令格式:
chown 用户名 文件或目录名
修改文件或目录的所属主
chown 用户名.用户组 文件或目录名
同时修改文件或目录的所属主或所属组
[root@centos7 ~]# ll test -d
drwxr-xr-x 2 root root 6 9月 28 16:16 test
[root@centos7 ~]# chown centos test
[root@centos7 ~]# ll test -d
drwxr-xr-x 2 centos root 6 9月 28 16:16 test
[root@centos7 ~]# chown root.centos test
[root@centos7 ~]# ll test -d
drwxr-xr-x 2 root centos 6 9月 28 16:16 test
- 修改所属组(需要root权限)
chgrp
修改文件或目录所属组
[root@centos7 ~]# ll test -d
drwxr-xr-x 2 root centos 6 9月 28 16:16 test
[root@centos7 ~]# chgrp root test
[root@centos7 ~]# ll test -d
drwxr-xr-x 2 root root 6 9月 28 16:16 test
- 特殊权限
- setuid与setgid
setuid和setgid 一般用于可执行文件,当其他用户执行拥有该权限的文件时,会以该文件所属主或所属组的身份运行改文件。
该权限的应用场景:
如passwd
命令,每个用户都需要使用它来修改自己的密码并把对应的信息写入到 /etc/shadow 中,所以若是要让每个用户都可以自己修改密码,则需要给他root权限,因为只有root用户才有权限编辑 /etc/shadow 的;这时,如果我们给 /bin/passwd 文件一个 setuid 权限,所有用户执行该命令时就会以 root 身份来运行了。
setuid 权限的字符表示为 s 数值表示为 4,可通过 4777 表示该文件具有setuid权限,且其他权限为 777
setgid 权限的字符表示为 s 数值表示为 2,可通过 2777 表示该文件具有setgid权限,且其他权限为 777
若没有 setuid或setgid或粘滞位 权限可以设置为0777
[centos@centos7 ~]$ ll /bin/passwd
-rwsr-xr-x. 1 root root 27856 4月 1 11:57 /bin/passwd
[centos@centos7 ~]$ ll /bin/top
-rwxr-xr-x. 1 root root 106880 4月 1 12:10 /bin/top
# 以centos普通用户运行两个命令,使用ps -ef 获取进程用户
[centos@centos7 ~]$ ps -ef | grep passwd
root 3235 2151 0 21:45 pts/0 00:00:00 passwd
[centos@centos7 ~]$ ps -ef | grep top
centos 3247 2151 0 21:46 pts/0 00:00:00 top
# 取消/bin/passwd 文件的setuid权限
[root@centos7 ~]# chmod u-s /bin/passwd
[root@centos7 ~]# ll /bin/passwd
-rwxr-xr-x. 1 root root 27856 4月 1 11:57 /bin/passwd
[centos@centos7 ~]$ passwd
更改用户 centos 的密码 。
为 centos 更改 STRESS 密码。
(当前)UNIX 密码:
[1]+ 已停止 passwd
[centos@centos7 ~]$ ps -ef | grep passwd
centos 1789 1736 0 13:12 pts/1 00:00:00 passwd
# 没有权限执行
[centos@centos7 ~]$ echo "1234abcd" | passwd --stdin
只有根用户才能进行此操作。
- 粘滞位
粘滞位一般设置给目录,共享目录,可以防止用户删除或重命名其他用户的文件。
设置粘滞位方式:
粘滞位的字符表示方法为 t ,数值表示方式为 1
同 setuid 可以通过 chmod 来设置该目录的粘滞位:
chmod o+t /tmp
chmod 1777 /tmp
若没有 setuid或setgid或粘滞位 权限可以设置为0777
# 可以看到该目录具有 粘滞位 t 权限
[root@centos7 /]# ll /tmp -d
drwxrwxrwt. 7 root root 93 9月 29 13:22 /tmp
# 使用centos 普通用户操作该文件,该文件具有777权限
[centos@centos7 /tmp]$ ll
总用量 728
-rwxrwxrwx 1 test root 744708 9月 29 13:25 PHP.png
[centos@centos7 /tmp]$ more PHP.png
PNG
ߓ©hͣnm¤yyob£«鴞®L¢ٌ睬\5~ֳޫw4¸.ͳL飮ح-More--(0%)
˓%^¼«6
--More--(0%)
[centos@centos7 /tmp]$ echo abc >PHP.png
[centos@centos7 /tmp]$ cat PHP.png
abc
# 可以看出其他用户虽有读和写的权限,但是不能删除该文件
[centos@centos7 /tmp]$ rm PHP.png
rm: 无法删除"PHP.png": 不允许的操作
# 无法修改文件名
[centos@centos7 /tmp]$ mv PHP.png php.jpg
mv: 无法将"PHP.png" 移动至"php.jpg": 不允许的操作