一、用户用户组
- 和用户及用户组相关的命令有哪些?
[root@JD ~]# ll /usr/sbin/user*
-rwxr-x--- 1 root root 118192 Nov 6 2016 /usr/sbin/useradd
-rwxr-x--- 1 root root 80360 Nov 6 2016 /usr/sbin/userdel
-rwxr-x--- 1 root root 113840 Nov 6 2016 /usr/sbin/usermod
-rwsr-xr-x 1 root root 11296 Apr 13 2017 /usr/sbin/usernetctl
[root@JD ~]# ll /usr/sbin/group*
-rwxr-x--- 1 root root 65480 Nov 6 2016 /usr/sbin/groupadd
-rwxr-x--- 1 root root 57016 Nov 6 2016 /usr/sbin/groupdel
-rwxr-x--- 1 root root 57064 Nov 6 2016 /usr/sbin/groupmems
-rwxr-x--- 1 root root 76424 Nov 6 2016 /usr/sbin/groupmod
[root@JD ~]#
- 添加用户
[root@JD ~]# useradd ruoze
[root@JD ~]# id ruoze
uid=1002(ruoze) gid=1002(ruoze) groups=1002(ruoze)
[root@JD ~]#
[root@JD ~]# cat /etc/passwd | grep ruoze
ruoze:x:1002:1002::/home/ruoze:/bin/bash
[root@JD ~]#
[root@JD ~]# cat /etc/group | grep ruoze
ruoze:x:1002:
[root@JD ~]#
useradd ruoze
:创建一个普通用户 ruoze,默认创建这个名称的用户组ruoze, 设置这个用户 主组为ruoze,且创建家目录为 /home/ruoze,从/etc/skel/ 拷贝三个文件到家目录。相应配置更新到 /etc/passwd 和 /etc/group
- 删除用户后重新建
[root@JD ~]# userdel ruoze
[root@JD ~]# id ruoze
id: ruoze: no such user
[root@JD ~]# useradd ruoze
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
Creating mailbox file: File exists
[root@JD ~]# cat /etc/group | grep ruoze
ruoze:x:1002:
[root@JD ~]# cat /etc/passwd | grep ruoze
ruoze:x:1002:1002::/home/ruoze:/bin/bash
[root@JD ~]#
userdel ruoze后,/etc/passwd 和 /etc/group文件中内容会删除,但是家目录还在。此时重新创建 ruoze用户,提示useradd: warning: the home directory already exists.Not copying any file from skel directory into it.Creating mailbox file: File exists 也就是说,创建家目录和 从/etc/skel 拷贝文件的动作被省略了
- 模拟切换用户丢失样式
[ruoze@JD ~]$ pwd
/home/ruoze
[ruoze@JD ~]$ ls -la
total 12
drwx------ 2 ruoze ruoze 59 Nov 17 10:31 .
drwxr-xr-x. 5 root root 57 Nov 17 11:33 ..
-rw-r--r-- 1 ruoze ruoze 18 Apr 11 2018 .bash_logout
-rw-r--r-- 1 ruoze ruoze 193 Apr 11 2018 .bash_profile
-rw-r--r-- 1 ruoze ruoze 231 Apr 11 2018 .bashrc
[ruoze@JD ~]$ rm -f .bash*
[ruoze@JD ~]$ exit
logout
[root@JD ~]# su - ruoze
Last login: Sun Nov 17 16:30:03 CST 2019 on pts/4
Last failed login: Sun Nov 17 16:33:46 CST 2019 on pts/4
There was 1 failed login attempt since the last successful login.
-bash-4.2$
-bash-4.2$
其实就是从/etc/skel 拷贝的文件丢失导致,重新拷贝登录即可
-bash-4.2$ ls -la /etc/skel/
total 24
drwxr-xr-x. 2 root root 59 Aug 8 2018 .
drwxr-xr-x. 86 root root 8192 Nov 17 16:29 ..
-rw-r--r-- 1 root root 18 Apr 11 2018 .bash_logout
-rw-r--r-- 1 root root 193 Apr 11 2018 .bash_profile
-rw-r--r-- 1 root root 231 Apr 11 2018 .bashrc
-bash-4.2$ cp /etc/skel/.bash* ~/
-bash-4.2$ ls -la
total 16
drwx------ 2 ruoze ruoze 79 Nov 17 16:35 .
drwxr-xr-x. 5 root root 57 Nov 17 11:33 ..
-rw------- 1 ruoze ruoze 75 Nov 17 16:34 .bash_history
-rw-r--r-- 1 ruoze ruoze 18 Nov 17 16:35 .bash_logout
-rw-r--r-- 1 ruoze ruoze 193 Nov 17 16:35 .bash_profile
-rw-r--r-- 1 ruoze ruoze 231 Nov 17 16:35 .bashrc
-bash-4.2$ exit
logout
[root@JD ~]# su - ruoze
Last login: Sun Nov 17 16:34:28 CST 2019 on pts/4
[ruoze@JD ~]$
[ruoze@JD ~]$
注意,安装软件,拷贝文件等要注意它的所属用户和所属组
- 用户组
一个用户可以有多个组,但是只有一个主组
创建一个bigdata组,将ruoze添加到bigdata组,然后更改bigdata为ruoze用户的主组
[root@JD ~]# groupadd bigdata # 创建bigdata组
[root@JD ~]# id ruoze
uid=1002(ruoze) gid=1002(ruoze) groups=1002(ruoze)
[root@JD ~]# usermod -a -G bigdata ruoze # 添加ruoze到bigdata组中,但不是主组
[root@JD ~]# id ruoze
uid=1002(ruoze) gid=1002(ruoze) groups=1002(ruoze),1003(bigdata)
[root@JD ~]# usermod -g bigdata ruoze # 更改ruoze的主组为bigdata,此时原来的主组丢失
[root@JD ~]# id ruoze
uid=1002(ruoze) gid=1003(bigdata) groups=1003(bigdata)
[root@JD ~]# groupdel bigdata #删除组,显示无法删除,因为有用户绑定为主组
groupdel: cannot remove the primary group of user 'ruoze'
[root@JD ~]# id ruoze
uid=1002(ruoze) gid=1003(bigdata) groups=1003(bigdata)
[root@JD ~]# usermod -g ruoze ruoze # 重新指定主组,此时 bigdata未丢失
[root@JD ~]# id ruoze
uid=1002(ruoze) gid=1002(ruoze) groups=1002(ruoze),1003(bigdata)
[root@JD ~]# groupdel bigdata # 可删除
[root@JD ~]# id ruoze
uid=1002(ruoze) gid=1002(ruoze) groups=1002(ruoze)
[root@JD ~]#
修改一个组,建议显性地指定他的所有组
二、sudo命令
- 设置用户密码
[root@JD ~]# passwd ruoze
Changing password for user ruoze.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@JD ~]#
- 切换用户
命令 | 是否切换用户 | 目录切换情况 | 环境变量 .bash_profile | .bashrc |
---|---|---|---|---|
su ruoze | 切换用户 | 不切换到对应的家目录 | 不执行 | 执行 |
su - ruoze | 不仅切换用户 | 切换该用户的家目录 | 执行 | 执行 |
所以,个人环境变量文件优先使用.bashrc
3. 普通用户获取root的最大权限
vi /etc/sudoers
ruoze ALL=(root) NOPASSWD:ALL # 所有命令都不输入密码
ruoze ALL=(root) ALL # 所有命令都输入密码
三、用户无法登录 passwd文件
[root@JD ~]# cat /etc/passwd | grep ruoze
ruoze:x:1002:1002::/home/ruoze:/bin/bash
更改/bin/bash为/bin/false 后切换ruoze用户,切换用户失败
[root@JD ~]# cat /etc/passwd | grep ruoze
ruoze:x:1002:1002::/home/ruoze:/bin/false
[root@JD ~]# su - ruoze
Last login: Sun Nov 17 17:22:41 CST 2019 on pts/4
[root@JD ~]#
更改/bin/bash为/bin/nologin 后切换ruoze用户,切换用户失败
[root@JD ~]# cat /etc/passwd | grep ruoze
ruoze:x:1002:1002::/home/ruoze:/bin/nologin
[root@JD ~]# su - ruoze
Last login: Sun Nov 17 17:26:17 CST 2019 on pts/4
su: failed to execute /bin/nologin: No such file or directory
[root@JD ~]#
CDH 很多的组件使用的用户登录时无法登陆,其实就是/etc/passwd下对应的配置为上面两种,改成通用可登陆的就ok
hdfs组件 hdfs用户:su - hdfs; 对应修改为 /bin/bash
hbase组件 hbase用户:su - hbase; yarn组件 yarn用户
四、权限 rwx------ chmod chown 案例
- 文件/文件夹权限
[root@JD ~]# ll
total 12
-rwxrwxrwx 1 root root 8388 Oct 25 12:22 temp.txt
-rw-r--r-- 1 root root 0 Nov 17 17:23 test.sh
drwxr-xr-x 2 root root 6 Nov 17 17:29 tmp
第一个字符,文件属性: d文件夹 -文件 l连接,
后面三个字符为一组,分别代表用户、组、其他人的对这个文件的权限,
再之后数字是连接数,表示有多少文件名连接到此节点,
再之后是所属用户和所属组,
再之后是文件大小,单位bytes,
然后是文件最后被修改的时间,
最后是文件名
r: read 4
w: write 2
x: 执行 1
-: 没权限 0
一般来说,带参数R的,我们基本就用到这两个
chmod -R 777 文件夹/文件路径
chown -R 用户:用户组 文件夹/文件路径
五、 其他命令
- vi 异常关闭时产生的swp文件
[ruoze@JD ~]$ ls -la
total 28
drwx------ 2 ruoze ruoze 129 Nov 17 17:42 .
drwxr-xr-x. 5 root root 57 Nov 17 11:33 ..
-rw------- 1 ruoze ruoze 192 Nov 17 17:42 .bash_history
-rw-r--r-- 1 ruoze ruoze 18 Nov 17 16:35 .bash_logout
-rw-r--r-- 1 ruoze ruoze 193 Nov 17 16:35 .bash_profile
-rw-r--r-- 1 ruoze ruoze 231 Nov 17 16:35 .bashrc
-rw-rw-r-- 1 ruoze ruoze 4 Nov 17 17:42 test.txt
-rw-r--r-- 1 ruoze ruoze 4096 Nov 17 17:42 .test.txt.swp
-rw------- 1 ruoze ruoze 596 Nov 17 17:42 .viminfo
[ruoze@JD ~]$
存在这个文件,编辑test.txt时会有以下类型提示:
E325: ATTENTION
Found a swap file by the name ".test.txt.swp"
owned by: ruoze dated: Sun Nov 17 17:44:14 2019
file name: ~ruoze/test.txt
modified: no
user name: ruoze host name: JD
process ID: 27023 (still running)
While opening file "test.txt"
dated: Sun Nov 17 17:42:27 2019
(1) Another program may be editing the same file. If this is the case,
be careful not to end up with two different instances of the same
file when making changes. Quit, or continue with caution.
(2) An edit session for this file crashed.
If this is the case, use ":recover" or "vim -r test.txt"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file ".test.txt.swp"
to avoid this message.
Swap file ".test.txt.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort:
- du
[ruoze@JD ~]$ du -sh .bash*
4.0K .bash_history
4.0K .bash_logout
4.0K .bash_profile
4.0K .bashrc
[ruoze@JD ~]$
不足4KB按4KB显示,和系统默认快大小有关
- find
find /usr/local -name '*hadoop*'
指定目录下,按文件名查找特定文件