用戶及文件目錄命令

問題1、Linux上的文件管理類命令都有哪些,其經常使用的使用方法及其相關示例演示。

經常使用的文件管理類命令:cp、 mv、 rm、file、ls、touch、more、less、head、tail、stat

一、cp命令:

cp--copy的簡寫,複製文件和目錄的命令;node

命令格式:python

cp [OPTION]... [-T] SOURCE DEST----單源複製git

若是DEST不存在,直接建立一個文件,複製源文件的數據流至DEST;shell

例如,複製/etc/fstab文件到/tmp目錄下,若是/tmp沒有一個叫fstab的文件,就先建立一個fstab文件,而後將fstab的數據流複製到/tmp的fstab中:vim

[root@magedu ~]# cp /etc/fstab /tmp/

[root@magedu ~]# ll /tmp/fstab

-rw-r--r-- 1 root root 465 Mar 25 16:51 /tmp/fstab

若是DEST是已存在的非目錄文件,則覆蓋;centos

例如,再複製/etc/fstab文件到/tmp目錄下,就會覆蓋第一次複製的文件,從時間上可看出來:bash

[root@magedu ~]# ll /tmp/fstab

-rw-r--r-- 1 root root 465 Mar 25 16:51 /tmp/fstab

[root@magedu ~]# cp /etc/fstab /tmp/fstab

cp: overwrite ‘/tmp/fstab’? y

[root@magedu ~]# ll /tmp/fstab

-rw-r--r-- 1 root root 465 Mar 25 16:58 /tmp/fstab

若是DEST是目錄文件,則先在DEST目錄下建立一個源文件同名的文件,並複製其流。session

例如:app

[root@magedu ~]# cp /etc/ntp.conf /tmp/

[root@magedu ~]# ll /tmp/ntp.conf

-rw-r--r-- 1 root root 2000 Mar 29 20:49 /tmp/ntp.conf

cp [OPTION]... SOURCE... DIRECTORY---->多源複製less

cp [OPTION]... -t DIRECTORY SOURCE... ---->多源複製

[root@magedu ~]# cp /etc/yum.conf /etc/vimrc /tmp

[root@magedu ~]# ll /tmp/yum.conf /tmp/vimrc

-rw-r--r-- 1 root root 1993 Mar 29 20:53 /tmp/vimrc

-rw-r--r-- 1 root root  970 Mar 29 20:53 /tmp/yum.conf

OPTIONS:

-i:交互式複製,覆蓋以前詢問

[root@magedu ~]# cp -i /etc/ntp.conf /tmp

cp: overwrite ‘/tmp/ntp.conf’? y

[root@magedu ~]# ls /tmp/ntp.conf

/tmp/ntp.conf

-f:強行覆蓋,覆蓋時不詢問

[root@magedu ~]# cp -f /etc/ntp.conf /tmp

[root@magedu ~]#

-r:遞歸複製目錄

[root@magedu ~]# cp -r /etc/yum.repos.d/ /tmp/yum

[root@magedu ~]# ll /tmp/yum

total 0

drwxr-xr-x 2 root root 187 Mar 29 21:08 yum.repos.d

[root@magedu ~]# tree /tmp/yum

/tmp/yum

`-- yum.repos.d

​    |-- CentOS-Base.repo

​    |-- CentOS-CR.repo

​    |-- CentOS-Debuginfo.repo

​    |-- CentOS-fasttrack.repo

​    |-- CentOS-Media.repo

​    |-- CentOS-Sources.repo

​    `-- CentOS-Vault.repo

1 directory, 7 files

-a:複製源文件的全部屬性,實現歸檔,通常用於備份;

-rw-------. 1 root   root        107 Mar 27 13:37 yum.log

[root@magedu ~]# cp -a /var/log/yum.log /tmp

[root@magedu ~]# ll /tmp/yum.log

-rw-------. 1 root root 107 Mar 27 13:37 /tmp/yum.log

\#可看到,yum.log 在源文件中的元數據複製到tmp目錄下後,都沒變化;
二、mv命令:

​ ---移動或重命名文件;

移動目錄時,直接跟目錄名,不須要加R選項,也就是移動文件和目錄是同樣的。

[root@magedu ~]# mv /tmp/yum/ ./

[root@magedu ~]# ll yum

total 0

drwxr-xr-x 2 root root 187 Mar 29 21:08 yum.repos.d

OPTIONS:

-i:交互式,移動覆蓋前詢問

[root@magedu ~]# mv -i /tmp/yum/yum.repos.d/CentOS-Base.repo ./yum/yum.repos.d/CentOS-Base.repo

mv: overwrite ‘./yum/yum.repos.d/CentOS-Base.repo’? y

[root@magedu ~]#

-f:force 強制覆蓋;

[root@magedu ~]# mv -f /tmp/yum/yum.repos.d/CentOS-Sources.repo ./yum/yum.repos.d/

[root@magedu ~]#
三、rm命令:

NAME

​ rm-remove files or directories --刪除文件或目錄

SYNOPSIS

