linux 系統管理與安全:基本 linux 系統知識

74

Upload: -

Post on 28-Jul-2015

865 views

Category:

Technology


6 download

TRANSCRIPT

Page 1: Linux 系統管理與安全:基本 Linux 系統知識
Page 2: Linux 系統管理與安全:基本 Linux 系統知識

Page 3: Linux 系統管理與安全:基本 Linux 系統知識

Page 4: Linux 系統管理與安全:基本 Linux 系統知識
Page 5: Linux 系統管理與安全:基本 Linux 系統知識

Page 6: Linux 系統管理與安全:基本 Linux 系統知識

Page 7: Linux 系統管理與安全:基本 Linux 系統知識

Page 8: Linux 系統管理與安全:基本 Linux 系統知識

Page 9: Linux 系統管理與安全:基本 Linux 系統知識

Page 10: Linux 系統管理與安全:基本 Linux 系統知識

Page 11: Linux 系統管理與安全:基本 Linux 系統知識
Page 12: Linux 系統管理與安全:基本 Linux 系統知識

•$  man  [section]  <program>  

LS(1)                                                                                User  Commands                                                                                LS(1)

NAMEls  -­‐ list  directory  contents

SYNOPSISls  [OPTION]...  [FILE]...

DESCRIPTIONList    information    about    the  FILEs  (the  current  directory  by  default).    Sort  entries  alphabeti-­‐cally if  none  of  -­‐cftuvSUX nor  -­‐-­‐sort  is  specified.

Mandatory  arguments  to  long  options  are  mandatory  for  short  options  too.

-­‐a,  -­‐-­‐alldo  not  ignore  entries  starting  with  .

Page 13: Linux 系統管理與安全:基本 Linux 系統知識

• $  echo  -­‐e  "abc\na"abca

Page 14: Linux 系統管理與安全:基本 Linux 系統知識

• STDIN  (0)

• STDOUT  (1)

• STDERR  (2)

• $  cat  /etc/passwd >  users.txt(STDOUT)-­‐

• $  cat  /etc/passwd >>  users.txt(STDOUT)-­‐

• $  mail  -­‐s  "Test  mail"  <  letter.txt(STDIN)

Page 15: Linux 系統管理與安全:基本 Linux 系統知識

• $  cat  /etc/passwd 2>  errors.txt( STDERR)

• $  find  /etc >  /dev/null( STDOUT)

• $  sh script.sh  >  /dev/null  2>&1( )

• $  sh script.sh  |  tee  -­‐a  log.txt  |  other_program( log.txt other_program)

• $  sh script.sh  |  xargs other_program( other_program )

Page 16: Linux 系統管理與安全:基本 Linux 系統知識

• |

• $  cat  /etc/passwd |  program

Page 17: Linux 系統管理與安全:基本 Linux 系統知識

• users.txt   snowman  $  grep snowman users.txt

• txt   snowman  $  grep snowman *.txt

• $  ifconfig |  grep 'eth0'  -­‐-­‐color=auto

•$  grep -­‐r  'some_text'  /etc/

•$  grep –e  '^Snowman$'  users.txt

Page 18: Linux 系統管理與安全:基本 Linux 系統知識

• $  find  [ ]  [ ]

• -­‐name  

• -­‐perm  

• -­‐type  

• -­‐mtime

• $  find  /etc -­‐name  passwd

Page 19: Linux 系統管理與安全:基本 Linux 系統知識

• $  sed [ ]  < >

Page 20: Linux 系統管理與安全:基本 Linux 系統知識

• passwd 2~5  $  nl /etc/passwd |  sed '2,5d'

• passwd 5~7  $  nl  /etc/passwd  |  sed  -­‐n  '5,7p'

•$  sed  's/ / /g'

• $  ifconfig  enp0s3  |  grep  'inet'  \|  sed  's/^.*inet  //g'  |  sed  's/netmask.*$//g'  

Page 21: Linux 系統管理與安全:基本 Linux 系統知識

• awk ' '  

• awk '{ }'   {}

• awk ' { }'   {}

• sed awk

Page 22: Linux 系統管理與安全:基本 Linux 系統知識

• tab  $  last  -­‐n  5  |  awk '{print  $1  "\t"  $3}'

• $  free  -­‐htotal              used              free          shared        buffers          cached

Mem:                    7.8G              7.6G              168M                18M              440M              3.0G-­‐/+  buffers/cache:              4.1G              3.6G

2.0G              232K              2.0G

• $  free  -­‐h  |  awk '/Mem/{print  $3}'7.6G

Page 23: Linux 系統管理與安全:基本 Linux 系統知識

