用户切换
用户切换
id
、whoami
:查看当前用户
[root@localhost ~]# id
uid=0(root) gid=0(root) 组=0(root)
[root@localhost ~]# whoami
root
su 命令
su 命令:
介绍:切换用户
格式:su [-] username
用法:
su - username :切换用户
su username :转换用户(环境变量不变)
su - :切换到root下
su --c “COMMAND” user1 :以user1的身份执行命令但,不切换
[root@localhost ~]# su - test
上一次登录:四 8月 2 22:33:52 CST 2018pts/0 上
[test@localhost ~]$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/test/.local/bin:/home/test/bin
[test@localhost ~]$ exit
登出
[root@localhost ~]# su test
[test@localhost root]$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[test@localhost root]$ exit
exit
[root@localhost ~]# su - test
上一次登录:四 8月 2 22:46:07 CST 2018pts/0 上
[test@localhost ~]$ su -
密码:
上一次登录:四 8月 2 21:39:44 CST 2018从 192.168.127.1pts/0 上
[root@localhost ~]# id
uid=0(root) gid=0(root) 组=0(root)
[root@localhost ~]# su --c "touch 11.txt" test
touch: 无法创建"11.txt": 权限不够
[root@localhost ~]# su --c "touch /tmp/1.txt" test
[root@localhost ~]# ls -l /tmp/1.txt
-rw-r--r-- 1 test test1 0 8月 2 22:47 /tmp/1.txt
普通用户使用root权限
sudo 命令:
介绍:让普通用户可以使用root用户的权限执行命令
格式:sudo command
用法:
sudo ls /root/
sudo -l :查看当前用户的sudo配置
如果要让不同用户能在某些命令中使用root的权限,需要配置文件。使用visudo
命令或直接编辑配置文件 /etc/sudoers
[root@localhost ~]# visudo
...
...
root ALL=(ALL) ALL
test ALL=(ALL) NOPASSWD: /usr/bin/cat, /usr/bin/mv, /usr/bin/ls
...
...
在
root ALL=(ALL) ALL
行下加入
test ALL=(ALL) NOPASSWD: /usr/bin/cat, /usr/bin/mv, /usr/bin/ls
test :表示用户
第一个ALL :表示在所有地方
第二个(ALL):表示所有的用户,也可用
(root, test)
或User_Alias ADMINS = root, test 中的 ADMINS
表示(注意空格)第三个ALL:表示命令,
NOPASSWD
表示sudo
命令时不同输入用户自身密码(注意命令绝对路径和空格,分离)
在sudo配置文件中,我们可以定义host、user以及命令的别名
# Host_Alias FILESERVERS = fs1, fs2
# User_Alias ADMINS = root, test
# Cmnd_Alias NETWORKING = /sbin/route, /sbin/ifconfig, /bin/ping
test Host_Alias=(User_Alias) Cmnd_Alias NETWORKING
# %wheel ALL=(ALL) ALL
表示组权限,设置同上
[root@localhost ~]# su - test
上一次登录:四 8月 2 22:59:30 CST 2018pts/0 上
[test@localhost ~]$ ls /root/
ls: 无法打开目录/root/: 权限不够
[test@localhost ~]$ sudo ls /root/
1.txt.bak anaconda-ks.cfg package-lock.json
[test@localhost ~]$ sudo cat /root/
cat: /root/: 是一个目录
[test@localhost ~]$ sudo cat /root/1.txt.bak
123
最后更新于
这有帮助吗?