您的位置:首页 > 运维架构 > Shell

linux基础-权限管理,手工添加用户,umask,bash配置文件

2017-02-15 23:06 811 查看
权限管理:
r:
w:
x:
三类用户:
u: 属主
g: 属组
o: 其它用户

chown: 改变文件属主,不会修改内部子文件(只有管理员可以使用此命令)
# chown USERNAME file,...
-R: 修改目录及其内部文件的属主
--reference=/path/to/somefile file,... (修改文件属主与某文件保持一直)
chown --refernce=/tmp/abc /tmp/test
ll /tmp/test
ll /tmp/abc

# chgrp GRPNAME file,... 改变文件属组(只有管理员可以使用此命令)
-R
--reference=/path/to/somefile file,...

可以同时修改属主和属组
chown USERNAME:GRPNAME file,...
chown USERNAME.GRPNAME file,...

chmod: 修改文件的权限
修改三类用户的权限:
chmod MODE file,...
chmod 750 /etc/test (
ll /etc/test

-R:修改目录及其内部文件的属主
--reference=/path/to/somefile file,... (修改文件属主与某文件保持一直)
rwxr-x---

修改某类用户或某些类用户权限:
u,g,o,a
chmod u=rwx /tmp/abc
ll /tmp/abc
chmod g=r,u=r /tmp/abc
chmod go=r /tmp/abc

chmod 用户类别=MODE file,...
修改某类用户的某位或某些位权限:
u,g,o,a
chmod 用户类别+|-MODE file,...
chmod u-x /tmp/abc
ll /tmp/abc
chmod a+x /tmp/abc
chmod +x /tmp/abc

练习:
1、新建一个没有家目录的用户openstack;
# useradd -M openstack
2、复制/etc/skel为/home/openstack;
# cp -r /etc/skel /home/openstack
# ll /home/openstack
3、改变/home/openstack及其内部文件的属主属组均为openstack;
# chown -R openstack:openstack /home/openstack
4、/home/openstack及其内部的文件,属组和其它用户没有任何访问权限
# chmod -R go= /home/openstack

手动添加用户,模拟useradd功能(练习)
手动添加用户hive, 基本组为hive (5000),附加组为mygroup
ll `which passwd`

nano /etc/group
mygroup:x:501:hive
hive:x:5000:

nano /etc/passwd
hive:x:5000:5000:Hive:/home/hive:/bin/bash

nano /etc/shadow
hive:!!:17125:0:99999:7:::

修改家目录用户/组/权限

root@localhost ~]# cp -r /etc/skel/ /home/hive
[root@localhost ~]# ll
total 64
-rw-------. 1 root root 4080 Jun 21 2015 anaconda-ks.cfg
-rw-r--r--. 1 root root 42001 Jun 21 2015 install.log
-rw-r--r--. 1 root root 9154 Jun 21 2015 install.log.syslog
[root@localhost ~]# ll /home/hive/
total 0
[root@localhost ~]# ll /home/hive/ -a
total 28
drwxr-xr-x. 4 root root 4096 Nov 19 19:59 .
drwxr-xr-x. 4 root root 4096 Nov 19 19:59 ..
-rw-r--r--. 1 root root 18 Nov 19 19:59 .bash_logout
-rw-r--r--. 1 root root 176 Nov 19 19:59 .bash_profile
-rw-r--r--. 1 root root 124 Nov 19 19:59 .bashrc
drwxr-xr-x. 2 root root 4096 Nov 19 19:59 .gnome2
drwxr-xr-x. 4 root root 4096 Nov 19 19:59 .mozilla
[root@localhost ~]# chown -R hive.hive /home/hive/
[root@localhost ~]# ll /home/hive/ -a
total 28
drwxr-xr-x. 4 hive hive 4096 Nov 19 19:59 .
drwxr-xr-x. 4 root root 4096 Nov 19 19:59 ..
-rw-r--r--. 1 hive hive 18 Nov 19 19:59 .bash_logout
-rw-r--r--. 1 hive hive 176 Nov 19 19:59 .bash_profile
-rw-r--r--. 1 hive hive 124 Nov 19 19:59 .bashrc
drwxr-xr-x. 2 hive hive 4096 Nov 19 19:59 .gnome2
drwxr-xr-x. 4 hive hive 4096 Nov 19 19:59 .mozilla
[root@localhost ~]# chmod -R go= /home/hive/
[root@localhost ~]# ll /home/hive/ -ad
drwx------. 4 hive hive 4096 Nov 19 19:59 /home/hive/
[root@localhost ~]# ll /home/hive/ -a
total 28
drwx------. 4 hive hive 4096 Nov 19 19:59 .
drwxr-xr-x. 4 root root 4096 Nov 19 19:59 ..
-rw-------. 1 hive hive 18 Nov 19 19:59 .bash_logout
-rw-------. 1 hive hive 176 Nov 19 19:59 .bash_profile
-rw-------. 1 hive hive 124 Nov 19 19:59 .bashrc
drwx------. 2 hive hive 4096 Nov 19 19:59 .gnome2
drwx------. 4 hive hive 4096 Nov 19 19:59 .mozilla
[root@localhost ~]#