• STDIN  

• /tmp core  $  find  /tmp -­‐name  '*core*' -­‐type  f -­‐print  |  xargs/bin/rm -­‐f

• /etc 664  $ find  /etc -­‐perm  -­‐664  |  xargs ls –l

Page 24: Linux 系統管理與安全:基本 Linux 系統知識

• cat  

• head  

• tail  

• sort  

• more/less  

• nl

• cut  

• uniq

• wc

Page 25: Linux 系統管理與安全:基本 Linux 系統知識

• $  cat  /proc/loadavg0.48  0.47  0.55  2/347  9893

• $  cat  /proc/loadavg  |  [...]0.48

• $  cat  /proc/meminfoMemTotal:                8138396  kBMemFree:                  2895168  kBBuffers:                    599540  kB...

• $  cat  /proc/meminfo |  [...]2895168

Page 26: Linux 系統管理與安全:基本 Linux 系統知識

1. $  cat  /proc/loadavg |  awk '{print  $1}'

2. $  cat  /proc/meminfo |  grep MemFree |  awk '{print  $2}'

Page 27: Linux 系統管理與安全:基本 Linux 系統知識
Page 28: Linux 系統管理與安全:基本 Linux 系統知識

• ls

• cd  <path>

• pwd

• file  <file>

• mkdir <folder>

• rmdir <folder>

• touch  <filename>

• ln  <destination>  <source>

Page 29: Linux 系統管理與安全:基本 Linux 系統知識

$  ls  -­‐al-­‐rwxr-­‐xr-­‐-­‐ 1  sntc06  sntc06 2.6K  Oct    8    2012  action.php

-­‐rwxr-­‐xr-­‐-­‐

-­‐:  regular  filed:  directoryl:  symbolic  linkc:  character  deviceb:  block  devices:  socketp:  named  pipe

Page 30: Linux 系統管理與安全:基本 Linux 系統知識

$  ls  -­‐al-­‐rwxr-­‐xr-­‐-­‐ 1  sntc06  sntc06 2.6K  Oct    8    2012  action.php

-­‐rwxr-­‐xr-­‐-­‐

Owner  permissionr:  readw:  writex:  execute

Group:r-­‐xOthers:r-­‐-­‐

Page 31: Linux 系統管理與安全:基本 Linux 系統知識

$  ls  -­‐al-­‐rwxr-­‐xr-­‐-­‐ 1  sntc06  sntc06 2.6K  Oct    8    2012  action.php

-­‐rwxr-­‐xr-­‐-­‐

Owner  permissionr:  100:  4w:  010:  2x:  001:  1

-­‐>   754

Example:

$  chmod 644  action.php$  chmod a+x action.php$  chmod -­‐w  action.php

Page 32: Linux 系統管理與安全:基本 Linux 系統知識

$  ls  -­‐al-­‐rwxr-­‐xr-­‐-­‐ 1  sntc06  sntc06 2.6K  Oct    8    2012  action.php

Page 33: Linux 系統管理與安全:基本 Linux 系統知識

$  ls  -­‐al-­‐rwxr-­‐xr-­‐-­‐ 1  sntc06  sntc06 2.6K  Oct    8    2012  action.php

Page 34: Linux 系統管理與安全:基本 Linux 系統知識

$  ls  -­‐al-­‐rwxr-­‐xr-­‐-­‐ 1  sntc06  sntc06 2.6K  Oct    8    2012  action.php

Page 35: Linux 系統管理與安全:基本 Linux 系統知識

• #  mount  /dev/sdd1  /media/usb-­‐drive

• #  mount  -­‐o  remount,ro -­‐t  ext4  /dev/sda1  /home-­‐o [ ] -­‐t  [ ]

•#  umount /media/usb-­‐drive

Page 36: Linux 系統管理與安全:基本 Linux 系統知識
Page 37: Linux 系統管理與安全:基本 Linux 系統知識

• init /  systemd

Page 38: Linux 系統管理與安全:基本 Linux 系統知識

Page 39: Linux 系統管理與安全:基本 Linux 系統知識

Page 40: Linux 系統管理與安全:基本 Linux 系統知識

• process$  ps -­‐l

• process#  ps axu

• process  tree#  ps axjf

1     2827    2827    2827  ?                      -­‐1  Ss 1003      0:00  SCREEN2827    2828    2828    2828  pts/4          2836  Ss 1003      0:00    \_  /bin/zsh2828    2836    2836    2828  pts/4          2836  S+        1003      0:00    |      \_  /bin/bash  ./start.sh2836    2837    2836    2828  pts/4          2836  Sl+      1003  207:14    |              \_  java  -­‐server  …2827    3025    3025    3025  pts/8          3025  Ss+      1003      0:00    \_  /bin/zsh

