特殊权限
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
1. setuid 权限位作用于属主的位置 可执行的权限位 执行这条命令的时候,相当于这条命令的所有者 root s 表示 [root@qls ~]# ll /usr/bin/passwd -rwsr-xr-x. 1 root root 27832 Jun 10 2014 /usr/bin/passwd [root@qls ~]# ll /usr/bin/rm -rwxr-xr-x. 1 root root 62952 Oct 31 2018 /usr/bin/rm [root@qls ~]# su - qls01 Last login: Thu Jul 23 12:24:52 CST 2020 from 10.0.0.1 on pts/4 [qls01@qls ~]$ rm -rf /opt/ rm: cannot remove ‘/opt/’: Permission denied [qls01@qls ~]$ ll -d / dr-xr-xr-x. 17 root root 224 Jul 22 10:21 / [qls01@qls ~]$ logout [root@qls ~]# rm -rf /opt/ [root@qls ~]# ll /opt/ ls: cannot access /opt/: No such file or directory [root@qls ~]# mkdir /opt/ [root@qls ~]# chmod 557 / [root@qls ~]# ll -d / dr-xr-xrwx. 17 root root 224 Jul 24 08:38 / [root@qls ~]# su - qls01 Last login: Fri Jul 24 08:37:21 CST 2020 on pts/0 [qls01@qls ~]$ rm -rf /opt/ [qls01@qls ~]$ logout [root@qls ~]# chmod -w / chmod: /: new permissions are r-xr-xrwx, not r-xr-xr-x [root@qls ~]# chmod o-w / [root@qls ~]# ll -d / dr-xr-xr-x. 16 root root 213 Jul 24 08:39 / [root@qls ~]# ll /usr/bin/rm -rwxr-xr-x. 1 root root 62952 Oct 31 2018 /usr/bin/rm [root@qls ~]# chmod u+s /usr/bin/rm [root@qls ~]# ll /usr/bin/rm -rwsr-xr-x. 1 root root 62952 Oct 31 2018 /usr/bin/rm [root@qls ~]# mkdir /opt [root@qls ~]# su - qls01 Last login: Fri Jul 24 08:39:42 CST 2020 on pts/0 [qls01@qls ~]$ rm -rf /opt/ [qls01@qls ~]$ logout [root@qls ~]# mkdir /opt [root@qls ~]# chmod u-s /usr/bin/rm [root@qls ~]# ll /usr/bin/yum -rwxr-xr-x. 1 root root 801 Nov 5 2018 /usr/bin/yum [root@qls ~]# su - qls01 Last login: Fri Jul 24 08:42:10 CST 2020 on pts/0 [qls01@qls ~]$ yum install -y sl Loaded plugins: fastestmirror You need to be root to perform this command. [qls01@qls ~]$ yum install -y mysql-server Loaded plugins: fastestmirror You need to be root to perform this command. [qls01@qls ~]$ logout [root@qls ~]# ll /usr/bin/yum -rwxr-xr-x. 1 root root 801 Nov 5 2018 /usr/bin/yum [root@qls ~]# chmod u+s /usr/bin/yum [root@qls ~]# ll /usr/bin/yum -rwsr-xr-x. 1 root root 801 Nov 5 2018 /usr/bin/yum [root@qls ~]# su - qls01 Last login: Fri Jul 24 08:45:28 CST 2020 on pts/0 [qls01@qls ~]$ yum install -y mysql-server Loaded plugins: fastestmirror You need to be root to perform this command. #普通用户不能使用yum,命令,即使添加了setuid的权限也不行 #此权限不能乱给 知道就行 [root@qls ~]# ll /usr/bin/cat -rwxr-xr-x. 1 root root 54160 Oct 31 2018 /usr/bin/cat [root@qls ~]# stat /usr/bin/cat File: ‘/usr/bin/cat’ Size: 54160 Blocks: 112 IO Block: 4096 regular file Device: 803h/2051d Inode: 201349408 Links: 1 Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2020-07-24 08:52:48.370831557 +0800 Modify: 2018-10-31 03:16:01.000000000 +0800 Change: 2020-07-24 08:52:58.961832157 +0800 Birth: - [root@qls ~]# stat /usr/bin/passwd File: ‘/usr/bin/passwd’ Size: 27832 Blocks: 56 IO Block: 4096 regular file Device: 803h/2051d Inode: 201636086 Links: 1 Access: (4755/-rwsr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2020-07-23 09:58:46.365163401 +0800 Modify: 2014-06-10 14:27:56.000000000 +0800 Change: 2020-07-06 02:14:21.159994247 +0800 Birth: - setuid的权限是4开头的 [root@qls ~]# ll /usr/bin/passwd -rwsr-xr-x. 1 root root 27832 Jun 10 2014 /usr/bin/passwd [root@qls ~]# chmod -x /usr/bin/cat [root@qls ~]# ll /usr/bin/cat -rw-r--r--. 1 root root 54160 Oct 31 2018 /usr/bin/cat [root@qls ~]# chmod u+s /usr/bin/cat [root@qls ~]# ll /usr/bin/cat -rwSr--r--. 1 root root 54160 Oct 31 2018 /usr/bin/cat [root@qls ~]# chmod +x /usr/bin/cat [root@qls ~]# ll /usr/bin/cat -rwsr-xr-x. 1 root root 54160 Oct 31 2018 /usr/bin/cat [root@qls ~]# chmod u-s /usr/bin/cat [root@qls ~]# 大S 和 小s的区别 拥有执行权限,设置setuid之后是小s 没有执行权限,设置setuid之后是大S 2. setgid 权限为作用在属组的x为 使用 s表示 有大S 和小s 用户在某个目录下新创建的目录或者文件,默认的所属组是自己的基本组 当设置setgid之后,用户新创建的目录或者文件的默认所属组不在是自己的组了 默认的组就是这个目录的所属组 让多个用户能够共享一个目录 [root@qls ~]# mkdir /data [root@qls ~]# ll -d /data drwxr-xr-x 2 root root 6 Jul 24 09:06 /data [root@qls ~]# chmod 770 /data [root@qls ~]# ll -d /data drwxrwx--- 2 root root 6 Jul 24 09:06 /data [root@qls ~]# groupadd ops_group [root@qls ~]# chgrp ops_group /data/ [root@qls ~]# ll -d /data/ drwxrwx--- 2 root ops_group 6 Jul 24 09:06 /data/ [root@qls ~]# useradd ops01 [root@qls ~]# useradd ops02 [root@qls ~]# useradd ops03 [root@qls ~]# id ops03 uid=1046(ops03) gid=1048(ops03) groups=1048(ops03) [root@qls ~]# usermod -aG ops_group ops01 [root@qls ~]# usermod -aG ops_group ops02 [root@qls ~]# usermod -aG ops_group ops03 [root@qls ~]# su - ops01 [ops01@qls ~]$ touch /data/ops01.txt [ops01@qls ~]$ ll /data/ops01.txt -rw-rw-r-- 1 ops01 ops01 0 Jul 24 09:10 /data/ops01.txt [ops01@qls ~]$ logout [root@qls ~]# su - ops02 [ops02@qls ~]$ touch /data/ops02.txt [ops02@qls ~]$ ll /data/ops02.txt -rw-rw-r-- 1 ops02 ops02 0 Jul 24 09:11 /data/ops02.txt [ops02@qls ~]$ logout [root@qls ~]# su - ops03 [ops03@qls ~]$ touch /data/ops03.txt [ops03@qls ~]$ ll /data/ops03.txt -rw-rw-r-- 1 ops03 ops03 0 Jul 24 09:11 /data/ops03.txt [ops03@qls ~]$ logout [root@qls ~]# ll /data/ total 0 -rw-rw-r-- 1 ops01 ops01 0 Jul 24 09:10 ops01.txt -rw-rw-r-- 1 ops02 ops02 0 Jul 24 09:11 ops02.txt -rw-rw-r-- 1 ops03 ops03 0 Jul 24 09:11 ops03.txt [root@qls ~]# chmod g+s /data/ [root@qls ~]# ll -d /data/ drwxrws--- 2 root ops_group 57 Jul 24 09:11 /data/ [root@qls ~]# stat /data/ File: ‘/data/’ Size: 57 Blocks: 0 IO Block: 4096 directory Device: 803h/2051d Inode: 818813 Links: 2 Access: (2770/drwxrws---) Uid: ( 0/ root) Gid: ( 1045/ops_group) Access: 2020-07-24 09:11:26.981894897 +0800 Modify: 2020-07-24 09:11:17.173894341 +0800 Change: 2020-07-24 09:13:03.894900384 +0800 Birth: - [root@qls ~]# ll /data/ total 0 -rw-rw-r-- 1 ops01 ops01 0 Jul 24 09:10 ops01.txt -rw-rw-r-- 1 ops02 ops02 0 Jul 24 09:11 ops02.txt -rw-rw-r-- 1 ops03 ops03 0 Jul 24 09:11 ops03.txt [root@qls ~]# echo "root" > /data/root.log [root@qls ~]# ll /data/ total 4 -rw-rw-r-- 1 ops01 ops01 0 Jul 24 09:10 ops01.txt -rw-rw-r-- 1 ops02 ops02 0 Jul 24 09:11 ops02.txt -rw-rw-r-- 1 ops03 ops03 0 Jul 24 09:11 ops03.txt -rw-r--r-- 1 root ops_group 5 Jul 24 09:14 root.log [root@qls ~]# su - ops01 Last login: Fri Jul 24 09:10:22 CST 2020 on pts/0 [ops01@qls ~]$ echo "ops01" > /data/ops01.log [ops01@qls ~]$ logout [root@qls ~]# su - ops02 Last login: Fri Jul 24 09:10:52 CST 2020 on pts/0 [ops02@qls ~]$ echo "ops02" > /data/ops02.log [ops02@qls ~]$ ll /data/ total 12 -rw-rw-r-- 1 ops01 ops_group 6 Jul 24 09:15 ops01.log -rw-rw-r-- 1 ops01 ops01 0 Jul 24 09:10 ops01.txt -rw-rw-r-- 1 ops02 ops_group 6 Jul 24 09:15 ops02.log -rw-rw-r-- 1 ops02 ops02 0 Jul 24 09:11 ops02.txt -rw-rw-r-- 1 ops03 ops03 0 Jul 24 09:11 ops03.txt -rw-r--r-- 1 root ops_group 5 Jul 24 09:14 root.log [ops02@qls ~]$ vim /data/ops01.log [ops02@qls ~]$ cat /data/ops01.log ops01 ops02 [ops02@qls ~]$ umask 0002 [ops02@qls ~]$ ll /data/ total 12 -rw-rw-r-- 1 ops01 ops_group 12 Jul 24 09:16 ops01.log -rw-rw-r-- 1 ops01 ops01 0 Jul 24 09:10 ops01.txt -rw-rw-r-- 1 ops02 ops_group 6 Jul 24 09:15 ops02.log -rw-rw-r-- 1 ops02 ops02 0 Jul 24 09:11 ops02.txt -rw-rw-r-- 1 ops03 ops03 0 Jul 24 09:11 ops03.txt -rw-r--r-- 1 root ops_group 5 Jul 24 09:14 root.log [ops02@qls ~]$ rm -f /data/ops01.txt [ops02@qls ~]$ ll -d /data/ drwxrws--- 2 root ops_group 90 Jul 24 09:20 /data/ [ops02@qls ~]$ mkdir /data/test [ops02@qls ~]$ ll /data/test total 0 [ops02@qls ~]$ ll /data/ total 12 -rw-rw-r-- 1 ops01 ops_group 12 Jul 24 09:16 ops01.log -rw-rw-r-- 1 ops02 ops_group 6 Jul 24 09:15 ops02.log -rw-rw-r-- 1 ops02 ops02 0 Jul 24 09:11 ops02.txt -rw-rw-r-- 1 ops03 ops03 0 Jul 24 09:11 ops03.txt -rw-r--r-- 1 root ops_group 5 Jul 24 09:14 root.log drwxrwsr-x 2 ops02 ops_group 6 Jul 24 09:21 test 3. sticky 粘滞位 权限作用于 others 的x为 使用 t 表示 小t 大T 给目录设置, 一个目录所有用户都拥有管理的权限 777 针对此目录设置一个粘滞位 所有用户都可以在这个目录进行创建、删除文件的权利 但是只能管理自己的文件 或者管理员拥有管理所有文件的权限 root [root@qls ~]# ll -d /tmp/ drwxrwxrwt. 15 root root 4096 Jul 24 09:02 /tmp/ [root@qls ~]# stat /tmp/ File: ‘/tmp/’ Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 803h/2051d Inode: 67108936 Links: 15 Access: (1777/drwxrwxrwt) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2020-07-24 09:49:40.026024737 +0800 Modify: 2020-07-24 09:02:34.229864730 +0800 Change: 2020-07-24 09:02:34.229864730 +0800 Birth: - [root@qls ~]# mkdir /test [root@qls ~]# chmod 777 /test [root@qls ~]# ll -d /test drwxrwxrwx 2 root root 6 Jul 24 09:50 /test [root@qls ~]# echo "root" /test/root.txt root /test/root.txt [root@qls ~]# echo "root" > /test/root.txt [root@qls ~]# su - ops01 Last login: Fri Jul 24 09:14:53 CST 2020 on pts/0 [ops01@qls ~]$ echo "ops01" > /test/ops01.txt [ops01@qls ~]$ logout [root@qls ~]# su - dev01 Last login: Wed Jul 22 10:49:56 CST 2020 from 10.0.0.1 on pts/1 [dev01@qls ~]$ echo "dev01" > /test/dev01.txt [dev01@qls ~]$ logout [root@qls ~]# ll /test/ total 12 -rw-rw-r-- 1 dev01 dev01 6 Jul 24 09:52 dev01.txt -rw-rw-r-- 1 ops01 ops01 6 Jul 24 09:51 ops01.txt -rw-r--r-- 1 root root 5 Jul 24 09:51 root.txt [root@qls ~]# su - dev01 Last login: Fri Jul 24 09:51:51 CST 2020 on pts/0 [dev01@qls ~]$ ll /test/ total 12 -rw-rw-r-- 1 dev01 dev01 6 Jul 24 09:52 dev01.txt -rw-rw-r-- 1 ops01 ops01 6 Jul 24 09:51 ops01.txt -rw-r--r-- 1 root root 5 Jul 24 09:51 root.txt [dev01@qls ~]$ rm -f /test/ops01.txt [dev01@qls ~]$ ll /test/ total 8 -rw-rw-r-- 1 dev01 dev01 6 Jul 24 09:52 dev01.txt -rw-r--r-- 1 root root 5 Jul 24 09:51 root.txt [root@qls ~]# chmod o+t /test/ [root@qls ~]# stat /test/ File: ‘/test/’ Size: 39 Blocks: 0 IO Block: 4096 directory Device: 803h/2051d Inode: 67588796 Links: 2 Access: (1777/drwxrwxrwt) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2020-07-24 09:53:31.177037826 +0800 Modify: 2020-07-24 09:53:25.993037532 +0800 Change: 2020-07-24 09:55:02.412042992 +0800 Birth: - [root@qls ~]# su - ops01 Last login: Fri Jul 24 09:51:32 CST 2020 on pts/0 [ops01@qls ~]$ echo "ops01" > /test/ops.txt [ops01@qls ~]$ ll /test/ total 12 -rw-rw-r-- 1 dev01 dev01 6 Jul 24 09:52 dev01.txt -rw-rw-r-- 1 ops01 ops01 6 Jul 24 09:55 ops.txt -rw-r--r-- 1 root root 5 Jul 24 09:51 root.txt [root@qls ~]# su - dev01 Last login: Fri Jul 24 09:52:59 CST 2020 on pts/0 [dev01@qls ~]$ ll /test/ total 12 -rw-rw-r-- 1 dev01 dev01 6 Jul 24 09:52 dev01.txt -rw-rw-r-- 1 ops01 ops01 6 Jul 24 09:55 ops.txt -rw-r--r-- 1 root root 5 Jul 24 09:51 root.txt [dev01@qls ~]$ rm -f /test/ops.txt rm: cannot remove ‘/test/ops.txt’: Operation not permitted [dev01@qls ~]$ rm -f /test/dev01.txt [dev01@qls ~]$ ll /test/ total 8 -rw-rw-r-- 1 ops01 ops01 6 Jul 24 09:55 ops.txt -rw-r--r-- 1 root root 5 Jul 24 09:51 root.txt [dev01@qls ~]$ logout [root@qls ~]# rm -f /test/ops.txt [root@qls ~]# |
特殊属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
特殊属性不受普通权限的限制 lsattr #显示特殊属性 chattr #设置特殊属性 a #这个文件只能追加内容和查看 别的什么都做不了 删除 移动 复制可以 i #只能查看 什么都操作不了 [root@qls ~]# touch test.txt [root@qls ~]# touch test.log [root@qls ~]# ll total 0 -rw-r--r-- 1 root root 0 Jul 24 10:08 test.log -rw-r--r-- 1 root root 0 Jul 24 10:08 test.txt [root@qls ~]# lsattr test.log ---------------- test.log [root@qls ~]# lsattr test.txt ---------------- test.txt [root@qls ~]# echo "test" > test.log [root@qls ~]# echo "test" > test.txt [root@qls ~]# ll total 8 -rw-r--r-- 1 root root 5 Jul 24 10:09 test.log -rw-r--r-- 1 root root 5 Jul 24 10:09 test.txt [root@qls ~]# chattr +a test.log [root@qls ~]# ll test.log -rw-r--r-- 1 root root 5 Jul 24 10:09 test.log [root@qls ~]# lsattr test.log -----a---------- test.log [root@qls ~]# cat test.log test [root@qls ~]# vim test.log [root@qls ~]# vim test.log [root@qls ~]# echo "hello" > test.log -bash: test.log: Operation not permitted [root@qls ~]# echo "hello" >> test.log [root@qls ~]# cat test.log test hello [root@qls ~]# rm -f test.log rm: cannot remove ‘test.log’: Operation not permitted [root@qls ~]# mv test.log /tmp/ mv: cannot move ‘test.log’ to ‘/tmp/test.log’: Operation not permitted [root@qls ~]# cp test.log /tmp/ [root@qls ~]# chattr +i test.txt [root@qls ~]# lsattr test.txt ----i----------- test.txt [root@qls ~]# cat test.txt test [root@qls ~]# rm -f test.txt rm: cannot remove ‘test.txt’: Operation not permitted [root@qls ~]# mv test.txt /tmp/ mv: cannot move ‘test.txt’ to ‘/tmp/test.txt’: Operation not permitted [root@qls ~]# cp test.txt /tmp/ [root@qls ~]# echo "hello" > test.txt -bash: test.txt: Permission denied [root@qls ~]# echo "hello" >> test.txt -bash: test.txt: Permission denied |
重定向的概述
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
将原本要输出到屏幕上面的内容,重定向到一个指定的文件中 将原本从键盘上面输入的内容,改为从命令或者文件当中读取 为什么要用重定向 1. 数据非常重要,需要保存 2. 后台程序的输出重定向到一个文件中 3. 定时任务的执行结果 4. 把一些错误的输出定向到空 5. 把正确和错误的信息都需要保存 stdin #标准输入 0 #从键盘上面读取输入的内容,或者从命令及文件中读取输入的内容 stdout #标准输出 1 #默认将正确的信息标准输出到屏幕上方 stderr #错误输出 2 #默认将错误的信息标准输出到屏幕上方 [root@qls ~]# ll /dev/std* lrwxrwxrwx 1 root root 15 Jul 20 19:44 /dev/stderr -> /proc/self/fd/2 lrwxrwxrwx 1 root root 15 Jul 20 19:44 /dev/stdin -> /proc/self/fd/0 lrwxrwxrwx 1 root root 15 Jul 20 19:44 /dev/stdout -> /proc/self/fd/1 [root@qls ~]# cat hello #标准输入 hello #标准输出 [root@qls ~]# tail -f 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 ^Z [1]+ Stopped tail -f passwd [root@qls ~]# ps PID TTY TIME CMD 10861 pts/0 00:00:00 bash 10879 pts/0 00:00:00 bash 10958 pts/0 00:00:00 su 10980 pts/0 00:00:00 su 10981 pts/0 00:00:00 bash 10998 pts/0 00:00:00 su 11020 pts/0 00:00:00 su 11021 pts/0 00:00:00 bash 11039 pts/0 00:00:00 su 11040 pts/0 00:00:00 bash 14928 pts/0 00:00:00 tail 14930 pts/0 00:00:00 ps [root@qls ~]# ll /proc/14928/fd total 0 lrwx------ 1 root root 64 Jul 24 10:51 0 -> /dev/pts/0 lrwx------ 1 root root 64 Jul 24 10:51 1 -> /dev/pts/0 lrwx------ 1 root root 64 Jul 24 10:51 2 -> /dev/pts/0 lr-x------ 1 root root 64 Jul 24 10:51 3 -> /root/passwd lr-x------ 1 root root 64 Jul 24 10:51 4 -> anon_inode:inotify [root@qls ~]# jobs [1]+ Stopped tail -f passwd [root@qls ~]# fg %1 tail -f passwd ^C [root@qls ~]# jobs [root@qls ~]# ll /proc/14928/fd ls: cannot access /proc/14928/fd: No such file or directory [root@qls ~]# ps PID TTY TIME CMD 10861 pts/0 00:00:00 bash 10879 pts/0 00:00:00 bash 10958 pts/0 00:00:00 su 10980 pts/0 00:00:00 su 10981 pts/0 00:00:00 bash 10998 pts/0 00:00:00 su 11020 pts/0 00:00:00 su 11021 pts/0 00:00:00 bash 11039 pts/0 00:00:00 su 11040 pts/0 00:00:00 bash 14933 pts/0 00:00:00 ps |
输出重定向
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
将原本要输出到屏幕上面的内容,重定向到一个指定的文件中 符号 > #标准覆盖正确输出重定向 #将正确的内容覆盖源文件的 当文件不存在时,会自动创建 >> #标准追加正确输出重定向 #将正确的内容追加到指定文件的底部 当文件不存在时,会自动创建 2> #标准覆盖错误输出重定向 #将错误的信息覆盖到指定的文件中 2>> #标准追加错误输出重定向 #将错误的信息追加到文件的底部 案例: [root@qls ~]# echo "hello" > 123.txt [root@qls ~]# ip a s eth0 > ip.txt [root@qls ~]# cat ip.txt 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:eb:ea:8d 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::3310:9d15:9ee4:43e8/64 scope link noprefixroute valid_lft forever preferred_lft forever #合并文件 [root@qls ~]# cat /etc/hosts /etc/resolv.conf > new.txt [root@qls ~]# cat new.txt 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 # Generated by NetworkManager nameserver 223.5.5.5 [root@qls ~]# cat 123.txt hello [root@qls ~]# echo "test" >> 123.txt [root@qls ~]# cat 123.txt hello test [root@qls ~]# ls /roott ls: cannot access /roott: No such file or directory [root@qls ~]# ls /roott 2>err.txt [root@qls ~]# cat err.txt ls: cannot access /roott: No such file or directory [root@qls ~]# ls /roott 2>>err.txt [root@qls ~]# ls /roott 2>>err.txt [root@qls ~]# ls /roott 2>>err.txt [root@qls ~]# cat err.txt ls: cannot access /roott: No such file or directory ls: cannot access /roott: No such file or directory ls: cannot access /roott: No such file or directory ls: cannot access /roott: No such file or directory #把正确的和错误的信息都保存到一个文件中 [root@qls ~]# ls /roott > file.txt 2>&1 #不推荐 [root@qls ~]# cat file.txt ls: cannot access /roott: No such file or directory [root@qls ~]# mkdir /roott [root@qls ~]# ls /roott > file.txt 2>&1 [root@qls ~]# cat file.txt [root@qls ~]# ls /roott &>file.txt [root@qls ~]# ls /roottt &>file.txt [root@qls ~]# cat file.txt ls: cannot access /roottt: No such file or directory #将正确的信息和错误的信息重定向到空 [root@qls ~]# ls /roottt &> /dev/null #把正确的和错误的放在不同的文件中 [root@qls ~]# ls /rooot >> file1.txt 2>> file2.txt [root@qls ~]# ll total 4 -rw-r--r-- 1 root root 0 Jul 24 11:14 file1.txt -rw-r--r-- 1 root root 52 Jul 24 11:14 file2.txt [root@qls ~]# cat file1.txt [root@qls ~]# cat file2.txt ls: cannot access /rooot: No such file or directory [root@qls ~]# ls /root >> file1.txt 2>> file2.txt [root@qls ~]# ll total 8 -rw-r--r-- 1 root root 20 Jul 24 11:15 file1.txt -rw-r--r-- 1 root root 52 Jul 24 11:14 file2.txt [root@qls ~]# cat file1.txt file1.txt file2.txt [root@qls ~]# cat file2.txt ls: cannot access /rooot: No such file or directory |
输入重定向
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
将原本从键盘上面输入的内容,改为从命令或者文件当中读取 符号 < #标准输入重定向 #将原本从默认的键盘中读取数据改为由命令或者文件中读取 << #标识符限定输入重定向 #从键盘中读取内容,直到遇到标识符的分解符为止 案例: [root@qls ~]# grep 'root' /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@qls ~]# grep 'root' < /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@qls ~]# grep root oldboy root root rottttroot rottttroot [root@qls ~]# dd if=/dev/zero of=/root/test.log bs=1M count=50 50+0 records in 50+0 records out 52428800 bytes (52 MB) copied, 0.0362247 s, 1.4 GB/s [root@qls ~]# ll total 51208 -rw-r--r-- 1 root root 20 Jul 24 11:15 file1.txt -rw-r--r-- 1 root root 52 Jul 24 11:14 file2.txt -rw-r--r-- 1 root root 52428800 Jul 24 11:45 test.log [root@qls ~]# ll -h total 51M -rw-r--r-- 1 root root 20 Jul 24 11:15 file1.txt -rw-r--r-- 1 root root 52 Jul 24 11:14 file2.txt -rw-r--r-- 1 root root 50M Jul 24 11:45 test.log [root@qls ~]# dd </dev/zero >/root/oldboy.log bs=10M count=100 100+0 records in 100+0 records out 1048576000 bytes (1.0 GB) copied, 21.3836 s, 49.0 MB/s [root@qls ~]# ll -h total 1.1G -rw-r--r-- 1 root root 20 Jul 24 11:15 file1.txt -rw-r--r-- 1 root root 52 Jul 24 11:14 file2.txt -rw-r--r-- 1 root root 1000M Jul 24 11:48 oldboy.log -rw-r--r-- 1 root root 50M Jul 24 11:45 test.log [root@qls ~]# cat file1.txt file1.txt file2.txt [root@qls ~]# tr 't' 'T' < file1.txt file1.TxT file2.TxT 数据库导入表 [root@qls ~]# mysql -uroot -p123 < all.sql [root@qls ~]# cat >>file3.txt 123 345 678 456 EOF 123 ^C [root@qls ~]# cat file3.txt 123 345 678 456 EOF 123 [root@qls ~]# cat >>file4.txt<<EOF > 123 > dfer > rghrt > gergrtg > EOF [root@qls ~]# cat file4.txt 123 dfer rghrt gergrtg [root@qls ~]# cat 123.sh cat<<EOF 1. rge 2. fgo 3. jfrio 4. hfoih EOF [root@qls ~]# sh 123.sh 1. rge 2. fgo 3. jfrio 4. hfoih |
管道技术
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
| 管道 连接左右两个命令的使用 把前面的命令作为标准输出通过管道交给后面的命令 作为标准输入 只能把正确的信息交给后面的命令,错误的信息的不会传递 [root@qls ~]# head 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 [root@qls ~]# head passwd | cat -n 1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologin 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin 4 adm:x:3:4:adm:/var/adm:/sbin/nologin 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6 sync:x:5:0:sync:/sbin:/bin/sync 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8 halt:x:7:0:halt:/sbin:/sbin/halt 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10 operator:x:11:0:operator:/root:/sbin/nologin [root@qls ~]# ifconfig eth0 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::3310:9d15:9ee4:43e8 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:eb:ea:8d txqueuelen 1000 (Ethernet) RX packets 99054 bytes 61039138 (58.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 55989 bytes 5452694 (5.2 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@qls ~]# ifconfig eth0 | awk 'NR==2' inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255 [root@qls ~]# ifconfig eth0 | awk 'NR==2' | awk '{print $2}' 10.0.0.100 [root@qls ~]# awk -F: '{print $3}' passwd | sort -rn | head 1046 1045 1044 1043 1042 1041 1040 1039 1038 1037 #管道技术中的tee技术 需要保存一些标准输出的内容 [root@qls ~]# echo $RANDOM | md5sum | cut -c 1-8 | tee pass.txt |passwd --stdin qls01 Changing password for user qls01. passwd: all authentication tokens updated successfully. [root@qls ~]# cat pass.txt 100ada15 -a #追加 tee 和 重定向的区别 重定向 #把要输出到屏幕上面的内容重定向到指定的文件中 tee #把要输出到屏幕上面的内容重定向到指定的文件中 ,并且可以继续输出一份到屏幕上面 #管道中xargs技术 让一些不支持管道的命令支持管道 把前面命令的执行结果以文件的参数传递方式传递给后面的命令 [root@qls ~]# ls | xargs sed -i 's#root#oldboy#g' [root@qls ~]# find /var/log/ -type f -name "*.log" |xargs cp -t /opt [root@qls ~]# find /var/log/ -type f -name "*.log" |xargs -I {} cp {} /mnt/ |