​ rm [OPTION] ... FILE...

OPTIONS

-r:刪除目錄

[root@magedu ~]# rm -r /tmp/yum/yum.repos.d/

rm: descend into directory ‘/tmp/yum/yum.repos.d/’? y

rm: remove regular file ‘/tmp/yum/yum.repos.d/CentOS-CR.repo’? y

rm: remove regular file ‘/tmp/yum/yum.repos.d/CentOS-Debuginfo.repo’? y

rm: remove regular file ‘/tmp/yum/yum.repos.d/CentOS-Media.repo’? y

rm: remove regular file ‘/tmp/yum/yum.repos.d/CentOS-Vault.repo’? y

rm: remove regular file ‘/tmp/yum/yum.repos.d/CentOS-fasttrack.repo’? y

rm: remove directory ‘/tmp/yum/yum.repos.d/’? y

[root@magedu ~]#

-f:強制刪除

[root@magedu ~]# rm -rf ./yum/

[root@magedu ~]#
四、file命令:

----查看文件類型命令

[root@magedu ~]# file /etc/passwd

/etc/passwd: ASCII text

[root@magedu ~]# file /etc/

/etc/: directory

[root@magedu ~]# file /dev/tty44

/dev/tty44: character special
五、ls 命令

​ ----list directory contents 列出列表,

SYNOPSIS

​ ls [OPTION]...[FILE]

OPTIONS

-a:列出全部文件,包括隱藏文件

[root@magedu ~]# ls -a /var/log

.                   lastlog            secure-20190317

..                  libvirt            secure-20190324

anaconda            maillog            speech-dispatcher

audit               maillog-20190303   spooler

boot.log            maillog-20190310   spooler-20190303

boot.log-20190228   maillog-20190317   spooler-20190310

boot.log-20190306   maillog-20190324   spooler-20190

-A:顯示除.和..的全部文件

[root@magedu ~]# ls -A /var/log

anaconda            libvirt            secure-20190317

audit               maillog            secure-20190324

boot.log            maillog-20190303   speech-dispatcher

boot.log-20190228   maillog-20190310   spooler

boot.log-20190306   maillog-20190317   spooler-20190303

-l:long 長格式列表,顯示文件詳細屬性信息,至關於ll;

[root@magedu ~]# ls -l /usr

total 256

dr-xr-xr-x.   2 root root 49152 Mar 27 13:37 bin

drwxr-xr-x.   2 root root     6 Apr 11  2018 etc

drwxr-xr-x.   2 root root     6 Apr 11  2018 games

drwxr-xr-x.   9 root root  4096 Oct 16 10:29 include

dr-xr-xr-x.  41 root root  4096 Oct 16 10:29 lib

dr-xr-xr-x. 142 root root 77824 Oct 16 10:31 lib64

-h:對文件大小單位換算,通常和-l一塊使用

[root@magedu ~]# ls -lh /usr

total 256K

dr-xr-xr-x.   2 root root  48K Mar 27 13:37 bin

drwxr-xr-x.   2 root root    6 Apr 11  2018 etc

drwxr-xr-x.   2 root root    6 Apr 11  2018 games

drwxr-xr-x.   9 root root 4.0K Oct 16 10:29 include

dr-xr-xr-x.  41 root root 4.0K Oct 16 10:29 lib

dr-xr-xr-x. 142 root root  76K Oct 16 10:31 lib64

drwxr-xr-x.  45 root root  12K Oct 16 10:29 libexec

drwxr-xr-x.  12 root root  131 Oct 16 10:25 local

dr-xr-xr-x.   2 root root  20K Oct 16 10:29 sbin

drwxr-xr-x. 231 root root 8.0K Oct 16 10:29 share

-d:查看指定目錄的自身屬性,通常和-l一塊使用

[root@magedu ~]# ls -ld /etc/

drwxr-xr-x. 139 root root 8192 Mar 29 10:11 /etc/

-r:逆序顯示

[root@magedu ~]# ll -r /etc

total 1420

drwxr-xr-x.  2 root root      187 Mar 28 15:45 yum.repos.d

-rw-r--r--.  1 root root      970 Apr 13  2018 yum.conf

drwxr-xr-x.  6 root root      100 Oct 16 10:26 yum

drwxr-xr-x.  2 root root       21 Oct 16 10:26 xml

drwxr-xr-x.  2 root root        6 Apr 11  2018 xinetd.d

drwxr-xr-x.  6 root root      116 Oct 16 10:28 xdg

drwxr-xr-x.  6 root root      103 Oct 16 10:26 X11

-rw-r--r--.  1 root root        0 Jun 10  2014 wvdial.conf

drwxr-xr-x.  2 root root       33 Oct 16 10:27 wpa_supplicant

-R:遞歸顯示

[root@magedu ~]# ll -R /var/log

/var/log/samba:

total 0

drwx------. 2 root root 6 Apr 13  2018 old

/var/log/samba/old:

total 0

/var/log/speech-dispatcher:

total 0

/var/log/sssd:

total 0

/var/log/tuned:

total 8