root          27850    0.0    0.0  113908    4332  ?                Ss Apr01      0:00  sshd:  sntc06  [priv]sntc06      27976    0.0    0.0  114048    2080  ?                S        Apr01      0:00  sshd:  sntc06@pts/6sntc06      27977    0.0    0.0    52076    6388  pts/6        Ss+    Apr01      0:00  -­‐zsh

F  S      UID      PID    PPID    C  PRI    NI  ADDR  SZ  WCHAN    TTY                    TIME  CMD0  S    1000    8755    8754    0    80      0  -­‐ 12210  sigsus pts/11      00:00:00  zsh0  R    1000    9082    8755    0    80      0  -­‐ 3383  -­‐ pts/11      00:00:00  ps

Page 41: Linux 系統管理與安全:基本 Linux 系統知識

Page 42: Linux 系統管理與安全:基本 Linux 系統知識

• Ctrl+Z

• fg

• bg

• jobs  

• shell   &  $  ping  localhost  &

Page 43: Linux 系統管理與安全:基本 Linux 系統知識

No.

1 SIGHUP log  file2 SIGINT Interrupt  (Ctrl+C)

3 SIGQUIT

9 SIGKILL

15 SIGTERM

18 SIGCONT

19 SIGSTP (Ctrl+Z)

$  man  7  signal

Page 44: Linux 系統管理與安全:基本 Linux 系統知識

• pid

• kill  [-­‐signal]  <pid>

• PID  5566  #  kill  5566#  kill  -­‐15  5566

• PID  5566  #  kill  -­‐9 5566

• PGID  5566  #  kill  -­‐1 -­‐5566( )

Page 45: Linux 系統管理與安全:基本 Linux 系統知識

• PID

• root   PID$  pgrep -­‐u  root  -­‐l

• httpd$  pgrep httpd -­‐l

Page 46: Linux 系統管理與安全:基本 Linux 系統知識

• pkill [-­‐signal]  < >

• httpd#  pkill httpd

• httpd#  pkill -­‐9  httpd

Page 47: Linux 系統管理與安全:基本 Linux 系統知識
Page 48: Linux 系統管理與安全:基本 Linux 系統知識

Page 49: Linux 系統管理與安全:基本 Linux 系統知識

• /etc/rcX.d/

• /etc/init.d/

Page 50: Linux 系統管理與安全:基本 Linux 系統知識

• sysvinit upstartinit

•$  systemctl

• / /#  systemctl start/stop/restart  <service>

• /#  systemctl status/is-­‐enabled  <service>

• /#  systemctl enable/disable  <service>

Page 51: Linux 系統管理與安全:基本 Linux 系統知識

root:~/  #  systemctl status  httpdhttpd.service -­‐ The  Apache  HTTP  Server

Loaded:  loaded  (/usr/lib/systemd/system/httpd.service;  enabled)Active:  active  (running)  since   2015-­‐04-­‐01  16:10:40  CST;  10min  ago

Main  PID:  1221  (httpd)Status:  "Total  requests:  0;  Current  requests/sec:  0;  Current  traffic:  0  B/sec"CGroup:  /system.slice/httpd.service

├─1221  /usr/sbin/httpd -­‐DFOREGROUND├─2475  /usr/sbin/httpd -­‐DFOREGROUND├─2476  /usr/sbin/httpd -­‐DFOREGROUND├─2477  /usr/sbin/httpd -­‐DFOREGROUND├─2478  /usr/sbin/httpd -­‐DFOREGROUND└─2479  /usr/sbin/httpd -­‐DFOREGROUND

4 01  16:10:37  localhost.localdomain httpd[1221]:  AH00558:  httpd:  ...4 01  16:10:40  localhost.localdomain systemd[1]:  Started  The  Apache  HTTP  Server.

Hint:  Some  lines  were  ellipsized,  use  -­‐l  to  show  in  full.

Page 52: Linux 系統管理與安全:基本 Linux 系統知識

[Unit]Description=The  Apache  HTTP  ServerAfter=network.target remote-­‐fs.target nss-­‐lookup.target

