标签:login shell non-login shell sudo su-
Linux系统自举时,内核会创建init进程,来进行一系列的系统初始化操作。每一个用户登录shell时,无论以伪终端登录:ssh,X11下控制台,还是tty控制台终端,都会读取相关相关的登录配置文件。linux 有两种登录shell:login和nologin:
这两种登入shell的区别是:在登入shell是,读取的配置文件不同。这里先介绍两个配置文件/etc/profile和~/.bashrc,在unix系统中,这两个shell环境的配置文件,是我们接触最多的两个文件:
login shell(bash)在登入时,会读取的配置文件:
其实登入时不仅仅读取这两个文件,其中在/etc/profile文件中,还会摄入其他的配置文件,例如我的ubuntu机器上该文件的内容如下:
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if [ "$PS1" ]; then
#如果shell环境存在且不为sh,就读取/etc/bash.bashrc
if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
# The file bash.bashrc already sets the default PS1.
# PS1='\h:\w\$ '
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ "`id -u`" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
fi
# The default umask is now handled by pam_umask.
# See pam_umask(8) and /etc/login.defs.
#读取/etc/profile.d目录下所有的sh文件
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi对于我的ubuntu机器中~/.profile文件内容如下:最后shell会读取~/.bashrc文件。# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
login shell读取配置的流程如下图(来自:<鸟哥>):
non-login shell(bash)在登入时,只会读取的配置文件:~/.bashrc。bashrc这个文件有时候不存在,需要自己创建,里面可以进行个性化的定制,不会影响到其他用户。
在我接触的Linux 发行版中,ubuntu在安装过程中不会提示root密码的设置,只有进入系统后才能在shell下通过passwd来设置root的密码,fedora,centos安装过程中多会要求设置root密码和建立一个常用的用户。可以看出linux的设计者们本身就期望用户以较低的权限来进行平时的操作,这是基于于安全的考虑。
但在shell环境下,由于各种工作的需要,我们经常需要进行用户权限的切换,最常用的就是获取root用户的权限。最常用的命令有su和sudo。
su [-lc] [username]
- , -l, --login : 表示使用以login shell的方式登录username,若username为空,则默认登录root。
如果没有该参数,则以nonlogin的方式登录
-c, 仅执行一次命令 ,命令需要用引号括起来这里要强调的就是su 和su -的区别,就是前面撤了一大串的login 和non-login的区别。sudo命令的存在我觉得有两个原因:
上面两个原因,就是sudo存在的理由。sudo可以让用户通过验证自己的密码来获得其他用户的权限,只需要root对/etc/sudoers进行相关的配置,我的sudoers文件的内容如下:
# User privilege specification root ALL=(ALL:ALL) ALL # admin组成员可以切换到任何用户执行任何命令 %admin ALL=(ALL) ALL # Allow members of group sudo to execute any command %sudo ALL=(ALL:ALL) ALL #users群组不需要密码进行切换 %users ALL=(ALL) NOPASSWD:ALL #允许guest切换到samba_group组中的所有用户 guest ALL=(%samba_group:ALL) ALLsudoers中设置一个用户具有sudo权限的方式如下:
登入者账号 登入者的来源主机名=(可切换身份)可执行的命令
登入者帐号可以为:个人,群组,别名。群组需要在前面加%来标志
对于别名,sudoers结构的四个组成部分都可以用别名表示:User_Alias , Runas_Alias, Host_Alias, Cmnd_Alias
每个别名的命名格式:Alias_Type NAME = item1, item2, ...,别名NAME必须由大写字母,数字,下划线组成,开头为大写字母。
例如:
User_Alias SBGROUP = user1, user2, user3
SBGROUP ALL=(ALL) ALL
为了保证/etc/sudoers语法的正确,我们一般通过visudo来编辑该文件。
Linux login & non-login shell 以及su, sudo相关概念
标签:login shell non-login shell sudo su-
原文地址:http://blog.csdn.net/anonymalias/article/details/41913207