-rw-r--r--. 1 root root 7159 Mar  5 09:24 tuned.log
六、touch 命令

NAME

touch - change file timestamps--修改文件的時間戳,可用於建立文件;

[root@magedu ~]# touch /tmp/touch.test

[root@magedu ~]# ll /tmp/touch.test

-rw-r--r-- 1 root root 0 Mar 29 22:06 /tmp/touch.test

SYNOPSIS

touch [OPTION]... FILE...

OPTIONS

-a:僅修改access time;

[root@magedu ~]# stat /tmp/touch.test

  File: ‘/tmp/touch.test’

  Size: 0             Blocks: 0          IO Block: 4096   regular empty file

Device: fd00h/64768d    Inode: 67976088    Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2019-03-29 22:09:05.609128074 +0800

Modify: 2019-03-29 22:09:05.609128074 +0800

Change: 2019-03-29 22:09:05.609128074 +0800

Birth: -

[root@magedu ~]# touch -a /tmp/touch.test

[root@magedu ~]# stat /tmp/touch.test

  File: ‘/tmp/touch.test’

  Size: 0             Blocks: 0          IO Block: 4096   regular empty file

Device: fd00h/64768d    Inode: 67976088    Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2019-03-29 22:09:25.882986471 +0800

Modify: 2019-03-29 22:09:05.609128074 +0800

Change: 2019-03-29 22:09:25.882986471 +0800

Birth: -

-m:僅修改modify time;

[root@magedu ~]# touch -m /tmp/touch.test

[root@magedu ~]# stat /tmp/touch.test

  File: ‘/tmp/touch.test’

  Size: 0             Blocks: 0          IO Block: 4096   regular empty file

Device: fd00h/64768d    Inode: 67976088    Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2019-03-29 22:09:25.882986471 +0800

Modify: 2019-03-29 22:11:12.518241679 +0800

Change: 2019-03-29 22:11:12.518241679 +0800

Birth: -

-t:STAMP,可指定時間戳,格式:[[CC]YY]MMDDhhmm[.ss]

[root@magedu ~]# touch -t 201903290830.33 /tmp/touch.test

[root@magedu ~]# stat /tmp/touch.test

  File: ‘/tmp/touch.test’

  Size: 0             Blocks: 0          IO Block: 4096   regular empty file

Device: fd00h/64768d    Inode: 67976088    Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2019-03-29 08:30:33.000000000 +0800

Modify: 2019-03-29 08:30:33.000000000 +0800

Change: 2019-03-29 22:14:02.791052405 +0800

Birth: -
七、more命令:分屏查看文件內容的命令

格式:

more FILENAME

[root@magedu ~]# more /etc/rc.d/init.d/functions

\# -*-Shell-script-*-

\#

\# functions This file contains functions to be used by most or all

\#       shell scripts in the /etc/init.d directory.

\#

TEXTDOMAIN=initscripts

\# Make sure umask is sane

umask 022

\# Set up a default search path.

PATH="/sbin:/usr/sbin:/bin:/usr/bin"

export PATH

if [ $PPID -ne 1 -a -z "$SYSTEMCTL_SKIP_REDIRECT" ] && \
八、less命令:分屏查看文件內容的命令

格式:

less FILENAME:

[root@magedu ~]# less /etc/rc.d/init.d/functions

注:more和less的區別是 more翻到文件的最後一行後,會自動退出;less而不會,man命令其實調用的是less命令;

十、head:查看文件頭幾行的命令,默認是前10行;

格式:head FILE

[root@magedu ~]# head /etc/rc.d/init.d/functions

\# -*-Shell-script-*-

\#

\# functions This file contains functions to be used by most or all

\#       shell scripts in the /etc/init.d directory.

\#

TEXTDOMAIN=initscripts

\# Make sure umask is sane

umask 022

OPTIONS

-n #: 指定查看#行;

[root@magedu ~]# head -n 5 /etc/rc.d/init.d/functions

\# -*-Shell-script-*-

\#

\# functions This file contains functions to be used by most or all

\#       shell scripts in the /etc/init.d directory.

\#
十一、tail命令:

查看文件的後n行;

格式:

tail [option] ifle

[root@magedu ~]# tail /etc/fstab

\#

\# /etc/fstab

\# Created by anaconda on Tue Oct 16 10:22:23 2018

\#

\# Accessible filesystems, by reference, are maintained under '/dev/disk'

\# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

\#

/dev/mapper/centos-root /                       xfs     defaults        0 0

UUID=9a3bd9e0-b338-4cee-8a0c-ab1ad4d4a80f /boot                   xfs     defaults        0 0

/dev/mapper/centos-swap swap                    swap    defaults        0 0

[root@magedu ~]#

-n # :查看後#行;

[root@magedu ~]# tail -2 /etc/fstab

UUID=9a3bd9e0-b338-4cee-8a0c-ab1ad4d4a80f /boot                   xfs     defaults        0 0

/dev/mapper/centos-swap swap                    swap    defaults        0 0

[root@magedu ~]#

