sudo授权、su切换用户、免密登陆与切换

1、由root用户切换至普通用户:

su chenfei

2、由普通用户切换至root用户

su root

但是需要输入密码

免密切换root用户的方法:

vim /etc/pam.d/su

#%PAM-1.0

auth            sufficient      pam_rootok.so

# Uncomment the following line to implicitly trust users in the "wheel" group.

auth            sufficient      pam_wheel.so trust use_uid #将这一行的#去掉,取消注释

# Uncomment the following line to require a user to be in the "wheel" group.

#auth          required        pam_wheel.so use_uid

auth            include        system-auth

account        sufficient      pam_succeed_if.so uid = 0 use_uid quiet

account        include        system-auth

password        include        system-auth

session        include        system-auth

session        optional        pam_xauth.so

保存退出

将普通用户添加到wheel组中

usermod -G wheel chenfei

然后再来切换即可

######################################

使用ssh或telnet登陆后如何自动切换root:

方法:

首先切到普通用户:su chenfei

然后回到家目录中:cd (cd就可以)

修改:vim .bash_profile

# .bash_profile

# Get the aliases and functions

if [ -f ~/.bashrc ]; then

. ~/.bashrc

fi

sudo su - root #加入这么一个授权的命令

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

回到root用户

su root #上面的操作可以实现由普通用户免密切换到root用户,所以这边不用输密码

修改sudoers这个文件

vim /etc/sudoers

root    ALL=(ALL)      ALL

chenfei ALL=(ALL)      NOPASSWD: ALL #在root后面添加普通用户

保存退出

切换普通用户可以进行测试: ssh登陆或者sudo su - root或sudo su root

su root与su - root的区别:

实践是检验真理的唯一标准:

[chenfei@master ~]$ sudo su root

[root@master chenfei]#

再看下一个:

[chenfei@master ~]$ sudo su - root

[root@master ~]#

你可能感兴趣的:(sudo授权、su切换用户、免密登陆与切换)