• Linux3.3su、sudo、限制root远程登录


    su

      su命令使用中,加 - 与不加的区别。

    [root@chy002 ~]# su chy002
    [chy002@chy002 root]$ pwd
    /root
    [chy002@chy002 root]$ exit
    exit
    [root@chy002 ~]# su - chy002
    上一次登录:二 10月 31 04:57:13 CST 2017pts/0 上
    [chy002@chy002 ~]$ pwd
    /home/chy002
    

      

    [user@localhost ~]$ su
    密码:
    [root@localhost user]# pwd
    /home/user
    [root@localhost user]# echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
    [root@localhost user]# exit
    exit
    [user@localhost ~]$ su -
    密码:
    上一次登录:三 7月  4 04:20:13 CST 2018pts/2 上
    [root@localhost ~]# pwd
    /root
    [root@localhost ~]# echo $PATH
    /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
    [root@localhost ~]# exit
    登出
    

      

      -c 以某用户身份去执行某个权限

    [root@chy002 chy002]# su - -c "touch /home/chy002/123.txt" chy002
    [root@chy002 chy002]# ls -ls
    总用量 0
    0 -rw-rw-r-- 1 chy002 chy002 0 10月 31 06:03 123.txt
    [root@chy002 chy002]# id chy002
    uid=1000(chy002) gid=1000(chy002) 组=1000(chy002)
    

      

    [root@chy002 chy002]# su - chy002
    上一次登录:二 10月 31 06:06:43 CST 2017pts/0 上
    su: 警告:无法更改到 /home/chy002 目录: 没有那个文件或目录
    shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
    job-working-directory: 获取当前目录时出错: getcwd: 无法访问父目录: 没有那个文件或目录
    -bash-4.2$ 登出     #该用户没有家目录,所以前缀与普通用户不同
    [root@chy002 chy002]# ls /home/chy002
    ls: 无法访问/home/chy002: 没有那个文件或目录
    

      如何进行修改

    [root@chy002 ~]# ls /home/chy002    
    ls: 无法访问/home/chy002: 没有那个文件或目录
    [root@chy002 ~]# mkdir /home/chy002            #创建家目录,发现还是不行,家目录缺少配置文件
    [root@chy002 ~]# chown chy002:chy002 /home/chy002
    [root@chy002 ~]# su - chy002
    上一次登录:二 10月 31 06:06:50 CST 2017pts/0 上
    -bash-4.2$ pwd
    /home/chy002
    -bash-4.2$ ls -la
    总用量 4
    drwxr-xr-x   2 chy002 chy002    6 10月 31 06:12 .
    drwxr-xr-x. 11 root   root   4096 10月 31 06:12 ..
    
    #/etc/skel有配置文件模板,拷贝后修改权限,可以正常登录
    [root@chy002 ~]# ls -la /etc/skel
    总用量 24
    drwxr-xr-x.  2 root root   59 10月 17 06:16 .
    drwxr-xr-x. 73 root root 8192 10月 31 03:33 ..
    -rw-r--r--.  1 root root   18 6月  10 2014 .bash_logout
    -rw-r--r--.  1 root root  193 6月  10 2014 .bash_profile
    -rw-r--r--.  1 root root  231 6月  10 2014 .bashrc
    [root@chy002 ~]# cp /etc/skel/.bash* /home/chy002
    [root@chy002 ~]# chown -R chy002:chy002 !$
    chown -R chy002:chy002 /home/chy002
    [root@chy002 ~]# su - chy002
    上一次登录:二 10月 31 06:12:35 CST 2017pts/0 上
    [chy002@chy002 ~]$
    

    sudo

              可以让普通用户以指定用户的身份去执行一条命令。用用命令 visudo打开 /etc/sudoers.tmp也就是sudo的配置文件,visudo可以检测修改的错误。指定到第92行

    91 ## Allow root to run any commands anywhere
    92 root    ALL=(ALL)       ALL
    93 chy002     ALL=(ALL)     /usr/bin/ls, /usr/bin/mv, /usr/bin/cat     
    #第二项    ALL=(ALL)    第一个ALL就写成这样就行了,小括号里面ALL是用户,被授予哪个用户的身份
    #第三项    ALL     所有的命令,也可以写一个两个,要写绝对路径,且有空格
    

      

    [root@chy002 ~]# su - chy002
    上一次登录:三 11月  1 05:20:55 CST 2017pts/0 上
    [chy002@chy002 ~]$ ls /root       #一般情况是不能访问root目录
    ls: 无法打开目录/root: 权限不够
    [chy002@chy002 ~]$ sudo /usr/bin/ls /root
    
    我们信任您已经从系统管理员那里了解了日常注意事项。
    总结起来无外乎这三点:
    
        #1) 尊重别人的隐私。
        #2) 输入前要先考虑(后果和风险)。
        #3) 权力越大,责任越大。
    
    [sudo] chy002 的密码:
    anaconda-ks.cfg
    [chy002@chy002 ~]$ cat /root/anaconda-ks.cfg
    cat: /root/anaconda-ks.cfg: 权限不够
    [chy002@chy002 ~]$ sudo /usr/bin/cat /root/anaconda-ks.cfg
    #version=RHEL7
    # System authorization information
    ... ...
    
    %end
    

      在visudo第92行配置中第三项加上NOPASSWD: .可以通过命令别名、用户别名等,把若干项放到一起配置更方便。

    sudo : 暂时切换到超级用户模式以执行超级用户权限,提示输入密码时该密码为当前用户的密码,而不是超级账户的密码。不过有时间限制,Ubuntu默认为一次时长15分钟。

    su : 切换到某某用户模式,提示输入密码时该密码为切换后账户的密码,用法为“su 账户名称”。如果后面不加账户时系统默认为root账户,密码也为超级账户的密码。没有时间限制。

    sudo -i: 为了频繁的执行某些只有超级用户才能执行的权限,而不用每次输入密码,可以使用该命令。提示输入密码时该密码为当前账户的密码。没有时间限制。执行该命令后提示符变为“#”而不是“$”。想退回普通账户时可以执行“exit”或“logout” 。

    sudo -i 直接运行sudo命令加-i参数
    sudo su 运行sudo命令给su命令提权,运行su命令。
    sudo -i 运行结果 PWD=/root
    sudo su 运行结果 PWD=/home/用户名(当前用户主目录)

    限制root远程登录

      本地无法限制,限制远程root登录。远程只允许普通用户登录。

    #在root中添加visudo配置
    ## Allow root to run any commands anywhere
    root    ALL=(ALL)       ALL
    chy002  ALL=(root)      /bin/ls, /bin/cat, /bin/su
    
    
    #这样chy002就可以以root用户身份执行su命令,
    #就会使得普通用户在不知道root密码情况下,用root登录
    [root@chy002 ~]# su chy002
    [chy002@chy002 root]$ sudo su
    [sudo] chy002 的密码:
    [root@chy002 ~]# 
    
    [root@chy002 ~]# vi /etc/ssh/sshd_config
    
    48 PermitRootLogin no            #打开配置,把yes改成no
    
    [root@chy002 ~]# systemctl restart sshd.service    #修改后,重启服务
    

      尝试使用xshell的root登录,发现sshd拒绝了服务

      普通用户可以

  • 相关阅读:
    python file op
    python write read
    Linux MD RAID 10
    bitmap.h
    1
    write 1 to block device
    tr '00' '377' < /dev/zero | dd of=/dev/$i bs=1024 count=1024000
    Superblock
    echo -e "33[41;36m something here 33[0m"
    May It Be
  • 原文地址:https://www.cnblogs.com/chyuanliu/p/7757911.html
Copyright © 2020-2023  润新知