-f:查看完成後不退出,通常用於監控日誌等;Ctrl+c退出

[root@magedu ~]# ll /var/log/secure-20190310

-rw------- 1 root root 4140 Mar  8 17:38 /var/log/secure-20190310

[root@magedu ~]# tail -f /var/log/secure-20190310

Mar  5 20:50:25 magedu sshd[10449]: Accepted password for root from 172.16.200.88 port 63255 ssh2

Mar  5 20:50:26 magedu sshd[10449]: pam_unix(sshd:session): session opened for user root by (uid=0)

Mar  5 21:30:46 magedu sshd[10449]: pam_unix(sshd:session): session closed for user root

Mar  6 17:25:58 magedu sshd[2027]: pam_unix(sshd:session): session closed for user root

Mar  7 09:36:33 magedu sshd[31722]: Accepted password for root from 172.16.0.249 port 10712 ssh2

Mar  7 09:36:33 magedu sshd[31722]: pam_unix(sshd:session): session opened for user root by (uid=0)

Mar  7 16:20:13 magedu sshd[18305]: Accepted password for root from 172.16.251.149 port 53805 ssh2

Mar  7 16:20:13 magedu sshd[18305]: pam_unix(sshd:session): session opened for user root by (uid=0)

Mar  7 20:00:39 magedu sshd[18305]: pam_unix(sshd:session): session closed for user root

Mar  8 17:38:08 magedu sshd[31722]: pam_unix(sshd:session): session closed for user root
十二、stat命令:顯示文件時間戳
[root@magedu ~]# stat /etc/passwd

File: ‘/etc/passwd’

Size: 2837 Blocks: 8 IO Block: 4096 regular file

Device: fd00h/64768d Inode: 67976084 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Access: 2019-03-30 10:11:22.003980561 +0800

Modify: 2019-03-29 10:11:22.332144545 +0800

Change: 2019-03-29 10:11:22.332144545 +0800

Birth: -

問題2、使用命令行展開功能,建立/tmp/a1, /tmp/a2, /tmp/a1/a, /tmp/a1/b,在/tmp目錄下建立目錄:x_y, x_z, q_y, q_z

[root@magedu ~]# mkdir -p /tmp/{a1/{a,b},a2}

[root@magedu ~]# tree /tmp/

/tmp/

|-- a1

|   |-- a

