linux账号权限分配
一、创建只读用户
先创建一个用户
[root@wyw ~]# useradd -s /bin/bash read-only
把它的家目录创建出来
[root@wyw ~]# mkdir /home/read-only/.bin
修改属主以及它的权限
[root@wyw ~]# chown root. /home/read-only/.bash_profile
[root@wyw ~]# chmod 755 /home/read-only/.bash_profile
修改他的.bash_profile
[root@wyw ~]# vim /home/read-only/.bash_profile
#Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
#User specific environment and startup programs
#PATH=P A T H : PATH:PATH:HOME/bin
PATH=$HOME/.bin
export PATH
设置密码
[root@wyw ~]# passwd read-only
更改用户 read-only 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
把允许它使用的命令软连接到他的.bin下
[root@wyw ~]# ln -s /usr/bin/wc /home/read-only/.bin/wc
[root@wyw ~]# ln -s /usr/bin/tail /home/read-only/.bin/tail
[root@wyw ~]# ln -s /bin/more /home/read-only/.bin/more
[root@wyw ~]# ln -s /bin/cat /home/read-only/.bin/cat
[root@wyw ~]# ln -s /bin/grep /home/read-only/.bin/grep
[root@wyw ~]# ln -s /bin/find /home/read-only/.bin/find
[root@wyw ~]# ln -s /bin/pwd /home/read-only/.bin/pwd
[root@wyw ~]# ln -s /bin/ls /home/read-only/.bin/ls
[root@wyw ~]# ln -s /bin/less /home/read-only/.bin/less
[root@wyw ~]# ln -s /bin/tar /home/read-only/.bin/tar
[root@wyw ~]# ln -s /usr/bin/ps /home/read-only/.bin/ps
[root@wyw ~]# ln -s /usr/bin/vim /home/read-only/.bin/vim
切换用户测试结果:
随便打几个没有授权的命令
二、创建普通用户拥有sudo权限
第一步还是先创建一个用户
[root@wyw ~]# useradd wyw
[root@wyw ~]# passwd wyw
更改用户 wyw 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
修改sudo配置文件
输入下面这个命令
[root@wyw ~]# visudo
wyw ALL=(ALL) NOPASSWD: ALL
[wyw@wyw ~]$ sudo ls /root
anaconda-ks.cfg
[wyw@wyw ~]$ ls /root
ls: 无法打开目录/root: 权限不够
可以达到上面的效果输入sudo执行出来的命令相当于用root执行的
三、禁止root用户通过ssh登录
前言:为什么要禁用root用户通过ssh登录,因为在真正的生产环境要防止被人挖矿,或者植入脚本暴力破解root密码等等。所有root这个超级管理员最好是禁止通过ssh登录.
附带一份修改的配置文件
[root@iZ8vbgv8uybhcjZ ssh]# cat sshd_config
# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/usr/bin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
Protocol 2
HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
# Ciphers and keying
#RekeyLimit default none
# Logging
#SyslogFacility AUTH
#SyslogFacility AUTHPRIV
#LogLevel INFO
# Authentication:
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
MaxAuthTries 3
#MaxAuthTries 6
#MaxSessions 10
RSAAuthentication yes
PubkeyAuthentication yes
#PubkeyAuthentication yes
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys
#AuthorizedPrincipalsFile none
#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes
# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
#PasswordAuthentication yes
# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no
# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes
# GSSAPI options
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
#GSSAPIEnablek5users no
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
# WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several
# problems.
UsePAM yes
AllowUsers root rrf audadmin secadmin read-only git
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation sandbox
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
#UseDNS yes
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none
# no default banner path
#Banner none
# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
# override default of no subsystems
Subsystem sftp /usr/libexec/openssh/sftp-server
# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
UseDNS no
AddressFamily inet
SyslogFacility AUTHPRIV
PermitRootLogin yes
PasswordAuthentication yes