find搜索命令
其他搜索命令如下:
[[email protected] 333]# which ls
alias ls=‘ls --color=auto‘
/bin/ls
[[email protected] 333]# echo $PATH
/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin:/usr/local/jdk1.6.0_23/bin:/usr/local/jdk1.6.0_23/jre/bin:/root/bin
[[email protected] 333]# touch wangchao
[[email protected] 333]# mv wangchao /tmp/
[[email protected] 333]# which wangchao
/usr/bin/which: no wangchao in (/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin:/usr/local/jdk1.6.0_23/bin:/usr/local/jdk1.6.0_23/jre/bin:/root/bin)
//找不到该文件,因为which不能收不可执行文件
[[email protected] 333]# whereis pwd //使用whereis查找pwd文件
pwd: /bin/pwd /usr/include/pwd.h /usr/share/man/man1/pwd.1.gz /usr/share/man/man1p/pwd.1p.gz
[[email protected] 333]# whereis wangchao
//查找该文件失败,因为whereis也只能在一些特定目录下查找
[[email protected] 333]# locate ls
[[email protected] 333]# updatedb 生成库文件,更新
[[email protected] 333]# locate wangchao
//找到相关文件,但是未找到/tmp/wangchao的文件
[[email protected] 333]# ls /tmp/wangchao
/tmp/wangchao
[[email protected] 333]# touch 123.1
[[email protected] 333]# locate 1234.1 新建的文件,不更新库,也找不到
使用find查找
[[email protected] 333]# find /tmp/ -name wangchao
//在tmp下查找文件,-name指定文件名
/tmp/wangchao
[[email protected] 333]# find /tmp/ -name "wangchao"
/tmp/wangchao
[[email protected] 333]# find /tmp/ -name ‘wangchao‘
/tmp/wangchao
[[email protected] 333]# find /tmp/ -type f //查找文件,-type指定文件类型,f为文件
[[email protected] 333]# find /tmp/ -type s
[[email protected] 333]# find /tmp/ -type b
[[email protected] 333]# find /tmp/ -type c
常用:
[[email protected] 333]# find /tmp/ -mtime +10
//找天的出创建时间,更改时间大于10天的文件
[[email protected] 333]# find /tmp/ -mmin -5
//找出5分钟内有变动的文件
[[email protected] 333]# find /tmp/ -type f -name wangchao
/tmp/wangchao
//在tmp下查找文件、文件名为wangchao的文件
[[email protected] 333]# find /var/log/ -type f -mtime +30
//查找30天前的日志
[[email protected] 333]# find /tmp/ -type f |xargs ls -l
//找到tmp下的文件并执行ls -l 命令
三个时间属性
mtime、atime、ctime
使用stat列出文件的mtime、atime、ctime
[[email protected] 333]# stat 33.txt
File: `33.txt‘
Size: 15 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 786406 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2015-07-03 21:09:37.209000025 +0800
Modify: 2015-07-03 21:09:37.209000025 +0800
Change: 2015-07-03 21:09:37.209000025 +0800
atime(Access time)是在读取文件或者执行文件时更改的(访问时间)
mtime(Modified time)是在写入文件时随文件内容的更改而更改的(创建更改时间)
ctime(Create time)是在写入文件、更改所有者、权限或链接设置时随inode的内容更改而更改的(更改inode,文件名,权限,主,组等的时间)
[[email protected] 333]# date
Fri Jul 3 21:57:34 CST 2015
[[email protected] 333]# echo "1111">33.txt
[[email protected] 333]# stat 33.txt
File: `33.txt‘
Size: 5 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 786406 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2015-07-03 21:09:37.209000025 +0800
Modify: 2015-07-03 21:58:04.060998612 +0800
Change: 2015-07-03 21:58:04.060998612 +0800
//文件mtime、ctime发生了变化(大小发生变化,inode发生了变化)
[[email protected] 333]# touch 33.txt
[[email protected] 333]# stat 33.txt
File: `33.txt‘
Size: 5 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 786406 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2015-07-03 22:00:38.642998994 +0800
Modify: 2015-07-03 22:00:38.642998994 +0800
Change: 2015-07-03 22:00:38.642998994 +0800
//touch无该文件就创建,有就触碰以下该文件
使用touch可更改mtime、atime、ctime
[[email protected] 333]# chmod 777 33.txt
[[email protected] 333]# stat 33.txt
File: `33.txt‘
Size: 5 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 786406 Links: 1
Access: (0777/-rwxrwxrwx) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2015-07-03 22:00:38.642998994 +0800
Modify: 2015-07-03 22:00:38.642998994 +0800
Change: 2015-07-03 22:02:32.206994821 +0800
//更改mtime后,ctime一定改变,更改ctime后,mtime不一定更改
硬链接和软连接
[[email protected] 333]# find /etc/ -type l //搜索目录下软连接文件
[[email protected] 333]# find /etc/ -type l |xargs ls -l //搜索目录下软连接文件并ls -l下
[[email protected] 333]# ls -l /bin/
[[email protected] 333]# ls -l /lib/
lrwxrwxrwx. 1 root root 16 Jun 8 19:15 libuuid.so.1 -> libuuid.so.1.3.0 //文件名复杂的创建软连接后可简化
[[email protected] 333]# du -sh /lib/libuuid.so.1 /lib/libuuid.so.1.3.0 //看两文件大小
0 /lib/libuuid.so.1
20K /lib/libuuid.so.1.3.0
[[email protected] 333]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 5.7G 11G 35% /
tmpfs 504M 72K 504M 1% /dev/shm
/dev/sda1 190M 26M 155M 15% /boot
假设/boot/下有个服务,但是/boot/下空间却不够了,此时可将/boot/下的文件移到/(空间大的)下,然后在/boot/下创建软连接。
[[email protected] 333]# ln -s /root/12.txt /tmp/23.txt
在做软连接/tmp/23.txt,指向到/root/12.txt
[[email protected] 333]# ln -s /root/111 /tmp/123
//目录做软连接
硬链接
[[email protected] 333]# ln /root/12.txt /tmp/45.txt
[[email protected] 333]# ls -i /root/12.txt /tmp/45.txt
920490 /root/12.txt 920490 /tmp/45.txt //两文件的inode相同
[[email protected] 333]# du -sh /root/12.txt
4.0K /root/12.txt
[[email protected] 333]# du -sh /tmp/45.txt
4.0K /tmp/45.txt
[[email protected] 333]# ls -ihl /root/12.txt /tmp/45.txt
920490 -rw-r--r--. 2 root root 4 Jul 3 22:26 /root/12.txt
920490 -rw-r--r--. 2 root root 4 Jul 3 22:26 /tmp/45.txt
[[email protected] 333]# rm -f /root/1.txt //删除一份文件后另一份还存在
[[email protected] 333]# cat /tmp/45.txt
123
硬链接只能在同一分区下做,不能跨分区
[[email protected] 333]# ln /tmp/45.txt /boot/1.txt
ln: creating hard link `/boot/1.txt‘ => `/tmp/45.txt‘: Invalid cross-device link
每个分区下都有自己独立的inode信息,每个分区下都有inode唯一的文件
[[email protected] 333]# ln /root/111/ /tmp/222/
ln: `/root/111/‘: hard link not allowed for directory
//硬链接不能作用于目录
用户名文件
[[email protected] 333]# ls /etc/passwd
/etc/passwd
[[email protected] 333]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
该文件由7端组成
用户名:密码:用户标识号uid:组标识号gid: 注释说明: 用户的家目录: shell
密码显示为x,存在其他文件中,防止每个人都能查看
shell中将/bin/bash改为/sbin/nologin用户将不能登入
[[email protected] 333]# id wangchao //查看用户id
uid=505(wangchao) gid=505(wangchao) groups=505(wangchao)
[[email protected] 333]# id root
uid=0(root) gid=0(root) groups=0(root)
密码文件
[[email protected] 333]# ls /etc/shadow
/etc/shadow
[[email protected] 333]# cat /etc/shadow
root:$6$GOGIZcxzc/oBMX9F$71OVjhnc4JKbNjzsU5NJ1aJoSMFRo23X2vTPcZxlmoAPaJOXnfhjWdI9o5A2oDs19.nLGyj290l2KgjS8fFQo1:16594:0:99999:7:::
bin:*:15980:0:99999:7:::
该文件被:分割成9端
用户名:用户密码(经加密的):上次更改密码的日期:密码多少天后到期:密码到期前的警告期限:账号失效期限:账号的生命周期:保留
上次更改密码的日期,值为距离1970年1月1日到上次更改密码的日期。
增加和删除用户组
[[email protected] 333]# groupadd grp1
[[email protected] 333]# tail /etc/group
tom:x:500:
mysql:x:501:
php-fpm:x:502:
named:x:25:
virftp:x:503:
smbuser1:x:504:
squid:x:23:
wangchao:x:505:
user1:x:506:
grp1:x:507:
[[email protected] 333]# tail /etc/gshadow
tom:!!::
mysql:!::
php-fpm:!::
named:!::
virftp:!::
smbuser1:!::
squid:!::
wangchao:!::
user1:!::
grp1:!::
[[email protected] 333]# groupadd -g 512 grp2 //指定组id创建组
[[email protected] 333]# groupadd grp3
[[email protected] 333]# tail /etc/gr
group group- grub.conf
[[email protected] 333]# tail /etc/group //再次新建组后组id从512后开始了
php-fpm:x:502:
named:x:25:
virftp:x:503:
smbuser1:x:504:
squid:x:23:
wangchao:x:505:
user1:x:506:
grp1:x:507:
grp2:x:512:
grp3:x:513:
[[email protected] 333]# groupdel grp3 //删除组
增加和删除用户
[[email protected] 333]# useradd wangchao
[[email protected] 333]# tail /etc/passwd
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
tom:x:500:500::/home/tom:/bin/bash
mysql:x:501:501::/home/mysql:/sbin/nologin
php-fpm:x:502:502::/home/php-fpm:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
virftp:x:503:503::/home/virftp:/sbin/nologin
smbuser1:x:504:504::/home/smbuser1:/bin/bash
squid:x:23:23::/var/spool/squid:/sbin/nologin
wangchao:x:505:505::/home/wangchao:/bin/bash
[[email protected] 333]# useradd -u 508 -g 512 user //创建用户uid=508,gid=512
[[email protected] 333]# tail -2 /etc/passwd
wangchao:x:505:505::/home/wangchao:/bin/bash
user:x:508:512::/home/user:/bin/bash
[[email protected] 333]# id user
uid=508(user) gid=512(grp2) groups=512(grp2)
[[email protected] 333]# groupdel grp2 //删除组失败,因为组内有成员
groupdel: cannot remove the primary group of user ‘user‘
[[email protected] 333]# useradd -d /home/user3 -u509 -g500 -s /sbin/nologin user2
//-d 指定家目录,-s指定shell
[[email protected] 333]# tail -2 /etc/passwd
user:x:508:512::/home/user:/bin/bash
user2:x:509:500::/home/user3:/sbin/nologin
[[email protected] 333]# useradd -M -s /sbin/nologin user3
//-M不创建家目录,只创建用户
[[email protected] 333]# id user3
uid=510(user3) gid=510(user3) groups=510(user3)
[[email protected] 333]# userdel user2
[[email protected] 333]# ls /home/
php-fpm/ smbuser1/ tom/ user/ user3/ virftp/ wangchao/
[[email protected] 333]# ls /home/user3
//删除用户后,家目录未删除
[[email protected] 333]# ls -ld /home/user3/
drwx------. 4 509 tom 4096 Jul 4 00:29 /home/user3/
//用户不存在后,主显示为uid
[[email protected] 333]# userdel -r user
[[email protected] 333]# ls /home/
php-fpm smbuser1 tom user3 virftp wangchao
//加-r参数删除用户后,家目录也删除
usermod修改用户属性
[[email protected] 333]# tail /etc/passwd
tcpdump:x:72:72::/:/sbin/nologin
tom:x:500:500::/home/tom:/bin/bash
mysql:x:501:501::/home/mysql:/sbin/nologin
php-fpm:x:502:502::/home/php-fpm:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
virftp:x:503:503::/home/virftp:/sbin/nologin
smbuser1:x:504:504::/home/smbuser1:/bin/bash
squid:x:23:23::/var/spool/squid:/sbin/nologin
wangchao:x:505:505::/home/wangchao:/bin/bash
user3:x:510:510::/home/user3:/sbin/nologin
[[email protected] 333]# tail /etc/group
php-fpm:x:502:
named:x:25:
virftp:x:503:
smbuser1:x:504:
squid:x:23:
wangchao:x:505:
user1:x:506:
grp1:x:507:
grp2:x:512:
user3:x:510:
[[email protected] 333]# id wangchao
uid=505(wangchao) gid=505(wangchao) groups=505(wangchao)
[[email protected] 333]# usermod -g 512 wangchao
[[email protected] 333]# id wangchao
uid=505(wangchao) gid=512(grp2) groups=512(grp2)
//组改为512
[[email protected] 333]# useradd -u 506 -g 502 -G 512 user3
useradd: user ‘user3‘ already exists
[[email protected] 333]# rm -rf /home/user3/ /var/spool/mail/
[[email protected] 333]# useradd -u 506 -g 502 -G 512 user3
[[email protected] 333]# id user3
uid=506(user3) gid=502(php-fpm) groups=502(php-fpm),512(grp2)
//-g主组为502,-G附加组为512
[[email protected] 333]# groupdel grp2 //删除组,组内有成员删除失败
groupdel: cannot remove the primary group of user ‘wangchao‘
[[email protected] 333]# usermod -L wangchao //锁定账号不能使用
[[email protected] 333]# usermod -U wangchao //解锁账号
[[email protected] 333]# tail /etc/shadow //查看账号锁定信息文件
passwd修改用户密码
[[email protected] ~]# passwd //修改root的密码
Changing password for user root.
New password:
[[email protected] ~]# passwd wangchao //修改指定用户密码
Changing password for user wangchao.
New password:
[[email protected] ~]# mkpasswd
-bash: mkpasswd: command not found
[[email protected] ~]# yum install -y expect
[[email protected] ~]# mkpasswd //生成一个长度为9的不规则字符
sp$Ntl24P
[[email protected] ~]# mkpasswd -l 12 -s 0 -d 4 -C 5
r79Y4LijP2IH
//-l 指定长度12,-s特殊字符0,-d数字4个,-C大写字母5个。-c指定小写字母
[[email protected] ~]# passwd --stdin wangchao //修改密码明文显示出(多用于脚本)
Changing password for user wangchao.
123456
passwd: all authentication tokens updated successfully.
[[email protected] ~]# echo "1111"|passwd --stdin wangchao
Changing password for user wangchao.
passwd: all authentication tokens updated successfully.
切换用户su
[[email protected] ~]# id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[[email protected] ~]# whoami
root
//查看当前用户
[[email protected] ~]# su wangchao
[[email protected] root]$ echo $PATH
/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin:/usr/local/jdk1.6.0_23/bin:/usr/local/jdk1.6.0_23/jre/bin:/root/bin:/usr/local/mysql/bin
[[email protected] root]$ pwd //切换后还在root目录下
/root
现想切换目录后到自己家目录下
[[email protected] ~]$ logout
[[email protected] ~]# su - wangchao //切换用户
[[email protected] ~]$ echo $PATH
/usr/lib/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/mysql/bin:/usr/local/jdk1.6.0_23/bin:/usr/local/jdk1.6.0_23/jre/bin:/home/wangchao/bin
[[email protected] ~]$ pwd
/home/wangchao
[[email protected] ~]$ whoami
wangchao
[[email protected] ~]$ su - //切换回root
Password:
[[email protected] ~]# id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[[email protected] ~]# su --c "id" wangchao //root下已wangchao身份运行id
uid=505(wangchao) gid=512(grp2) groups=512(grp2) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[[email protected] ~]# su --c "ls -l /tmp/123" wangchao
lrwxrwxrwx. 1 root root 9 Jul 3 22:20 /tmp/123 -> /root/111
//root下已wangchao身份运行ls -l /tmp/123
sudo详解
[[email protected] ~]# su - wangchao
[[email protected] ~]$ ls /root/
ls: cannot open directory /root/: Permission denied
[[email protected] ~]$ logout
[[email protected] ~]# visudo
root ALL=(ALL) ALL
wangchao ALL=(root) /bin/ls,/usr/bin/passwd
// root ALL=(ALL) ALL ,含义:
root: root用户,
ALL=(ALL):ALL从哪里登入,允许所有来源IP。=(ALL):所有权限
ALL:所有命令都可以使用
wangchao ALL=(root) /bin/ls,/usr/bin/passwd中ALL=(root),拥有root用户的命令权限,/bin/ls,/usr/bin/passwd,该用户拥有root权限运行的命令为ls、passwd.