Linux:CentOS7.4新建用戶並授權
之前買了一台阿里雲服務器,准備用來搭建一些服務,由於使用root用戶登錄進行操作比較敏感,就新建了一個用戶,用來登錄並進行日常操作。
這篇博客,介紹下centos7.4下如何新建用戶並且授權。。。
一、創建新用戶
1、創建一個新用戶:prefma
[root@localhost ~]# adduser prefma
2、為新用戶創建初始化密碼
[root@localhost~]# passwd prefma Changing password for user prefma. New password: # 輸入密碼 Retype new password: # 再次輸入密碼 passwd: all authentication tokens updated successfully.
二、授權
個人用戶的權限只可以在本home下有完整權限,其他目錄需要別人授權。經常需要root用戶的權限,可以通過修改sudoers文件來賦予權限。
新創建的用戶並不能使用sudo命令,需要給他添加授權。
1、查找sudoers文件路徑並賦予權限
1 [root@localhost~]# whereis sudoers # 查找sudoers文件路徑 2 sudoers: /etc/sudoers /etc/sudoers.d /usr/share/man/man5/sudoers.5.gz 3 [root@localhost~]# ls -l /etc/sudoers # 查看權限 4 -r--r----- 1 root root 3938 Sep 6 2017 /etc/sudoers # 只有讀權限 5 [root@localhost~]# chmod -v u+w /etc/sudoers # 賦予讀寫權限 6 mode of ‘/etc/sudoers’ changed from 0440 (r--r-----) to 0640 (rw-r-----)
2、修改sudoers文件
輸入命令 vim /etc/sudoers 修改sudoers文件,添加新用戶信息:
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
prefma ALL=(ALL) ALL #這個是新用戶
然后輸入命令 wq! 保存修改。
3、收回權限
[root@localhost~]# chmod -v u-w /etc/sudoers
mode of ‘/etc/sudoers’ changed from 0640 (rw-r-----) to 0440 (r--r-----)
4、新用戶登錄
新建連接,使用新創建的用戶登錄,並進行驗證,比如:
[prefma@localhost~]$ pwd
/home/prefma
[prefma@localhost~]$ ls -l /etc/sudoers
-r--r----- 1 root root 3995 Oct 16 22:42 /etc/sudoers
更多關於Linux和centos的內容,可以關注Linux公社。。。