`

linux 培训笔记

 
阅读更多
何世晓
server raid lsi 内存大于等于1G,带锂电池的。
硬盘:标配2~3块硬盘,300G
SAS硬盘。==》服务器初始化工作(创建服务器硬盘的RAID级别)

操作系统的选择:
redhat,  rhel6.3   x86_64    (32位的rhel6,只支持16G的内存)
centos,
novell,netware,suse+ibm
debain,ubuntu

安装Linux操作系统:
Linux系统的分区:
/boot分区单独划分,大小为200M.
其它分区,采用LVM逻辑卷划分。

分区最底标准:swap(物理内存的1~2倍, <=32G),根分区。
生产环境的分区原则:
1、/boot分区独立划分。
2、系统分区,采用LVM划分独立的卷组。 rootvg
3、数据分区,采用LVM划分独立的卷组。 datavg

4、操作系统和应用系统分离,数据库系统和数据分离。


linux系统是一个单根的操作系统,/root目录为root用户的自家目录或个人目录。
linux系统的命令语法:
<command> [options]  parm1 parm2.
rm -r -f /root/*.c  /tmp/*    (选项可以合并)
rm -rf /root/*.c  /tmp/*      (选项和参数的位置可以互换)。

Linux系统的命令介绍。
一、关于目录管理的命令。
ls  显示目录的文件或目录名。
   -l 显示详细列表
   -a 显示隐藏文件
   -h 人性化显示
   -i 显示目录的i节点。
   -d 显示目录,非目录下的内容。

dir 与ls命令一样。
cd  目录切换, cd ~ ;cd ;cd -
pwd 显示当前目录的绝对路径。
clean  清屏(ctrl+(小L))
mkdir  创建目录, mkdir -m 777
dir1(创建目录时设置权限,创建目录时默认权限为777-

umask值); mkdir -p a/b/c/d (创建多级目录)
rmdir  删除空目录(已不使用)
rm     删除目录和文件。注意删除后很难恢复。rm -rf dir;rm file.txt;
       -r 递归删除,在linux中-r或-R都具有递归的意思。
       -f 不提示用户,直接删除。
       -i 提示用户,是否删除。
touch  创建空文件,修改文件的时间戳。

[root@test ~]# ls -l
总用量 44
-rw-------. 1 root root  1335 10月 27 18:26 anaconda-ks.cfg
-rw-r--r--. 1 root root     0 10月 27 18:48 file.txt
-rw-r--r--. 1 root root 28037 10月 27 18:48 install.log
-rw-r--r--. 1 root root  7690 10月 27 18:24 install.log.syslog
第一列:文件的类型和权限
  - rw- r-- r--
  第1栏:表示文件的类型,
       -:普通文件
       d:dir目录
       b:block块设备文件
       c:char字符设备文件
       s:socket文件
       p:pipo管道文件
       l:link符号链接文件,快捷方式
  第2栏:后面连续的3位:表示用户的权限,rwx(读,写,执行)
  第3栏:表示工作组的权限
  第4栏:表示其它用户的权限

[root@test ~]# ll /bin/ping
-rwsr-xr-x. 1 root root 40760 3月  22 2011 /bin/ping
[root@test ~]# find /bin -perm +2000 -exec ls -l {} \;
-rwxr-sr-x. 1 root cgred 8424 2月  23 2012 /bin/cgexec
[root@test ~]# ls -ld /tmp/ /usr/tmp/
drwxrwxrwt. 3 root root 4096 10月 27 18:27 /tmp/
drwxrwxrwt. 2 root root 4096 10月 27 18:26 /usr/tmp/
[root@test ~]#

-rwsr-xr-x  ==> uid:rws===>suid
-rwxr-sr-x  ==> gid:r-s===>sgid
drwxrwxrwt  ==> other:rwt==>t

suid:普通用户在执行具有suid权限的文件时,是以属有者的身份执行。
sgid:任何用户在sgid权限的目录下创建文件或目录时,会自动的继承父目录的工作组。
t:在t位的目录下,普通用户无法删除其它用户的文件。

[root@test ~]# mkdir dir1
[root@test ~]# ll dir1/ -d
drwxr-xr-x. 2 root root 4096 10月 27 19:07 dir1/
[root@test ~]# chown root.bin dir1
[root@test ~]# ll dir1/ -d
drwxr-xr-x. 2 root bin 4096 10月 27 19:07 dir1/
[root@test ~]# chmod g+s dir1/
[root@test ~]# ls -l dir1/ -d
drwxr-sr-x. 2 root bin 4096 10月 27 19:07 dir1/
[root@test ~]# cd dir1/
[root@test dir1]# touch file1
[root@test dir1]# mkdir dir2
[root@test dir1]# ll
总用量 4
drwxr-sr-x. 2 root bin 4096 10月 27 19:07 dir2
-rw-r--r--. 1 root bin    0 10月 27 19:07 file1
[root@test dir1]# cd ..
[root@test ~]# chmod g-s dir1/
[root@test ~]# ll -d dir1/
drwxr-xr-x. 3 root bin 4096 10月 27 19:07 dir1/
[root@test ~]# cd dir1/
[root@test dir1]# touch file2
[root@test dir1]# mkdir dir3
[root@test dir1]# ll
总用量 8
drwxr-sr-x. 2 root bin  4096 10月 27 19:07 dir2
drwxr-xr-x. 2 root root 4096 10月 27 19:08 dir3
-rw-r--r--. 1 root bin     0 10月 27 19:07 file1
-rw-r--r--. 1 root root    0 10月 27 19:08 file2


[root@test ~]# useradd a
[root@test ~]# useradd b
[root@test ~]# ll /tmp/ -d
drwxrwxrwt. 3 root root 4096 10月 27 18:27 /tmp/
[root@test ~]# su - a
[a@test ~]$ whoami
a
[a@test ~]$ cd /tmp/
[a@test tmp]$ touch a.file
[a@test tmp]$ chmod 777 a.file
[a@test tmp]$ ll a.file
-rwxrwxrwx. 1 a a 0 10月 27 19:05 a.file
[a@test tmp]$ exit
logout
[root@test ~]# su - b
[b@test ~]$ whoami
b
[b@test ~]$ cd /tmp/
[b@test tmp]$ ll a.file
-rwxrwxrwx. 1 a a 0 10月 27 19:05 a.file
[b@test tmp]$ rm a.file
rm: 无法删除"a.file": 不允许的操作
[b@test tmp]$ echo "adsfasdfafd " >a.file
[b@test tmp]$ ls
a.file  yum.log
[b@test tmp]$ ll a.file
-rwxrwxrwx. 1 a a 13 10月 27 19:06 a.file
[b@test tmp]$


第二列:文件的硬链接
第三列:用户,属有者
第四列:工作组
第五列:文件的大小(b)
第六列:修改时间(最后的时间)
第七列:文件名,目录名称

[root@test ~]# ls
anaconda-ks.cfg  file.txt  install.log  install.log.syslog
[root@test ~]# ls /bin/ping
/bin/ping
[root@test ~]# ls /dev/sda*
/dev/sda  /dev/sda1  /dev/sda2
[root@test ~]# ls /bin/vi*
/bin/vi  /bin/view
[root@test ~]# cat /etc/DIR_COLORS  (修改ls命令显示的颜色)
[root@test ~]#

[root@test ~]# ls -lh
总用量 48K
-rw-------. 1 root root 1.4K 10月 27 18:26 anaconda-ks.cfg
drwxr-xr-x. 4 root bin  4.0K 10月 27 19:08 dir1
-rw-r--r--. 1 root root    0 10月 27 18:48 file.txt
-rw-r--r--. 1 root root  28K 10月 27 18:48 install.log
-rw-r--r--. 1 root root 7.6K 10月 27 18:24 install.log.syslog
[root@test ~]# ls -l
总用量 48
-rw-------. 1 root root  1335 10月 27 18:26 anaconda-ks.cfg
drwxr-xr-x. 4 root bin   4096 10月 27 19:08 dir1
-rw-r--r--. 1 root root     0 10月 27 18:48 file.txt
-rw-r--r--. 1 root root 28037 10月 27 18:48 install.log
-rw-r--r--. 1 root root  7690 10月 27 18:24 install.log.syslog
[root@test ~]# ls -a
.   anaconda-ks.cfg  .bash_profile  .cshrc  file.txt     install.log.syslog 

.viminfo
..  .bash_logout     .bashrc        dir1    install.log  .tcshrc
[root@test ~]# clear
[root@test ~]# ls
anaconda-ks.cfg  dir1  file.txt  install.log  install.log.syslog
[root@test ~]# ls -l dir1/
总用量 8
drwxr-sr-x. 2 root bin  4096 10月 27 19:07 dir2
drwxr-xr-x. 2 root root 4096 10月 27 19:08 dir3
-rw-r--r--. 1 root bin     0 10月 27 19:07 file1
-rw-r--r--. 1 root root    0 10月 27 19:08 file2
[root@test ~]# ls -ld dir1/
drwxr-xr-x. 4 root bin 4096 10月 27 19:08 dir1/
[root@test ~]# ls -li
总用量 48
270434 -rw-------. 1 root root  1335 10月 27 18:26 anaconda-ks.cfg
270437 drwxr-xr-x. 4 root bin   4096 10月 27 19:08 dir1
270435 -rw-r--r--. 1 root root     0 10月 27 18:48 file.txt
261123 -rw-r--r--. 1 root root 28037 10月 27 18:48 install.log
261124 -rw-r--r--. 1 root root  7690 10月 27 18:24 install.log.syslog
[root@test ~]#




[root@test /]# cd /usr/share/doc/binutils-2.20.51.0.2/
[root@test binutils-2.20.51.0.2]# cd /var/log/
anaconda.ifcfg.log    boot.log              maillog               secure
anaconda.log          btmp                  mcelog                spooler
anaconda.program.log  ConsoleKit/           messages              sssd/
anaconda.storage.log  cron                  ntpstats/             tallylog
anaconda.syslog       cups/                 prelink/              wtmp
anaconda.xlog         dmesg                 rhsm/                 yum.log
anaconda.yum.log      dracut.log            sa/
audit/                lastlog               samba/
[root@test binutils-2.20.51.0.2]# cd /var/log/samba/
[root@test samba]# ls
old
[root@test samba]# cd /usr/share/doc/binutils-2.20.51.0.2/
[root@test binutils-2.20.51.0.2]# cd -
/var/log/samba
[root@test samba]# cd -
/usr/share/doc/binutils-2.20.51.0.2
[root@test binutils-2.20.51.0.2]# pwd
/usr/share/doc/binutils-2.20.51.0.2
[root@test binutils-2.20.51.0.2]#

[root@test ~]# mkdir dirname1
[root@test ~]# ls -dl dirname1
drwxr-xr-x. 2 root root 4096 10月 27 18:41 dirname1
[root@test ~]# umask
0022
[root@test ~]# dir=777-umask=777-022=755^C
[root@test ~]# mkdir -m 777 dirname2
[root@test ~]# ls -ld dirname*
drwxr-xr-x. 2 root root 4096 10月 27 18:41 dirname1
drwxrwxrwx. 2 root root 4096 10月 27 18:41 dirname2
[root@test ~]# mkdir -p dirname3/a/b/c/d/
[root@test ~]# ls -Rl dirname3
dirname3:
总用量 4
drwxr-xr-x. 3 root root 4096 10月 27 18:42 a

dirname3/a:
总用量 4
drwxr-xr-x. 3 root root 4096 10月 27 18:42 b

dirname3/a/b:
总用量 4
drwxr-xr-x. 3 root root 4096 10月 27 18:42 c

dirname3/a/b/c:
总用量 4
drwxr-xr-x. 2 root root 4096 10月 27 18:42 d

dirname3/a/b/c/d:
总用量 0
[root@test ~]#
[root@test ~]# ls
anaconda-ks.cfg  dirname1  dirname2  dirname3  install.log  install.log.syslog
[root@test ~]# rm dirname1 -rf
[root@test ~]# ls
anaconda-ks.cfg  dirname2  dirname3  install.log  install.log.syslog
[root@test ~]# rm dirname* -rf
[root@test ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog
[root@test ~]# ll install.log
-rw-r--r--. 1 root root 28037 10月 27 18:26 install.log
[root@test ~]# touch file.txt
[root@test ~]# ll file.txt
-rw-r--r--. 1 root root 0 10月 27 18:48 file.txt
[root@test ~]# date
2014年 10月 27日 星期一 18:48:10 CST
[root@test ~]# touch install.log
[root@test ~]# ll install.log
-rw-r--r--. 1 root root 28037 10月 27 18:48 install.log
[root@test ~]#



二、权限和属有者的设置。
chmod 设置文件和目录的权限
chown 设置文件和目录的属有者和工作组

chmod语法
方法一:数字表达:
chmod -R 7777 filename|dirname
第1个7:表示高级权限,suid,sgid,t
第2个7:user:rwx
第3个7:group:rwx
第4个7:other:rwx

方法二:文字表达
chmod -R [ugoa][+-=][rwxst] filename|dirname

drwxr-xr-x.==> rwx r-x r-x 111 101 101 ==>755
chmod -R u=rwx,g=rx,o=rx filename   chmod -R 755 filename
chmod -R u=rwx,og=rx filename
chmod -R 755 filename

-rwsr-xr-x. 1 root root 40760 3月  22 2011 /bin/ping
suid - -   100
user:rwx   111
group:r-x  101
other:r-x  101

-rwsr-xr-x  ==>  4755

[root@test ~]# ll /bin/cgexec
-rwxr-sr-x. 1 root cgred 12576 2月  23 2012 /bin/cgexec
- sgid -   010
user:rwx   111
group:r-x  101
other:r-x  101

-rwxr-sr-x ==> 2755
drwxrwxrwt.==> 1777

chown语法:
chown -R [username][:.][groupname] filename|dirname

-rw-------. 1 root root  1335 10月 27 18:26 anaconda-ks.cfg (将属有者改为bin)
chown bin anaconda*


[root@test ~]# ll anaconda-ks.cfg
-rw-------. 1 root root 1335 10月 27 18:26 anaconda-ks.cfg
[root@test ~]# useradd he.shixiao
[root@test ~]# chown :he.shixiao anaconda-ks.cfg
[root@test ~]# ll anaconda-ks.cfg
-rw-------. 1 root he.shixiao 1335 10月 27 18:26 anaconda-ks.cfg
[root@test ~]# chown .bin anaconda-ks.cfg
[root@test ~]# ll anaconda-ks.cfg
-rw-------. 1 root bin 1335 10月 27 18:26 anaconda-ks.cfg
[root@test ~]# chown bin anaconda-ks.cfg
[root@test ~]# ls -l anaconda-ks.cfg
-rw-------. 1 bin bin 1335 10月 27 18:26 anaconda-ks.cfg
[root@test ~]#


三、文件内容的显示命令。
cat 显示小文件的内容。  tac
  -n 显示行号
  -E 显示回车符。$ (中英文的回车符)

head 显示文件的头10行

tail 显示文件的末10行
  -f 动态的显示某个文件的变化。一般用于故障排除。

more 分屏显示大文件
   空格:向文件末尾翻页
   b:向文件头翻页

less 分屏显示大文件
   N,n 继续查找。
   /abc 查找abc字符。

[root@test ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@test ~]# cat -n /etc/hosts
     1  127.0.0.1   localhost localhost.localdomain localhost4
localhost4.localdomain4
     2  ::1         localhost localhost.localdomain localhost6
localhost6.localdomain6
[root@test ~]# cat -n /etc/hosts|tac
     2  ::1         localhost localhost.localdomain localhost6
localhost6.localdomain6
     1  127.0.0.1   localhost localhost.localdomain localhost4
localhost4.localdomain4
[root@test ~]#
[root@test ~]# tac /etc/hosts
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
[root@test ~]# cat -E /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4
localhost4.localdomain4$
::1         localhost localhost.localdomain localhost6
localhost6.localdomain6$

[root@test ~]# cat -n /root/install.log|head
     1  安装 libgcc-4.4.6-4.el6.x86_64
     2  warning: libgcc-4.4.6-4.el6.x86_64: Header V3 RSA/SHA256 Signature,
key
     3  安装 setup-2.8.14-16.el6.noarch
     4  安装 ca-certificates-2010.63-3.el6_1.5.noarch
     5  安装 tzdata-2012c-1.el6.noarch
     6  安装 filesystem-2.4.30-3.el6.x86_64
     7  安装 foomatic-db-filesystem-4.0-7.20091126.el6.noarch
     8  安装 xml-common-0.6.3-32.el6.noarch
     9  安装 iso-codes-3.16-2.el6.noarch
    10  安装 fontpackages-filesystem-1.41-1.1.el6.noarch
[root@test ~]# cat -n /root/install.log|head -2
     1  安装 libgcc-4.4.6-4.el6.x86_64
     2  warning: libgcc-4.4.6-4.el6.x86_64: Header V3 RSA/SHA256 Signature,
key ID fd431d51: NOKEY
[root@test ~]# cat -n /root/install.log|tail -2
   716  安装 redhat-indexhtml-6-1.el6.noarch
   717  *** FINISHED INSTALLING PACKAGES ***[root@test ~]#


[root@test ~]# tail -f /var/log/messages  (ctrl+c 中止)


四、文件和目录处理命令。
cp 复制文件和目录  
  cp /root/install.log  /tmp
  cp -rap /var/log  /root

  -r 复制目录
  -a 复制隐藏文件。
  -p 复制时,权限不发生变化

mv 重命名,移动文件或目录
  mv /root/install.log /root/install.log.bak

ln 创建快捷方式。
  1\软链接:快捷方式。方便用户使用,占用硬盘的i节点,可以跨分区操作,可以对文件和目录操作。
  -s 创建软链接
  -f 强制创建
  2\硬链接:防止文件被误删。不占用硬盘空间,不能跨分区操作,只能对文件操作。

软链接的实验:
[root@test ~]# ln -sf /var/log/ .
[root@test ~]# ll
总用量 48
-rw-------. 1 bin  bin   1335 10月 27 18:26 anaconda-ks.cfg
drwxr-xr-x. 4 root bin   4096 10月 27 19:08 dir1
-rw-r--r--. 1 root root     0 10月 27 18:48 file.txt
-rw-r--r--. 1 root root 28037 10月 27 18:48 install.log.bak
-rw-r--r--. 1 root root  7690 10月 27 18:24 install.log.syslog
lrwxrwxrwx. 1 root root     9 10月 27 20:29 log -> /var/log/
[root@test ~]# ln -sf /usr/share/doc/  word
[root@test ~]# ll
总用量 48
-rw-------. 1 bin  bin   1335 10月 27 18:26 anaconda-ks.cfg
drwxr-xr-x. 4 root bin   4096 10月 27 19:08 dir1
-rw-r--r--. 1 root root     0 10月 27 18:48 file.txt
-rw-r--r--. 1 root root 28037 10月 27 18:48 install.log.bak
-rw-r--r--. 1 root root  7690 10月 27 18:24 install.log.syslog
lrwxrwxrwx. 1 root root     9 10月 27 20:29 log -> /var/log/
lrwxrwxrwx. 1 root root    15 10月 27 20:29 word -> /usr/share/doc/
[root@test ~]#


硬链接的实验:
[root@test ~]# df -hP
文件系统              容量  已用  可用 已用%% 挂载点
/dev/mapper/rootvg-LogVol01   18G  2.2G   15G  13% /
tmpfs                 495M     0  495M   0% /dev/shm
/dev/sda1             194M   28M  157M  15% /boot
/dev/sr0              3.5G  3.5G     0 100% /media
[root@test ~]# ln /boot/config-2.6.32-279.el6.x86_64 config
ln: 创建硬链接"config" => "/boot/config-2.6.32-279.el6.x86_64":
无效的跨设备连接
[root@test ~]# ls
anaconda-ks.cfg  file.txt         install.log.syslog  log
dir1             install.log.bak  kk                  word
[root@test ~]# ln install.log.bak test
[root@test ~]# ll install.log.bak test -i
261123 -rw-r--r--. 2 root root 28037 10月 27 18:48 install.log.bak
261123 -rw-r--r--. 2 root root 28037 10月 27 18:48 test
[root@test ~]#
[root@test ~]#
[root@test ~]#
[root@test ~]# chmod 777 test
[root@test ~]# ll install.log.bak test -i
261123 -rwxrwxrwx. 2 root root 28037 10月 27 18:48 install.log.bak
261123 -rwxrwxrwx. 2 root root 28037 10月 27 18:48 test
[root@test ~]# echo "" >> install.log.bak
[root@test ~]# ll install.log.bak test -i
261123 -rwxrwxrwx. 2 root root 28038 10月 27 20:34 install.log.bak
261123 -rwxrwxrwx. 2 root root 28038 10月 27 20:34 test
[root@test ~]# rm install.log.bak -rf
[root@test ~]# ll install.log.bak test -i
ls: 无法访问install.log.bak: 没有那个文件或目录
261123 -rwxrwxrwx. 1 root root 28038 10月 27 20:34 test
[root@test ~]# mv test install.log.bak
[root@test ~]# ls -l install.log.bak
-rwxrwxrwx. 1 root root 28038 10月 27 20:34 install.log.bak
[root@test ~]#


五、文件查找的命令:
find    功能最强的查找。可以通过各种信息查找。
which 在$PATH变量的目录中查找相关的命令。
whereis 在$PATH变量的目录和/usr/share/man目录中查找相关的命令
locate  快速查找。在update.db数据文件中查找。快速最快,实效性较差。

[root@test ~]# which zip
/usr/bin/zip
[root@test ~]# which ls
alias ls='ls --color=auto'
        /bin/ls
[root@test ~]# which jake
/usr/bin/which: no jake in
(/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
[root@test ~]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@test ~]#



[root@test ~]# whereis zip
zip: /usr/bin/zip /usr/share/man/man1/zip.1.gz
[root@test ~]# whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[root@test ~]# whereis jake
jake:



[root@test ~]# updatedb  创建数据库引索
[root@test ~]# locate /etc/hosts
/etc/hosts
/etc/hosts.allow
/etc/hosts.deny


语法:
find <路径> -options var1

实例:
-rwxrwxrwx. 1 root root 28038 10月 27 20:34 install.log.bak

通过文件名:-name *.bak
find . -name "*.bak"

通过时间:
-ctime [-+]n   创建时间  -2 近两天的  +2 2天前
-mtime [-+]n   修改时间
-atime [-+]n   访问时间

通过文件大小:
-size [-+]n[kmg]   查找空文件: find . -size 0
-empty             查找空文件和空目录:  find . -empty

通过用户和工作组:
-user <username>
-group <groupname>
-nouser
-nogroup

[root@test ~]# ll install.log.bak
-rwxrwxrwx. 1 root root 28038 10月 27 20:34 install.log.bak
[root@test ~]# chown he.shixiao:he.shixiao install.log.bak
[root@test ~]# ll install.log.bak
-rwxrwxrwx. 1 he.shixiao he.shixiao 28038 10月 27 20:34 install.log.bak
[root@test ~]# find . -user he.shixiao
./install.log.bak
[root@test ~]# find . -group he.shixiao
./install.log.bak
[root@test ~]# cat /etc/passwd|grep he.shixiao
he.shixiao:x:502:502::/home/he.shixiao:/bin/bash
[root@test ~]# userdel -r he.shixiao
[root@test ~]# ll install.log.bak
-rwxrwxrwx. 1 502 502 28038 10月 27 20:34 install.log.bak
[root@test ~]# find . -nouser
./install.log.bak
[root@test ~]# find . -nogroup
./install.log.bak


通过文件的权限:-perm [-+]1000
查找包含有suid权限的文件。 find . -perm +4000
查找包含有sgid权限的文件。 find . -perm +2000
t位   find . -perm +1000

[root@test ~]# find /bin/ -perm +2000 -exec ls -l {} \;
-rwxr-sr-x. 1 root cgred 12576 2月  23 2012 /bin/cgexec
[root@test ~]# find /bin/ -perm +4000 -exec ls -l {} \;
-rwsr-xr-x. 1 root root 36488 3月  22 2011 /bin/ping6
-rwsr-xr-x. 1 root root 76056 4月   6 2012 /bin/mount
-rwsr-xr-x. 1 root root 50496 4月   6 2012 /bin/umount
-rwsr-xr-x. 1 root root 34904 4月  17 2012 /bin/su
-rwsr-xr-x. 1 root root 40760 3月  22 2011 /bin/ping


[root@test ~]# find  . -perm 644 -exec ls -l {} \;
-rw-r--r--. 1 root root 176 9月  23 2004 ./.bashrc
-rw-r--r--. 1 root root 129 12月  4 2004 ./.tcshrc
-rw-r--r--. 1 root root 0 10月 27 18:48 ./file.txt
-rw-r--r--. 1 root root 7690 10月 27 18:24 ./install.log.syslog
-rw-r--r--. 1 root root 176 5月  20 2009 ./.bash_profile
-rw-r--r--. 1 root bin 0 10月 27 19:07 ./dir1/file1
-rw-r--r--. 1 root root 0 10月 27 19:08 ./dir1/file2
-rw-r--r--. 1 root root 18 5月  20 2009 ./.bash_logout
-rw-r--r--. 1 root root 100 9月  23 2004 ./.cshrc

通过文件类型查找:-type f|b|c|s|l|d|p

find /dev -type s
[root@test ~]# find /dev -type s -exec ls -l {} \;
srw-rw-rw-. 1 root root 0 10月 27 18:27 /dev/log

设备文件和普通文件的区别。
[root@test ~]# ls -l install.log.bak
-rwxrwxrwx. 1 502 502 28038 10月 27 20:34 install.log.bak
[root@test ~]#
[root@test ~]# ls -l /dev/sda*
brw-rw----. 1 root disk 8, 0 10月 27 18:27 /dev/sda
brw-rw----. 1 root disk 8, 1 10月 27 18:27 /dev/sda1
brw-rw----. 1 root disk 8, 2 10月 27 18:27 /dev/sda2

[root@test ~]# mknod /dev/test b 8 1
[root@test ~]# ll /dev/test /dev/sda1
brw-rw----. 1 root disk 8, 1 10月 27 18:27 /dev/sda1
brw-r--r--. 1 root root 8, 1 10月 27 21:00 /dev/test
[root@test ~]# mkdir /boot1
[root@test ~]# mount /dev/test /boot1
[root@test ~]# df -hP
文件系统              容量  已用  可用 已用%% 挂载点
/dev/mapper/rootvg-LogVol01   18G  2.2G   15G  13% /
tmpfs                 495M     0  495M   0% /dev/shm
/dev/sda1             194M   28M  157M  15% /boot
/dev/sr0              3.5G  3.5G     0 100% /media
/dev/test             194M   28M  157M  15% /boot1
[root@test ~]#


六、帮助类命令。
man
帮助。提供命令、函数、配置文件的帮助文档。man命令所调用的帮助文档都存放在/usr/share/man/man*
[root@test man]# man ls   (按q退出)
[root@test man]#
[root@test man]# man -a open
[root@test manma]# man vsftpd.conf

<command> --help

[root@test man]# ls --help
用法:ls [选项]... [文件]...
列出 FILE 的信息(默认为当前目录)。
如果不指定-cftuvSUX 或--sort 选项,则根据字母大小排序。

长选项必须使用的参数对于短选项时也是必需使用的。
  -a, --all                     不隐藏任何以. 开始的项目
  -A, --almost-all              列出除. 及.. 以外的任何项目
      --author                  与-l 同时使用时列出每个文件的作者
  -b, --escape                  以八进制溢出序列表示不可打印的字符
      --block-size=大小         块以指定大小的字节为单位
  -B, --ignore-backups          不列出任何以"~"字符结束的项目
  -c                            配合-lt:根据ctime 排序并显示ctime(文件
                                状态最后更改的时间)
                                配合-l:显示ctime 但根据名称排序
/usr/share/doc/*    所有软件的说明文档。

[root@test man]# cd /usr/share/doc/vsftpd-2.2.2/
[root@test vsftpd-2.2.2]# ls
AUDIT       Changelog  FAQ      README           SECURITY  TODO
BENCHMARKS  COPYING    INSTALL  README.security  SIZE      TUNING
BUGS        EXAMPLE    LICENSE  REWARD           SPEED     vsftpd.xinetd
[root@test vsftpd-2.2.2]# cd EXAMPLE/
[root@test EXAMPLE]# ls
INTERNET_SITE          PER_IP_CONFIG  VIRTUAL_HOSTS  VIRTUAL_USERS_2
INTERNET_SITE_NOINETD  README         VIRTUAL_USERS
[root@test EXAMPLE]#


七、关机和重启类
关机:
init 0
shutdown -h now
halt
poweroff

重启:
init 6
reboot
shutdown -r now

八、打包和压缩类命令。
打包,备份:-c 打包
tar -cf backup_etc.tar /etc/ 
tar -cf backup_log.tar /var/log

备份时压缩:-j 使用bzip2压缩,  -z 使用gzip压缩。
tar -jcf backup_etc.tar.bz2 /etc
tar -zcf backup_etc.tar.gz /etc

解包,恢复:-x 解包  -C 解包到指定目录。
tar -xjf backup_etc.tar.bz2 -C /opt



[root@test ~]# tar -zcf backup_etc.tar.gz /etc
tar: 从成员名中删除开头的“/”
[root@test ~]# ll -h backup_etc.tar.gz
-rw-r--r--. 1 root root 7.2M 10月 27 21:35 backup_etc.tar.gz
[root@test ~]# tar -tzf backup_etc.tar.gz |more
etc/
etc/avahi/
etc/avahi/hosts


[root@test ~]# tar -zxf backup_etc.tar.gz -C /opt/
[root@test ~]# cd /opt/
[root@test opt]# ls
etc  rh
[root@test opt]# cd etc/
[root@test etc]# ls



[root@test ~]# ll install.log.bak
-rwxrwxrwx. 1 502 502 28038 10月 27 20:34 install.log.bak
[root@test ~]# bzip2 install.log.bak
[root@test ~]# ll install.log.bak.bz2
-rwxrwxrwx. 1 502 502 6421 10月 27 20:34 install.log.bak.bz2
[root@test ~]# bzip2 -d install.log.bak.bz2
[root@test ~]# gzip install.log.bak
[root@test ~]# ll install.log.bak.gz
-rwxrwxrwx. 1 502 502 7490 10月 27 20:34 install.log.bak.gz
[root@test ~]#


九、磁盘管理类
du  目录大小统计命令。粗略的统计
df  显示磁盘使用情况。
fdisk 显示硬盘信息。
mount 挂载某个设备。

[root@test ~]# du -sm
8       .
[root@test ~]# du -sk
7364    .
[root@test ~]# du -sb
7505390 .

[root@test ~]# df -hP
文件系统              容量  已用  可用 已用%% 挂载点
/dev/mapper/rootvg-LogVol01   18G  2.2G   15G  14% /
tmpfs                 495M     0  495M   0% /dev/shm
/dev/sda1             194M   28M  157M  15% /boot
/dev/sr0              3.5G  3.5G     0 100% /media
/dev/test             194M   28M  157M  15% /boot1
[root@test ~]# df -hT -t ext4
文件系统    类型      容量  已用  可用 已用%% 挂载点
/dev/mapper/rootvg-LogVol01
              ext4     18G  2.2G   15G  14% /
/dev/sda1     ext4    194M   28M  157M  15% /boot
/dev/test     ext4    194M   28M  157M  15% /boot1
[root@test ~]# df -ih
文件系统              Inode  已用(I)  可用(I) 已用(I)%% 挂载点
/dev/mapper/rootvg-LogVol01
                        1.2M     82K    1.1M    8% /
tmpfs                   124K       1    124K    1% /dev/shm
/dev/sda1                50K      38     50K    1% /boot
/dev/sr0                   0       0       0    -  /media
/dev/test                50K      38     50K    1% /boot1


[root@test ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00008ff4

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        2611    20765696   8e  Linux LVM



mount命令语法:
mount [-t filesystem] [-o options] <device_name> <mount_point>

filesystem:
ext2/ext3/ext4
nfs/cifs
iso9660/udf
vfat/ntfs-3g

options:
ro
rw
sync
noexec
....


挂载光驱:
mount /dev/cdrom /media

mount /dev/sr0 /media


[root@test ~]# ll /dev/cdr*
lrwxrwxrwx. 1 root root 3 10月 27 19:25 /dev/cdrom -> sr0
lrwxrwxrwx. 1 root root 3 10月 27 19:25 /dev/cdrw -> sr0
[root@test ~]# mount /dev/sr0  /media/
mount: block device /dev/sr0 is write-protected, mounting read-only
mount: /dev/sr0 already mounted or /media/ busy
mount: according to mtab, /dev/sr0 is already mounted on /media
[root@test ~]# umount /dev/sr0
[root@test ~]# umount /dev/sr0
umount: /dev/sr0: not mounted
[root@test ~]# mount /dev/sr0  /media/
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@test ~]#


挂载U盘、SD、移动硬盘:
1、fdisk -l 查看设备的设备号 /dev/sdb   /dev/sdb1
2、mount使用。

mkdir /mnt/usb
mount /dev/sdb /mnt/usb  (u盘)
mount /dev/sdb1 /mnt/usb (移动硬盘)


挂载windows共享(cifs)
mount //192.168.1.100/winshare /mnt/wincifs -o username=administrator

挂载linux共享(nfs)
mount 192.168.1.200:/nfsshare /mnt/linuxnfs

挂载iso文件。(/1.iso)
mkdir /mnt/iso
mount /1.iso /mnt/iso -o loop


用户和工作组管理:
useradd/usermod/userdel/passwd
/etc/passwd 用户信息文件
/etc/shadow 用户密码文件,影子文件,权限为0

用户的创建:
方法一:通过命令。
方法二:在图形界面,通过system-config-users管理用户。
方法三:通过webmin管理用户。

[root@test ~]# useradd user1
[root@test ~]# cat /etc/default/useradd
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

[root@test ~]# useradd user1 与下面的命令功能相同。
[root@test ~]# useradd -d /home/user1 -f -1 -s /bin/bash  -k /etc/skel/ user1

[root@test ~]# cat /etc/passwd|grep user1
user1:x:502:502:xxxx:/home/user1:/bin/bash
账号名称:密码位:uid:gid:账号的描述信息:home目录:工作的shell

useradd user1 -u 502 -g 502 -c "this is test account" -d /home/user1 -s
/bin/bash

创建一个与root用户权限相同的用户admin。  (-o 去除用户uid的唯一性)
[root@test ~]# cat /etc/passwd|grep ^root
root:x:0:0:root:/root:/bin/bash
[root@test ~]# useradd admin -o -u 0 -g 0 -c root -d /root -s /bin/bash

[root@test ~]# useradd admin -o -u 0 -g 0 -c root -d /root -s /bin/bash
useradd:警告:此主目录已经存在。
不从 skel 目录里向其中复制任何文件。
[root@test ~]# cat /etc/passwd|grep -E  '^root|admin'
root:x:0:0:root:/root:/bin/bash
admin:x:0:0:root:/root:/bin/bash
[root@test ~]# su - admin
[root@test ~]# whoami
root


usermod 修改用户信息。 将user1改名为user.
[root@test ~]# cat /etc/passwd|tail -3
b:x:501:501::/home/b:/bin/bash
user1:x:502:502::/home/user1:/bin/bash
admin:x:0:0:root:/root:/bin/bash
[root@test ~]# usermod -l user user1
[root@test ~]# cat /etc/passwd|tail -3
b:x:501:501::/home/b:/bin/bash
admin:x:0:0:root:/root:/bin/bash
user:x:502:502::/home/user1:/bin/bash

userdel 删除用户信息。 -r 删除用户时,一并将用户的工作目录和邮箱删除。
[root@test ~]# cd /home/
[root@test home]# ls
a  b  user1
[root@test home]# userdel -r a
[root@test home]# ls
b  user1
[root@test home]# userdel b
[root@test home]# ls
b  user1
[root@test home]# cat /etc/passwd|grep ^b
bin:x:1:1:bin:/bin:/sbin/nologin
[root@test home]# rm /home/b /var/spool/mail/b -rf    通过rm命令补删用户资料。
[root@test home]# ls
user1
[root@test home]#

创建用户user1,要求uid为1000,gid为0,工作目录为/tmp/user1,密码为123
创建用户user2,不允许远程管理系统,但可以访问本系统中的授权资源。(/sbin/nologin)





创建用户user1,要求uid为1000,gid为0,工作目录为/tmp/user1,密码为123
[root@test home]# useradd user1 -u 1000 -g 0 -d /tmp/user1
[root@test home]# ls /tmp/user1/
[root@test home]# passwd user1
更改用户 user1 的密码 。
新的 密码:
无效的密码: 过短
无效的密码: 过于简单
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。
[root@test home]#

[root@test home]# su - user1  (会调用系统的配置文件)
[user1@test ~]$ exit
logout
[root@test home]# su user1   (只调用用户的配置文件)
[user1@test home]$ exit
exit


创建用户user2,不允许远程管理系统,但可以访问本系统中的授权资源。(/sbin/nologin)
<eg:邮箱用户,samba共享用户,ftp用户>  /sbin/nologin(redhat) ==
/bin/false(suse)
[root@test home]# useradd user2 -s /sbin/nologin
[root@test home]# cat /etc/passwd|grep user2
user2:x:1001:1001::/home/user2:/sbin/nologin
[root@test home]#
[root@test home]# passwd user2
更改用户 user2 的密码 。
新的 密码:
无效的密码: 过短
无效的密码: 过于简单
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。
[root@test home]# su - user2
This account is currently not available.
[root@test home]#


[root@test home]# cat /etc/shadow|grep user
rpcuser:!!:16370::::::
user:!!:16370:0:99999:7:::
user1:$6$.YwGugCD$HcuY2x3UTwFOGDny9x7/JMgweI4KwrKuhTpgtlxuAgh0erk/C12y3p.PIa.YEofD3wSpw1NztQ3Qad8zG8L7G1:16370:0:99999:7:::
user2:$6$PS6FaleS$QdIo3tLHu0LMkR3Sd9dsoUbLzZyOGBkK0.T/Y9hPhEos.faeKdslKeqYPpKr7NRfr5XqjP49H0Zf9iTTCncxu/:16370:0:99999:7:::
[root@test home]# passwd -l user1   锁定用户
锁定用户 user1 的密码 。
passwd: 操作成功
[root@test home]# cat /etc/shadow|grep user
rpcuser:!!:16370::::::
user:!!:16370:0:99999:7:::
user1:!!$6$.YwGugCD$HcuY2x3UTwFOGDny9x7/JMgweI4KwrKuhTpgtlxuAgh0erk/C12y3p.PIa.YEofD3wSpw1NztQ3Qad8zG8L7G1:16370:0:99999:7:::
user2:$6$PS6FaleS$QdIo3tLHu0LMkR3Sd9dsoUbLzZyOGBkK0.T/Y9hPhEos.faeKdslKeqYPpKr7NRfr5XqjP49H0Zf9iTTCncxu/:16370:0:99999:7:::
[root@test home]# passwd -u user1   解锁用户。
解锁用户 user1 的密码 。
passwd: 操作成功
[root@test home]# cat /etc/shadow|grep user
rpcuser:!!:16370::::::
user:!!:16370:0:99999:7:::
user1:$6$.YwGugCD$HcuY2x3UTwFOGDny9x7/JMgweI4KwrKuhTpgtlxuAgh0erk/C12y3p.PIa.YEofD3wSpw1NztQ3Qad8zG8L7G1:16370:0:99999:7:::
user2:$6$PS6FaleS$QdIo3tLHu0LMkR3Sd9dsoUbLzZyOGBkK0.T/Y9hPhEos.faeKdslKeqYPpKr7NRfr5XqjP49H0Zf9iTTCncxu/:16370:0:99999:7:::
[root@test home]#


工作组的管理:
groupadd/groupmod/groupdel/gpasswd
/etc/group
/etc/gshadow

groupadd -options <groupname>
-o gid <gid_number>  去除gid的唯一
-r 创建系统账号。

groupmod -options <groupname>
-n, --new-name NEW_GROUP 对工作组改名

groupdel <groupname>


创建工作组work1,gid号为0.将bin,user1,user2添加到此工作组。
[root@test home]# groupadd -o -g 0 work1
[root@test home]# cat /etc/group|grep work1
work1:x:0:
[root@test home]# cat /etc/gshadow|grep work1
work1:!::
[root@test home]# usermod -G work1 user1
[root@test home]# usermod -G work1 user2
[root@test home]# usermod -G work1 bin
[root@test home]# cat /etc/group|grep work1
work1:x:0:user1,user2,bin
[root@test home]#

===>
[root@test home]# usermod -G work1 user1
[root@test home]# usermod -G work1 user2
[root@test home]# usermod -G work1 bin
以上三行替换命令
[root@test home]# gpasswd -M user,user1,user2,bin work1
[root@test home]# cat /etc/group|grep work1
work1:x:0:user,user1,user2,bin

gpasswd的命令选项。
语法: gpasswd -options <groupname>
-a 添加用户到工作组中。   gpasswd -a root work1
-d 从工作组中删除用户。   gpasswd -d user1,user2 work1
-M 设置工作组中的成员。   gpasswd -M user,user1,user2,bin work1


用户和工作组相关的其它文件。
/etc/skel/*   新建用户的基本配置信息,属性信息。
~/.bashrc 某个用户的bash初始化文件
~/.bash_profile 某个用户的系统配置文件
/etc/profile 所有用户的系统配置文件
/etc/bashrc 所有用户的Bash初始化文件
/etc/login.defs 用户登录的配置文件
/etc/default/useradd  useradd命令的默认值

[root@test default]# cd /etc/skel/
[root@test skel]# ls
[root@test skel]# ls -a
.  ..  .bash_logout  .bash_profile  .bashrc  .gnome2
[root@test skel]# useradd test
[root@test skel]# ls /home/test
[root@test skel]# ls /home/test -a
.  ..  .bash_logout  .bash_profile  .bashrc  .gnome2
[root@test skel]#


[root@test ~]# echo .bashrc
.bashrc
[root@test ~]# echo "echo .bashrc" >> .bashrc
[root@test ~]# echo "echo .bash_profile" >> .bash_profile
[root@test ~]# echo "echo /etc/profile" >> /etc/profile
[root@test ~]# echo "echo /etc/bashrc" >>/etc/bashrc
[root@test ~]# su root
/etc/bashrc
.bashrc
[root@test ~]# exit
exit
[root@test ~]# su - root
/etc/profile
/etc/bashrc
.bashrc
.bash_profile
[root@test ~]#

用户登录测试:
login as: root
root@172.16.4.49's password:
Last login: Mon Oct 27 20:09:56 2014 from 172.16.4.47
/etc/profile
/etc/bashrc
.bashrc
.bash_profile
[root@test ~]#

[root@test ~]# passwd -S root
root PS 2014-10-27 0 99999 7 -1 (密码已设置,使用 SHA512 加密。)
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics