Linux(CentOS)服务器用户的一些常用管理方法
罗列一些常用的用户、用户组、权限等操作方法,方便理解和使用linux的权限
新建用户
新建用户命令:
useradd -d /home/serveradm -m serveradm # 新建serveradm用户并且设定其用户目录为 /home/serveradm
修改用户密码命令:
passwd myuser #回车输入密码 注意一定要带用户名,不然就是修改root!!!
限制用户权限命令:
chown serveradm -R /home/serveradm #这样用户myuser不能上传或修改其他目录文件,但是可以下载可以看,只读。
新建用户组
增加一个新的用户组使用groupadd命令。 其格式如下:
代码:
groupadd 选项 用户组
可以使用的选项有:
代码:
-g GID 指定新用户组的组标识号(GID)。
-o 一般与-g选项同时使用,表示新用户组的GID可以与系统已有用户组的GID相同。
groupadd group1 #此命令向系统中增加了一个新组group1,新组的组标识号是在当前已有的最大组标识号的基础上加1。
groupadd -g 101 group2 #此命令向系统中增加了一个新组group2,同时指定新组的组标识号是101。
添加用户到组
命令:
usermod -a -G groupA user
注意:不能直接用: usermod -G groupA,这样做会使你离开其他用户组,仅仅做为 这个用户组 groupA 的成员。
命令的所有的选项,及其含义:
Options:
-c, --comment COMMENT new value of the GECOS field
-d, --home HOME_DIR new home directory for the user account
-e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE
-f, --inactive INACTIVE set password inactive after expiration
to INACTIVE
-g, --gid GROUP force use GROUP as new primary group
-G, --groups GROUPS new list of supplementary GROUPS
-a, --append append the user to the supplemental GROUPS
mentioned by the -G option without removing
him/her from other groups
-h, --help display this help message and exit
-l, --login NEW_LOGIN new value of the login name
-L, --lock lock the user account
-m, --move-home move contents of the home directory to the new
location (use only with -d)
-o, --non-unique allow using duplicate (non-unique) UID
-p, --password PASSWORD use encrypted password for the new password
-s, --shell SHELL new login shell for the user account
-u, --uid UID new UID for the user account
-U, --unlock unlock the user account
查看用户所属的组使用命令:
$ groups user
或者查看文件:
$ cat /etc/group
普通用户赋予root权限
修改 /etc/sudoers 文件,找到下面一行,在root下面添加一行,如下所示:
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
bpascal ALL=(ALL) ALL
修改完毕,现在可以用bpascal帐号登录,然后用命令 su - ,即可获得root权限进行操作。
禁止root用户远程登录
修改sshd_config文件
#vi /etc/ssh/sshd_config
把
PermitRootLogin yes
改为
PermitRootLogin no
重启sshd服务
service sshd restart
远程管理用普通用户sshuser登录,然后用 su root 切换到root用户拿到最高权限