手动生成加密密码

[root@localhost ~]# openssl passwd -1 -salt '12345678'
Password:
$1$12345678$0ME5N6oDyoEAwUp7b5UDM/

nano /etc/shadow
hive:$1$12345678$0ME5N6oDyoEAwUp7b5UDM/:17125:0:99999:7:::

校验hive是否正常
[root@localhost ~]# su - hive
[hive@localhost ~]$ ll
total 0
[hive@localhost ~]$ touch test
[hive@localhost ~]$ ll
total 0
-rw-rw-r--. 1 hive hive 0 Nov 19 20:04 test
[hive@localhost ~]$ mkdir testdir
[hive@localhost ~]$ ll
total 4
-rw-rw-r--. 1 hive hive 0 Nov 19 20:04 test
drwxrwxr-x. 2 hive hive 4096 Nov 19 20:04 testdir
[hive@localhost ~]$

遮罩码:umask
(rw-rw-rw-)666-umask:创建文件(默认没有执行权限,如果有x权限,自动加+1)
(rwxrwxrwx)777-umask:创建目录

普通用户umask:002(-------w-)
[rhel@localhost ~]$ umask
0002
[rhel@localhost ~]$
[rhel@localhost tmp]$ touch test2.txt
[rhel@localhost tmp]$ ll test2.txt
-rw-rw-r--. 1 rhel rhel 0 Nov 19 18:32 test2.txt

管理员umask:022(----w--w-)
[root@localhost tmp]# umask
0022
[root@localhost tmp]#
[root@localhost tmp]# touch test.txt
[root@localhost tmp]# ll test.txt
-rw-r--r--. 1 root root 0 Nov 19 18:31 test.txt

计算器
bc
[root@localhost tmp]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
1*2
2
exit
0

q
0
^C
(interrupt) Exiting bc.

登录式shell:
正常通常某终端登录
su - USERNAME
su -l USERNAME

非登录式shell:
su USERNAME
图形终端下打开命令窗口
自动执行的shell脚本

bash的配置文件:
全局配置(/etc/)
/etc/profile, /etc/profile.d/*.sh, /etc/bashrc
个人配置(~/)
~/.bash_profile, ~/.bashrc

profile类的文件:
设定环境变量
运行命令或脚本

bashrc类的文件
设定本地变量
定义命令别名

登录式shell如何读取配置文件?
/etc/profile --> /etc/profile.d/*.sh --> ~/.bash_profile --> ~/.bashrc (个人设置)--> /etc/bashrc(全局配置)

非登录式shell如何配置文件?(不读取环境设置,/etc/profile,~/.bash_profile)
~/.bashrc --> /etc/basrc --> /etc/profile.d/*.sh

nano .bashrc
alias cls="clear"
su - root
cls

nano ~/.bash_profile
echo "hello world, welcome to cxiong's world, it's `date`"
[rhel@localhost ~]$ su - root
Password:
hello world, welcome to cxiong's world, it's Sat Nov 19 19:01:59 PST 2016
[root@localhost ~]#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