Linuxlogin&nonloginshell以及su sudo相关概念

本文发布时间: 2019-Mar-21
1.login & non-login shellLinux系统自举时,内核会创建init进程,来进行一系列的系统初始化操作。每一个用户登录shell时,无论以伪终端登录:ssh,X11下控制台,还是tty控制台终端,都会读取相关相关的登录配置文件。linux 有两种登录shell:login和nologin:login shell:登录shell时需要完整的登录流程,称为 login shell。何为完整:输入用户名和密码。例如:走tty1-tty6控制终端,或走ssh等伪终端远程登入non-login shell:登入shell时不需要输入帐号信息。例如在X11下,打开伪终端,或者在shell下,进入shell子进程。这两种登入shell的区别是:在登入shell是,读取的配置文件不同。这里先介绍两个配置文件/etc/profile和~/.bashrc,在unix系统中,这两个shell环境的配置文件,是我们接触最多的两个文件:/etc/profile,处在shell配置文件的最顶端。这是系统shell环境的全局设定,例如PATH,MAIL很多环境变量。对它的修改,会影响到所有用户。~/.bashrc,处在shell配置文件的最低端。这是针对每个用户shell环境的配置文件,我们的大部分个性化的定制,都可以直接修改在这个文件中。login shell(bash)在登入时,会读取的配置文件:/etc/profile,全局配置~/.bash_profile 或~/.bash_login 或 ~/.profile,个人配置。之所以有三个文件,是因为不同的shell有可能命名不同,只会按顺序读取其中的一个。其实登入时不仅仅读取这两个文件,其中在/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 fifi# 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 ifi对于我的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 bashif [ -n "$BASH_VERSION" ]; then # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fifi# set PATH so it includes user's private bin if it existsif [ -d "$HOME/bin" ] ; then PATH="$HOME/bin:$PATH"filogin shell读取配置的流程如下图(来自:<鸟哥>):non-login shell(bash)在登入时,只会读取的配置文件:~/.bashrc。bashrc这个文件有时候不存在,需要自己创建,里面可以进行个性化的定制,不会影响到其他用户。2.su &sudo在我接触的Linux 发行版中,ubuntu在安装过程中不会提示root密码的设置,只有进入系统后才能在shell下通过passwd来设置root的密码,fedora,centos安装过程中多会要求设置root密码和建立一个常用的用户。可以看出linux的设计者们本身就期望用户以较低的权限来进行平时的操作,这是基于于安全的考虑。但在shell环境下,由于各种工作的需要,我们经常需穴ky"http:///qq/" target="_blank" class="keylink">qq9+NDQ08O7p8ioz961xMfQu7ujrNfus6PTw7XEvs3Kx7vxyKFyb29008O7p7XEyKjP3qGj1+6zo9PDtcTD/MHu09BzdbrNc3Vkb6GjPC9wPgo8cD48L3A+CjxwcmUgY2xhc3M9"brush:java;">su [-lc] [username]- , -l, --login : 表示使用以login shell的方式登录username,若username为空,则默认登录root。 如果没有该参数,则以nonlogin的方式登录-c, 仅执行一次命令 ,命令需要用引号括起来这里要强调的就是su 和su -的区别,就是前面撤了一大串的login 和non-login的区别。sudo命令的存在我觉得有两个原因:使用su切换到root,需要是所有用户都知道root密码,不安全;很多时候我们切换到root用户只是需要执行一条语句,尽管su -c可以完成,但每次都要敲个空格-c,而且每次都要输入root密码;上面两个原因,就是sudo存在的理由。sudo可以让用户通过验证自己的密码来获得其他用户的权限,只需要root对/etc/sudoers进行相关的配置,我的sudoers文件的内容如下:# User privilege specificationroot 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) ALL sudoers中设置一个用户具有sudo权限的方式如下:登入者账号 登入者的来源主机名=(可切换身份)可执行的命令登入者帐号可以为:个人,群组,别名。群组需要在前面加%来标志对于别名,sudoers结构的四个组成部分都可以用别名表示:User_Alias , Runas_Alias, Host_Alias, Cmnd_Alias 每个别名的命名格式:Alias_Type NAME = item1, item2, ...,别名NAME必须由大写字母,数字,下划线组成,开头为大写字母。例如:User_Alias SBGROUP = user1, user2, user3SBGROUP ALL=(ALL) ALL为了保证/etc/sudoers语法的正确,我们一般通过visudo来编辑该文件。


(以上内容不代表本站观点。)
---------------------------------
本网站以及域名有仲裁协议。
本網站以及域名有仲裁協議。

2024-Mar-04 02:08pm
栏目列表