|   `-- b

|-- a2

[root@magedu ~]# mkdir -v  /tmp/{x,q}_{y,z}

mkdir: created directory ‘/tmp/x_y’

mkdir: created directory ‘/tmp/x_z’

mkdir: created directory ‘/tmp/q_y’

mkdir: created directory ‘/tmp/q_z’

問題3、文件的元數據信息有哪些,分別表示什麼含義,如何查看?如何修改文件的時間戳信息。

文件的元數據包括:文件類型、文件權限、文件被連接的次數 、屬主、屬組、文件大小、時間戳、文件名等;

可使用 ls -l File_Name 命令查看,例如:

[root@magedu ~]# ll -rd /var/log

drwxr-xr-x. 19 root root 4096 Mar 24 03:10 /var/log

d--是文件類型,表明的是此文件是目錄;還有其它的文件類型

​ -:表明的是文件

​ l:的意思是連接文件,至關於Windows中的快捷方式;

​ p:是管道;

​ b:塊設備文件;

​ c:字符設備文件;

​ s:套接字文件;

rwxr-xr-x--是文件的權限規定;權限有三類:r--讀,w--寫,x--執行;分三組:屬主、屬組、其餘,每組三位;-表明的是沒有;例如:rwxr-xr-x前三位的rwx表明文件的屬主有讀寫執行的權限。中間三位r-x表明文件屬主的屬組有讀執行的權限;後三位r-x表明即不是屬主也不是屬組的其餘用戶權限是讀和執行;

19--是表明這個文件被連接的次數;

root--是表明文件屬主的名稱;

root--是表明文件屬主的屬組的名稱;

4096--是表明文件的大小;

Mar 24 03:10--是表明文件的最後修改的時間;

/var/log--是文件名;

可能經過 stat命令來查看文件的元數據:

例:

[root@magedu ~]# stat /var/log

  File: ‘/var/log’

  Size: 4096          Blocks: 8          IO Block: 4096   directory

Device: fd00h/64768d    Inode: 37059       Links: 19

Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2019-03-24 03:10:02.285254964 +0800

Modify: 2019-03-24 03:10:02.293254907 +0800

Change: 2019-03-24 03:10:02.293254907 +0800

Birth: -

時間戳的時間主要有三個:

訪問:Access Time,訪問的時間;

修改:Modify Time:修改文件數據的時間;

改變Change Time:元數據發生改變的時間;

若是要修改時間戳的時間,用touch命令:

​ touch -a File_Name 修改訪問時間;

​ touch -m File_Name 修改修改時間;

問題4、在/tmp目錄下建立以tfile開頭,後跟當前日期和時間的文件,文件名形如:tfile-2016-05-27-09-32-22。

[root@magedu ~]# touch /tmp/tfile-$(date +%Y-%m-%d-%H-%M-%S)

[root@magedu ~]# ll /tmp/tfile-2019-03-26-21-20-27

-rw-r--r-- 1 root root 0 Mar 26 21:20 /tmp/tfile-2019-03-26-21-20-27

問題5、複製/etc目錄下全部以p開頭,以非數字結尾的文件或目錄到/tmp/mytest1目錄中。

[root@magedu ~]# mkdir /tmp/mytest1

[root@magedu ~]# cp -r /etc/p*[^[:digit:]] /tmp/mytest1/

[root@magedu ~]# ll /tmp/mytest1/

total 48

drwxr-xr-x  2 root root 4096 Mar 26 21:34 pam.d

-rw-r--r--  1 root root 2526 Mar 26 21:34 passwd

-rw-r--r--  1 root root 2485 Mar 26 21:34 passwd-

-rw-r--r--  1 root root 1362 Mar 26 21:34 pbm2ppa.conf

-rw-r--r--  1 root root 2872 Mar 26 21:34 pinforc

drwxr-xr-x 10 root root  116 Mar 26 21:34 pki

drwxr-xr-x  2 root root   28 Mar 26 21:34 plymouth

drwxr-xr-x  5 root root   52 Mar 26 21:34 pm

-rw-r--r--  1 root root 6300 Mar 26 21:34 pnm2ppa.conf

drwxr-xr-x  2 root root    6 Mar 26 21:34 popt.d

drwxr-xr-x  2 root root  154 Mar 26 21:34 postfix

drwxr-xr-x  3 root root  219 Mar 26 21:34 ppp

drwxr-xr-x  2 root root  105 Mar 26 21:34 prelink.conf.d

-rw-r--r--  1 root root  233 Mar 26 21:34 printcap

-rw-r--r--  1 root root 1819 Mar 26 21:34 profile

drwxr-xr-x  2 root root 4096 Mar 26 21:34 profile.d

-rw-r--r--  1 root root 6545 Mar 26 21:34 protocols

drwxr-xr-x  2 root root   79 Mar 26 21:34 pulse

drwxr-xr-x  2 root root   23 Mar 26 21:34 purple

drwxr-xr-x  2 root root   35 Mar 26 21:34 python

問題6、建立用戶tom,指定UID爲5001,指定家目錄爲/tmp/tom, 指定shell爲/bin/zsh, 指定基本組爲tom,附加組爲jack

[root@magedu ~]# groupadd jack

[root@magedu ~]# useradd -u 5001 -d /tmp/tom -s /bin/zsh -G jack tom

[root@magedu ~]# tail -1 /etc/passwd

tom:x:5001:5004::/tmp/tom:/bin/zsh

[root@magedu ~]# tail -2 /etc/group

jack:x:5003:tom

tom:x:5004:

[root@magedu ~]#

問題7、經常使用的用戶以及目錄管理命令有哪些,並演示命令以及用法。

用戶管理類命令:

一、useradd:添加用戶

SYNOPSIS

useradd [options] LOGIN_NAME

useradd後面直接跟上要建立的用戶名,就可添加一個新用戶;

例:

[root@magedu ~]# useradd shared

[root@magedu ~]#

OPTIONS

-u,--uid UID:指定UID,默認自動根據前一個用戶名的ID日後增長;

[root@magedu ~]# useradd -u 1345 oneday

[root@magedu ~]# tail -1 /etc/passwd

oneday:x:1345:1345::/home/oneday:/bin/bash

-g,--gid GID:指定GID,指定的組ID必須提早建立;

[root@magedu ~]# useradd -g 1345 twoday

[root@magedu ~]# tail -1 /etc/passwd

twoday:x:5009:1345::/home/twoday:/bin/bash

-G,--groups GROUP1[,GROUP2,...[,GROUPN]]:添加附加組,可同時添加多個;

[root@magedu ~]# useradd --groups oneday,testbash,tom,centos liuyue

[root@magedu ~]# cat  /etc/group

centos:x:4004:liuyue

user1:x:4005:

jack:x:5003:tom

tom:x:5004:liuyue

bash:x:5005:

testbash:x:5006:liuyue

basher:x:5007:

nologin:x:5008:

gaohua:x:5009:

zhouhaicheng:x:5010:

shared:x:5011:

oneday:x:1345:liuyue

liuyue:x:5012:

-c,--comment COMMENT:添加用戶的註釋信息;

[root@magedu ~]# useradd -c "大管家" songhuizong

[root@magedu ~]# tail -1 /etc/passwd
songhuizong:x:5011:5013:大管家:/home/songhuizong:/bin/bash

-d,--home HOME_DIR--->指定家目錄;

[root@magedu ~]# useradd -d /tmp/袁騰飛 yuantengfei

[root@magedu ~]# tail -1 /etc/passwd

yuantengfei:x:5012:5014::/tmp/袁騰飛:/bin/bash

-M,不建立主目錄;

[root@magedu ~]# useradd -M weizheng

[root@magedu ~]# tail -1 /etc/passwd

weizheng:x:5013:5015::/home/weizheng:/bin/bash

[root@magedu ~]# cd /home/

[root@magedu home]# ll

total 0

drwx------   3 bash         bash          78 Mar 27 10:43 bash

drwx------   3 basher       basher        78 Mar 27 10:43 basher

drwx------   5 centos       centos       144 Mar 24 15:24 centos

drwx------   3 fedora       fedora        78 Mar 13 12:37 fedora

drwx------   3 gaohua       gaohua        78 Mar 29 10:11 gaohua

drwx------.  3         1000         1000  78 Apr 11  2018 guoxi

drwx------  15 lijianzhi    lijianzhi    316 Mar  5 15:31 lijianzhi

drwx------   3 liuyue       liuyue        78 Mar 30 22:09 liuyue

drwx------   3 nologin      nologin       78 Mar 27 10:44 nologin

drwx------   3 oneday       oneday        78 Mar 30 21:59 oneday

drwx------   3 shared       shared        78 Mar 30 21:54 shared

drwx------   3 slackware    slackware     78 Mar  5 11:50 slackware

drwx------   3 songhuizong  songhuizong   78 Mar 31 09:40 songhuizong

drwx------   3 testbash     testbash      78 Mar 27 10:43 testbash

drwx------   3 twoday       oneday        78 Mar 30 22:04 twoday

drwx------   3 user1        user1         78 Mar 26 12:01 user1

drwx------   3 user3        user3         78 Mar 22 17:18 user3

drwx------   3 zhouhaicheng zhouhaicheng  78 Mar 29 10:11 zhouhaicheng

[root@magedu home]#

-D :查看默認建立用戶的參數,也可修改默認參數;默認配置文件----/etc/login.defs,

[root@magedu home]# useradd -D

GROUP=100

HOME=/home

INACTIVE=-1

EXPIRE=

SHELL=/bin/bash

SKEL=/etc/skel

CREATE_MAIL_SPOOL=yes

例如:默認shell 就改成 csh

[root@magedu ~]# useradd -D -s /bin/csh

[root@magedu ~]# useradd -D

GROUP=100

HOME=/home

INACTIVE=-1

EXPIRE=

SHELL=/bin/csh

SKEL=/etc/skel

CREATE_MAIL_SPOOL=yes
二、修改用戶命令:usermod

NAME

usermod

​ - modify a user account,修改用戶帳戶

SYNOPSIS

​ -usermod [options] LOGIN

OPTIONS

-u,--uid UID:修改用戶的UID ;

[root@magedu home]# grep oneday /etc/passwd

oneday:x:1345:1345::/home/oneday:/bin/bash

[root@magedu home]# usermod -u 2345 oneday

[root@magedu home]# grep oneday /etc/passwd

oneday:x:2345:1345::/home/oneday:/bin/bash

-g,--gid GID:修改基本組ID,必須事先建立;

[root@magedu home]# grep yuantengfei /etc/passwd

yuantengfei:x:5012:5012::/tmp/袁騰飛:/bin/bash

[root@magedu home]# usermod -g 5015 yuantengfei

[root@magedu home]# grep yuantengfei /etc/passwd

yuantengfei:x:5012:5015::/tmp/袁騰飛:/bin/bash

[root@magedu home]#

-G:修改用戶附加組,原先附加組會被覆蓋;

-a;和-G一塊兒使做,只追加附加組,原先的附加組不會被覆蓋;

[root@magedu home]# grep liuyue /etc/group

centos:x:4004:liuyue

tom:x:5004:liuyue

testbash:x:5006:liuyue

oneday:x:1345:liuyue

liuyue:x:5012:

[root@magedu home]# usermod -aG oneday tom

[root@magedu home]# grep liuyue /etc/group

centos:x:4004:liuyue

tom:x:5004:liuyue

testbash:x:5006:liuyue

oneday:x:1345:liuyue,tom

liuyue:x:5012:

-c :修改用戶註釋信息;

-d:修改用戶家目錄;

-l:修改用戶名;

-s:修改用戶默認shell;

[root@magedu home]# grep "songhuizong" /etc/passwd

songhuizong:x:5011:5013:大管家:/home/songhuizong:/bin/bash

[root@magedu home]# usermod -c 大奸臣 -d /home/caijing -l caijing -s /sbin/nologin songhuizong

[root@magedu home]# grep "songhuizong" /etc/passwd

[root@magedu home]# grep "caijing" /etc/passwd

caijing:x:5011:5013:大奸臣:/home/caijing:/sbin/nologin

-r:建立系統用戶

[root@magedu home]# useradd -r suse

[root@magedu home]# tail -1 /etc/passwd

suse:x:988:982::/home/suse:/bin/bash
三、刪除用戶命令:userdel

userdel

[root@magedu home]# userdel caijing

-r:刪除用戶時一併刪除其家目錄

[root@magedu home]# cd /home/

[root@magedu home]# ll

total 0

drwx------   3 bash         bash          78 Mar 27 10:43 bash

drwx------   3 basher       basher        78 Mar 27 10:43 basher

drwx------   5 centos       centos       144 Mar 24 15:24 centos

drwx------   3 fedora       fedora        78 Mar 13 12:37 fedora

drwx------   3 gaohua       gaohua        78 Mar 29 10:11 gaohua

drwx------.  3         1000         1000  78 Apr 11  2018 guoxi

drwx------  15 lijianzhi    lijianzhi    316 Mar  5 15:31 lijianzhi

drwx------   3 liuyue       liuyue        78 Mar 30 22:09 liuyue

drwx------   3 nologin      nologin       78 Mar 27 10:44 nologin

drwx------   3 oneday       oneday        78 Mar 30 21:59 oneday

drwx------   3 shared       shared        78 Mar 30 21:54 shared

drwx------   3 slackware    slackware     78 Mar  5 11:50 slackware

drwx------   3         5011 songhuizong   78 Mar 31 09:40 songhuizong

drwx------   3 testbash     testbash      78 Mar 27 10:43 testbash

drwx------   3 twoday       oneday        78 Mar 30 22:04 twoday

drwx------   3 user1        user1         78 Mar 26 12:01 user1

drwx------   3 user3        user3         78 Mar 22 17:18 user3

drwx------   3 zhouhaicheng zhouhaicheng  78 Mar 29 10:11 zhouhaicheng

[root@magedu home]# userdel -r twoday

[root@magedu home]# ll

total 0

drwx------   3 bash         bash          78 Mar 27 10:43 bash

drwx------   3 basher       basher        78 Mar 27 10:43 basher

drwx------   5 centos       centos       144 Mar 24 15:24 centos

drwx------   3 fedora       fedora        78 Mar 13 12:37 fedora

drwx------   3 gaohua       gaohua        78 Mar 29 10:11 gaohua

drwx------.  3         1000         1000  78 Apr 11  2018 guoxi

drwx------  15 lijianzhi    lijianzhi    316 Mar  5 15:31 lijianzhi

drwx------   3 liuyue       liuyue        78 Mar 30 22:09 liuyue

drwx------   3 nologin      nologin       78 Mar 27 10:44 nologin

drwx------   3 oneday       oneday        78 Mar 30 21:59 oneday

drwx------   3 shared       shared        78 Mar 30 21:54 shared

drwx------   3 slackware    slackware     78 Mar  5 11:50 slackware

drwx------   3         5011 songhuizong   78 Mar 31 09:40 songhuizong

drwx------   3 testbash     testbash      78 Mar 27 10:43 testbash

drwx------   3 user1        user1         78 Mar 26 12:01 user1

drwx------   3 user3        user3         78 Mar 22 17:18 user3

drwx------   3 zhouhaicheng zhouhaicheng  78 Mar 29 10:11 zhouhaicheng

[root@magedu home]#
四、修改用用戶密碼:passwd

NAME

passwd

SYNOPSIS

passwd [--stdin] [USERNAME]

修改當前用戶名密碼

passwd:

[root@magedu home]# passwd

Changing password for user root.

New password:

Retype new password:

passwd: all authentication tokens updated successfully.

[root@magedu home]#

修改其餘用戶的密碼,只有root用戶才能修改其餘用戶密碼

passwd USERNAME

[root@magedu home]# passwd oneday

Changing password for user oneday.

New password:

BAD PASSWORD: The password is shorter than 8 characters

Retype new password:

passwd: all authentication tokens updated successfully.

指定一個字符串輸入給用戶當密碼

echo "PASSWORD" | passwd --stdin USERNAME

[root@magedu home]# echo "gentoo" | passwd --stdin gentoo

Changing password for user gentoo.

passwd: all authentication tokens updated successfully.
五、添加用戶組命令:groupadd

NAME

groupadd

SYNOPSIS

groupadd [option] GROUP_NAME

OPTIONS

-g GID :指定組ID;

[root@magedu home]# groupadd -g 2510 tongguan

[root@magedu home]#
六、刪除組命令:groupdel

groupdel

[root@magedu home]# groupdel tongguan

[root@magedu home]#
七、修改組命令:groupmod

groupmod

-g:修改組ID;

[root@magedu home]# grep "weizheng" /etc/group

weizheng:x:5015:

[root@magedu home]# groupmod -g 1555 weizheng

[root@magedu home]# grep "weizheng" /etc/group

weizheng:x:1555:

[root@magedu home]#

-n:修改組名

[root@magedu home]# grep "1555" /etc/group

weizheng:x:1555:

[root@magedu home]# groupmod -n caochao weizheng

[root@magedu home]# grep "1555" /etc/group

caochao:x:1555:
八、顯示用戶ID命令:id

NAME

id

SYNOPSIS

id [OPTION] ... [USERNAME]

-u:只顯示UID

-g:只顯示GID

-G:顯示全部GID

[root@magedu home]# id -u weizheng

5013

[root@magedu home]# id -g weizheng

1555

[root@magedu home]# id -G liuyue

5012 4004 5004 5006 1345
九、切換用戶命令

su

su USERNAME :非登陸式切換,不會切換目標用戶的配置文件進行初始化;

[root@magedu home]# su liuyue

您好,請你天天加油學習,歡迎入坑

[liuyue@magedu home]$

su - USERNAME:登陸式切換,會從新讀取用戶的匹配文件初始化登錄信息;

[root@magedu home]# su - liuyue

Last login: Sun Mar 31 16:02:28 CST 2019 on pts/0

您好,請你天天加油學習,歡迎入坑

[liuyue@magedu ~]$
十、修改權限命令:chmod

NAME

chmod

SYNOPSIS

chmod [OPTION] MODE FILE

chmod --reference=FILE

OPTIONS

遞歸修改權限

-R,--recursive

[root@magedu tmp]# ll man

total 124

drwxr-xr-x 2 root root  53248 Mar  8 14:37 man1

drwxr-xr-x 2 root root 192512 Mar  8 14:37 man3

drwxr-xr-x 2 root root  16384 Mar  8 14:37 man5

drwxr-xr-x 2 root root    173 Mar  8 14:37 man7

drwxr-xr-x 2 root root  32768 Mar  8 14:37 man8

[root@magedu tmp]# chmod -R 774 man

[root@magedu tmp]# ll man

total 124

drwxrwxr-- 2 root root  53248 Mar  8 14:37 man1

drwxrwxr-- 2 root root 192512 Mar  8 14:37 man3

drwxrwxr-- 2 root root  16384 Mar  8 14:37 man5

drwxrwxr-- 2 root root    173 Mar  8 14:37 man7

drwxrwxr-- 2 root root  32768 Mar  8 14:37 man8

--reference=FILE

[root@magedu tmp]# ll man

total 124

drwxrwxr-- 2 root root  53248 Mar  8 14:37 man1

drwxrwxr-- 2 root root 192512 Mar  8 14:37 man3

drwxrwxr-- 2 root root  16384 Mar  8 14:37 man5

drwxrwxr-- 2 root root    173 Mar  8 14:37 man7

drwxrwxr-- 2 root root  32768 Mar  8 14:37 man8

[root@magedu tmp]# chmod -R --reference=/etc/fstab man

[root@magedu tmp]# ll man

total 124

drw-r--r-- 2 root root  53248 Mar  8 14:37 man1

drw-r--r-- 2 root root 192512 Mar  8 14:37 man3

drw-r--r-- 2 root root  16384 Mar  8 14:37 man5

drw-r--r-- 2 root root    173 Mar  8 14:37 man7

drw-r--r-- 2 root root  32768 Mar  8 14:37 man8
十一、修改文件的用戶和組:chown

chown

NAME

​ chown - change file owner and group

SYNOPSIS

​ chown [OPTION]... [OWNER][:[GROUP]] FILE...

​ chown [OPTION]... --reference=RFILE FILE...

OPTIONS

-R --recursive:遞歸修改用戶或組

--reference=RFILE:以某個文件爲基準,修改用戶和組;

[root@magedu tmp]# ll man

total 124

drw-r--r-- 2 root root  53248 Mar  8 14:37 man1

drw-r--r-- 2 root root 192512 Mar  8 14:37 man3

drw-r--r-- 2 root root  16384 Mar  8 14:37 man5

drw-r--r-- 2 root root    173 Mar  8 14:37 man7

drw-r--r-- 2 root root  32768 Mar  8 14:37 man8

[root@magedu tmp]# man chown

[root@magedu tmp]# chown -R liuyue.liuyue /tmp/man

[root@magedu tmp]# ll man

total 124

drw-r--r-- 2 liuyue liuyue  53248 Mar  8 14:37 man1

drw-r--r-- 2 liuyue liuyue 192512 Mar  8 14:37 man3

drw-r--r-- 2 liuyue liuyue  16384 Mar  8 14:37 man5

drw-r--r-- 2 liuyue liuyue    173 Mar  8 14:37 man7

drw-r--r-- 2 liuyue liuyue  32768 Mar  8 14:37 man8

[root@magedu tmp]# ll man

total 124

drw-r--r-- 2 liuyue liuyue  53248 Mar  8 14:37 man1

drw-r--r-- 2 liuyue liuyue 192512 Mar  8 14:37 man3

drw-r--r-- 2 liuyue liuyue  16384 Mar  8 14:37 man5

drw-r--r-- 2 liuyue liuyue    173 Mar  8 14:37 man7

drw-r--r-- 2 liuyue liuyue  32768 Mar  8 14:37 man8

[root@magedu tmp]# chown -R --reference=/etc/passwd  /tmp/man

[root@magedu tmp]# ll man

total 124

drw-r--r-- 2 root root  53248 Mar  8 14:37 man1

drw-r--r-- 2 root root 192512 Mar  8 14:37 man3

drw-r--r-- 2 root root  16384 Mar  8 14:37 man5

drw-r--r-- 2 root root    173 Mar  8 14:37 man7

drw-r--r-- 2 root root  32768 Mar  8 14:37 man8
相關文章
相關標籤/搜索