[Service]Type=notifyEnvironmentFile=/etc/sysconfig/httpdExecStart=/usr/sbin/httpd $OPTIONS  -­‐DFOREGROUNDExecReload=/usr/sbin/httpd $OPTIONS  -­‐k  gracefulExecStop=/bin/kill  -­‐WINCH  ${MAINPID}#  We  want  systemd to  give  httpd some  time  to  finish  gracefully,  but  still  want#  it  to  kill  httpd after  TimeoutStopSec if  something  went  wrong  during  the#  graceful  stop.  Normally,  Systemd sends  SIGTERM  signal  right  after  the#  ExecStop,  which  would  kill  httpd.  We  are  sending  useless  SIGCONT  here  to  give#  httpd time  to  finish.KillSignal=SIGCONTPrivateTmp=true

[Install]WantedBy=multi-­‐user.target

Page 53: Linux 系統管理與安全:基本 Linux 系統知識

• at

• crontab

•$  crontab -­‐l

•$  crontab -­‐e

• root  #  crontab -­‐u  root

root:/dev/  #  at  18:30  2015-­‐04-­‐01at>  echo  "Happy  April  Fools!"  >  /dev/pts/0at>  <EOT>job  2  at  Wed  Apr    1  18:20:00  2015

Page 54: Linux 系統管理與安全:基本 Linux 系統知識

• /etc/crontab,  /etc/cron.d/SHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=root

#m      h    dom mon  dow user    command0 0 *     * 1 root  /bukkit_backup/backup.sh  >>  /dev/null  2>&1*/30 8-­‐17 *     * 1-­‐5 root  /root/check_website.sh

Page 55: Linux 系統管理與安全:基本 Linux 系統知識

• /etc/cron.daily/

• /etc/cron.weekly/

• /etc/cron.monthly/

#period  in  days      delay  in  minutes      job-­‐identifier      command1              5              cron.daily nice  run-­‐parts  /etc/cron.daily7              25            cron.weekly nice  run-­‐parts  /etc/cron.weekly@monthly  45          cron.monthly nice  run-­‐parts  /etc/cron.monthly

Page 56: Linux 系統管理與安全:基本 Linux 系統知識

• mariadb

Page 57: Linux 系統管理與安全:基本 Linux 系統知識
Page 58: Linux 系統管理與安全:基本 Linux 系統知識

Page 59: Linux 系統管理與安全:基本 Linux 系統知識

Page 60: Linux 系統管理與安全:基本 Linux 系統知識

• *.rpm

•#  rpm  -­‐i package.rpm

•#  rpm  -­‐U  package.rpm

•#  rpm  -­‐i package.rpm -­‐-­‐test  -­‐v

•#  rpm  -­‐e  package

•#  rpm  -­‐q  -­‐a

Page 61: Linux 系統管理與安全:基本 Linux 系統知識

Page 62: Linux 系統管理與安全:基本 Linux 系統知識

•#  yum  check-­‐update

•#  yum  update  mosh

•#  yum  install  mosh

•#  yum  remove  mosh

Page 63: Linux 系統管理與安全:基本 Linux 系統知識

•#  yum  list  installed

•#  yum  search  mosh

•#  yum  info  mosh

Page 64: Linux 系統管理與安全:基本 Linux 系統知識

• mariadb

Page 65: Linux 系統管理與安全:基本 Linux 系統知識
Page 66: Linux 系統管理與安全:基本 Linux 系統知識

• /etc/passwd

• /etc/group

root:x:0:0:root:/root:/bin/zsh

root:x:0:user1,user2

Page 67: Linux 系統管理與安全:基本 Linux 系統知識

• /etc/shadow

Page 68: Linux 系統管理與安全:基本 Linux 系統知識

•$  passwd

•#  passwd sntc06

Page 69: Linux 系統管理與安全:基本 Linux 系統知識

•#  useradd [ ]  < >

• -­‐m

• -­‐c

• -­‐u UID

• -­‐g GID

• -­‐G

• -­‐s login  shell

Page 70: Linux 系統管理與安全:基本 Linux 系統知識

• admin#  groupadd admin

• test#  groupdel test

• snowman   admin#  usermod -­‐a  -­‐G  admin  snowman

Page 71: Linux 系統管理與安全:基本 Linux 系統知識

•$  su -­‐ root  ( login  shell)$  su ( shell)

• snowman  $  su snowman

Page 72: Linux 系統管理與安全:基本 Linux 系統知識

• snowman  $  sudo -­‐u  snowman  vim  test.txt

• root  $  sudo reboot

• root   shell$  sudo su -­‐

Page 73: Linux 系統管理與安全:基本 Linux 系統知識

##  Allow  root  to  run  any  commands  anywhereroot        ALL=(ALL)              ALL

##  Allows  people  in  group  wheel  to  run  all  commands%wheel    ALL=(ALL)              ALL

Page 74: Linux 系統管理與安全:基本 Linux 系統知識

1.

2. sudo

3. su

4. sudo mariadb