天天看點

shell 常見面試題及答案(4)

本文接 《shell 常見面試題及答案(3)》

七、

一、指令部分

1.建立一個使用者user,密碼設為123456,并将其加到root組。寫出所用指令。

Loong:/home/yee/shell# adduser julius
Adding user `julius' ...
Adding new group `julius' (1001) ...
Adding new user `julius' (1001) with group `julius' ...
Creating home directory `/home/julius' ...
Copying files from `/etc/skel' ...
輸入新的 UNIX 密碼:
重新輸入新的 UNIX 密碼:
passwd:已成功更新密碼
正在改變 julius 的使用者資訊
請輸入新值,或直接敲Enter鍵以使用預設值
    全名 []: julius
    房間号碼 []: 
    工作電話 []: 
    家庭電話 []: 
    其它 []: 
Is the information correct? [Y/n] y      

Loong:/home/julius# groups julius

julius

Loong:/home/julius# groups root

root fileshare

Loong:/home/julius# usermod -g root julius    

組在前,使用者在後

Loong:/home/julius# groups julius

root

Loong:/home/julius#

一,組操作

1,建立組

groupadd  test

增加一個test組

2,修改組

groupmod -n test2  test

将test組的名子改成test2

3,删除組

groupdel test2

删除 組test2

4,檢視組

a),檢視目前登入使用者所在的組 groups (使用者名)

2.将目前計算機的IP位址設為192.168.1.123,子網路遮罩為255.255.255.0。寫出所用指令。

ifconfig eth0 192.168.1.123 netmask 255.255.255.0

3.你是公司的Linux服務的管理者。你使用vi編緝/etc/inittab檔案時,在指令模式下查詢shutdown字元串,使用的指令。

/shutdown 回車

4.顯示目前系統的日期和時間指令。

yee@Loong:~/shell$ date
2012年 11月 30日 星期五 15:22:19 CST      

5.檢視指令sort的幫助檔案,寫出所用指令。

man sort

info sort

sort --help

6.統計/etc/passwd檔案的位元組數和行數,寫出所用指令。

yee@Loong:~/shell$ wc -l /etc/passwd
31 /etc/passwd
yee@Loong:~/shell$ wc -c /etc/passwd
1435 /etc/passwd      

7.分屏顯示/etc/httpd/conf/httpd.conf檔案的指令。

:vsplit

:vsplit xxx(要打開的檔案名)

水準切割:

:split

:split xxx(要打開的檔案名)

:new(在新視窗建立檔案)

<CTRL+W> k:跳轉到上面的視窗 (若為大寫K,則是把目前視窗調至上方)

<CTRL+W> j:跳轉到下面的視窗 (類似于上)

垂直切割:

:vsplit

:vsplit xxx(要打開的檔案名)

:vnew(在新視窗建立檔案)

:only        取消分屏

<CTRL+W> h:跳轉到左邊的視窗 (若為大寫H,則是把目前視窗調至左方)

<CTRL+W> l:跳轉到右邊的視窗 (類似于上)

<CTRL+W> w:在視窗之間跳轉

<CTRL+W> +:擴大視窗 (或在目前視窗位置,輸入指令:res +9)

<CTRL+W> -:縮小視窗

8.将hello!寫入 /home/wang/test.txt 的指令(vi及vim編輯器除外)。

Loong:/home/yee/shell# cat kkk.txt 
name=abc
pwd=123456
permission=rwx
Loong:/home/yee/shell# echo "hello" >> kkk.txt 
Loong:/home/yee/shell# cat kkk.txt 
name=abc
pwd=123456
permission=rwx
hello      

9.讓named守護程序運作在級别3,5的指令。

# telinit 3                    #用init也可以

# telinit 5                   #不懂named守護程序是啥程序,沒有找到

        守護程序,也就是通常說的Daemon程序,是Linux中的​​背景​​服務程序。它是一個生存期較長的程序,通常獨立于控制終端并且周期性地執行某種任務或等待處理某些發生的事件。守護程序常常在系統引導裝入時啟動,在系統關閉時終止。Linux系統有很多守護程序,大多數服務都是通過守護程序實作的,同時,守護程序還能完成許多系統任務,例如,作業規劃程序crond、列印程序lqd等(這裡的結尾字母d就是Daemon的意思)。

  由于在Linux中,每一個系統與使用者進行交流的界面稱為終端,每一個從此終端開始運作的程序都會依附于這個終端,這個終端就稱為這些程序的控制終端,當控制終端被關閉時,相應的程序都會自動關閉。但是守護程序卻能夠突破這種限制,它從被執行開始運轉,直到整個系統關閉時才退出。如果想讓某個程序不因為使用者或終端或其他地變化而受到影響,那麼就必須把這個程序變成一個守護程序。

常用的守護程序簡單介紹:

Kudzu(硬體檢測)  sshd(遠端連接配接)  inetd(網絡連接配接) ldap(目錄通路協定)

​​Linux​​​運作等級介紹:

最常用的是3,5

run level 0:是作關機用的,一開機就會作關機的動作。

run level 1:都是Single user mode模式,隻允許root賬号登入,主要是作一些系統維護的工作。

run level 2:可以使所有的使用者登入,但不會啟用NFS working,也就是沒有網絡功能

run level 3:可以使所有使用者登入,并擁有完整的功能。包含run level 2沒有的功 能,但是開機後是用文本模式

run level 4 :使用者自己定義,但是預設情況下和run level 3完全相同,

run level 5:和run level 3幾乎一樣,唯一的不同是開機後是圖形界面,

run level 6:重新開機。開機後立刻重新開機

顯示系統目前運作級别:

# runlevel          檢視運作級别。

改變系統運作級别:

# init [0123456]   更改運作級别。(所更改的内容儲存在記憶體中,關機或重新開機後還原)

系統服務程式在各運作級别的啟動狀态:

檢視服務啟動狀态:

# Chkconfig  --list 檢視目前系統中所有程序。

# chkconfig  --list    檢視單個程序。

設定獨立服務的啟動狀态:

  Chkconfig  --level   ]

例:# chkconfig --level 234 init off     将init程序設定為在2 3 4運作級别為off。

10.更改使用者zhang的登入shell為/sbin/nologin的指令。

# usermod –s /sbin/nologin  yangang

11.檢視目前linux伺服器的主機名的指令。

yee@Loong:~$ uname -a
Linux Loong 2.6.27.1 #6 Wed Nov 10 16:17:35 CST 2010 mips64 GNU/Linux
yee@Loong:~$ cat /proc/sys/kernel/ho
hostname  hotplug   
yee@Loong:~$ cat /proc/sys/kernel/hostname 
Loong      

12.檢視目前主機的路由的指令。

yee@Loong:~$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.16.0.0      *               255.255.0.0     U     1      0        0 eth0
default         172.16.1.254    0.0.0.0         UG    0      0        0 eth0
yee@Loong:~$      

13.配置目前主機的預設網關為192.168.2.254的指令。

 # route add default gw 192.168.2.254

14.連續ping目的IP為192.168.2.245為10次的的指令。

yee@Loong:~$ ping -c 10 172.16.0.30
PING 172.16.0.30 (172.16.0.30) 56(84) bytes of data.
64 bytes from 172.16.0.30: icmp_seq=1 ttl=64 time=4.48 ms
64 bytes from 172.16.0.30: icmp_seq=2 ttl=64 time=0.241 ms
64 bytes from 172.16.0.30: icmp_seq=3 ttl=64 time=0.231 ms
64 bytes from 172.16.0.30: icmp_seq=4 ttl=64 time=0.242 ms
64 bytes from 172.16.0.30: icmp_seq=5 ttl=64 time=0.226 ms
64 bytes from 172.16.0.30: icmp_seq=6 ttl=64 time=0.214 ms
64 bytes from 172.16.0.30: icmp_seq=7 ttl=64 time=0.259 ms
64 bytes from 172.16.0.30: icmp_seq=8 ttl=64 time=0.230 ms
64 bytes from 172.16.0.30: icmp_seq=9 ttl=64 time=0.212 ms
64 bytes from 172.16.0.30: icmp_seq=10 ttl=64 time=0.234 ms

--- 172.16.0.30 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9012ms
rtt min/avg/max/mdev = 0.212/0.657/4.484/1.275 ms
yee@Loong:~$      

15.檢視目前主機TCP協定連接配接情況的指令。

yee@Loong:~$ netstat -t
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0     52 Loong-3.local:ssh       172.16.7.94:52508       ESTABLISHED
tcp        0      0 Loong-3.local:ssh       172.16.7.94:53189       ESTABLISHED
yee@Loong:~$      

NETSTAT(8)                           Linux Programmer's Manual                           NETSTAT(8)

NAME

       netstat  -  Print network connections, routing tables, interface statistics, masquerade con-

       nections, and multicast memberships

SYNOPSIS

       netstat   [address_family_options]   [--tcp|-t]   [--udp|-u]   [--raw|-w]   [--listening|-l]

       [--all|-a]  [--numeric|-n]  [--numeric-hosts]  [--numeric-ports]  [--numeric-users]  [--sym-

       bolic|-N] [--extend|-e[--extend|-e]] [--timers|-o] [--program|-p] [--verbose|-v] [--continu-

       ous|-c]

       netstat   {--route|-r}  [address_family_options]  [--extend|-e[--extend|-e]]  [--verbose|-v]

       [--numeric|-n] [--numeric-hosts] [--numeric-ports] [--numeric-users] [--continuous|-c]

       netstat  {--interfaces|-i}  [--all|-a]  [--extend|-e[--extend|-e]]  [--verbose|-v]   [--pro-

       gram|-p]  [--numeric|-n]  [--numeric-hosts]  [--numeric-ports] [--numeric-users] [--continu-

       ous|-c]

       netstat {--groups|-g} [--numeric|-n] [--numeric-hosts]  [--numeric-ports]  [--numeric-users]

       [--continuous|-c]

       netstat  {--masquerade|-M}  [--extend|-e] [--numeric|-n] [--numeric-hosts] [--numeric-ports]

       [--numeric-users] [--continuous|-c]

       netstat {--statistics|-s} [--tcp|-t] [--udp|-u] [--raw|-w]

16.打包并壓縮/etc目錄的指令。

yee@Loong:~$ tar -czvf etc.tar.gz /etc      

17.測試httpd是否安裝的指令。

 # rpm  -q  httpd

18.解除安裝named軟體包的指令。

 # rpm –e named   

19.檢視月曆并将内容追加到檔案/dd中的指令。

yee@Loong:~$ cal -j
        十二月 2012        
 一  二  三  四  五  六  日
                    336 337
338 339 340 341 342 343 344
345 346 347 348 349 350 351
352 353 354 355 356 357 358
359 360 361 362 363 364 365
366      
yee@Loong:~$ cal -j >>qq      

CAL(1)                              BSD General Commands Manual                              CAL(1)

NAME

     cal, ncal - displays a calendar and the date of easter

SYNOPSIS

     cal

[-3jmy] [[month] year]

     ncal [-jJpwy] [-s country_code] [[month] year]

     ncal [-Jeo] [year]

yee@Loong:~$ cal -3j
    十一月 2012           十二月 2012            一月 2013        
一 二 三 四 五 六 日  一 二 三 四 五 六 日  一 二 三 四 五 六 日  
          1  2  3  4                  1  2      1  2  3  4  5  6
 5  6  7  8  9 10 11   3  4  5  6  7  8  9   7  8  9 10 11 12 13
12 13 14 15 16 17 18  10 11 12 13 14 15 16  14 15 16 17 18 19 20
19 20 21 22 23 24 25  17 18 19 20 21 22 23  21 22 23 24 25 26 27
26 27 28 29 30        24 25 26 27 28 29 30  28 29 30 31         
                      31      

20.檢視目前目錄的指令。

Pwd或ls

21.顯示曆史指令表的所有内容的指令。

history

22.将/etc/passwd的内容追加到檔案aaa中的指令。

cat /etc/passwd >>aaa

23.檢視/etc/passwd檔案的前10行的指令。

head -n 10 /etc/passwd

24.檢視/etc/vsftpd/vsftpd.conf的後10行的指令。

tail -n 10 /etc/vsftpd/vsftpd.conf

25.重新開機samba服務的指令。

yee@Loong:~$ /etc/init.d/samba restart      

service smb restart

26.檢視程序狀态的指令。

ps -aux 或top

27.列出背景作業的指令。

jobs -l

28.将作業ID為5的背景作業放到前台的指令。

fg 5

29.停止ssh服務的指令。

/etc/init.d/ssh stop      

service sshd stop

30.将name=wxstc配置為全局變量的指令。

export name=wxstc

31.重新開機linux伺服器的指令。

shutdown -r now

reboot

32.要給檔案file1加上其他人可執行屬性的指令。

chmod o+x file1

33.為了達到使檔案sudent.txt的所有者有讀(r)和寫(w)的許可,而其他使用者隻能進行隻讀通路的指令。

chmod u=rw,o=r student.txt

34.把大寫字母轉換為小寫字母的指令。

tr  'A-Z' 'a-z'

35.顯示作業系統核心版本詳細資訊的指令。

uname -a

36.将建立檔案權限設定為600的指令。

umask 600 filename

umask指令的一般格式:

  umask [選項] [

​​掩碼​​]  該指令用來設定限制新檔案權限的掩碼。當新檔案被建立時,其最初的權限由檔案建立掩碼決定。使用者每次注冊進入系統時,umask指令都被執行,并自動設定掩碼改變預設值,新的權限将會把舊的覆寫。  選項及其含義如下。  -S:顯示目前的掩碼。  umask是從權限中“拿走”相應的位,且檔案建立時不能賦予執行權限

umask設定了使用者建立檔案的預設 權限,它與chmod的效果剛好相反,umask設定的是權限“補碼”,而chmod設定的是檔案權限碼。

37.從IP位址查找域名或從域名來查找IP位址應使用什麼指令。

host

38.如何檢視使用者zhang屬于哪個組。

cat /etc/group

39.假如目前系統是在 level 3 運作,不重新開機系統就可轉換到 level 5 運作的指令。

telinit 5

40.檢視Linux的啟動資訊的指令。

dmesg

41.建立一個以自己姓的拼音縮寫為名的使用者。

useradd lee

42.進入使用者主目錄,顯示目前的路徑。

cd /home/lee

pwd

43.複制檔案/etc/group到使用者主目錄,檔案名不變。

cp /etc/group /home/lee

44.統計檔案/etc/fstab的行數、單詞數、字元數。

yee@Loong:~$ wc -lwc /etc/fstab 
  5  32 179 /etc/fstab
yee@Loong:~$      

45.檢視/etc/passwd檔案的後5行。

ail -5 /etc/passwd

46.建立普通檔案/home/abc。

touch /home/abc

47.修改/home/abc的屬性為:屬主具有一切權限,同組使用者和其他使用者具有可讀可寫的權限。

chmod 766 /home/abc

chmod u=rwx,go=rw /home/abc

48. 顯示目前的時間。

date

49. 寫出系統1分鐘後關機的指令。

shutdown -t -s 60       #因系統不同參數有些差異

50. 啟動ssh伺服器。

service sshd start

56.顯示2011年1月的月曆。

yee@Loong:~$ ncal 01 2011
    一月 2011
一     3 10 17 24 31
二     4 11 18 25   
三     5 12 19 26   
四     6 13 20 27   
五     7 14 21 28   
六  1  8 15 22 29   
日  2  9 16 23 30      
yee@Loong:~$ cal 01 2011
     一月 2011      
一 二 三 四 五 六 日
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31                  
yee@Loong:~$      

57.建立檔案/etc/group的符号連結檔案/root/abc。

ln –s /etc/group /root/abc

       -d, -F, --directory

              allow the superuser to attempt to hard link directories (note: will probably fail due

              to system restrictions, even for the superuser)

       -f, --force

              remove existing destination files

       -n, --no-dereference

              treat destination that is a symlink to a directory as if it were a normal file

       -i, --interactive

              prompt whether to remove destinations

       -s, --symbolic

              make symbolic links instead of hard links

       -S, --suffix=SUFFIX

              override the usual backup suffix

       -t, --target-directory=DIRECTORY

              specify the DIRECTORY in which to create the links

58.背景啟動程式gedit。

/usr/bin/gedit &

67.寫出30分鐘後系統重新開機的指令。

shutdown –s –t 1800

77.設定目前時間設為15:00,顯示目前系統時間。

date –s “15:00”

date

80.檢視DHCP伺服器的狀态。

 service dhcpd status

二、操作題

1. 某系統管理者需每天做一定的重複工作,請按照下列要求,編制一個解決方案:

(1)在下午4 :50删除/abc目錄下的全部子目錄和全部檔案;

(2)從早8:00~下午6:00每小時讀取/xyz目錄下x1檔案中每行第一個域的全部資料加入到/backup目錄下的bak01.txt檔案内。

 利用crontab 編輯定時任務,舉例設定每個4分鐘重新開機一次如下:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
 */4 *  *  *  * /usr/bin/reboot   #設定每個4分鐘重新開機一次      

2. 某學校内部既存在WINDOWS作業系統,又存在LINUX作業系統,為了友善資源共享,建立一台SAMBA伺服器,其位址為192.168.0.5,建立std、teacher兩個使用者,同時設定其密碼。

 實踐

3.富曼公司有2個部門,分别是研發部yf和工廠中的房間cj.搭建公司WWW伺服器,IP位址為192.168.2.6,公司域名為www.fuman.com,研發部域名為yf.fuman.com,工廠中的房間域名為cj.fuman.com,要求根據以上域名在IP位址192.168.2.6上搭建基于域名的虛拟伺服器,并且不允許192.168.2.245的IP通路研發部的網頁。

  實踐

4.外語系老師為了共享教學資料,需要架設一個FTP伺服器,允許所有老師以匿名方式登入FTP伺服器,可以浏覽檔案、上傳檔案和建立檔案夾,但不允許删除和修改檔案。

  實踐

5.編寫一個名為mul的腳本程式,參數為一個大于20的正整數。先檢查參數是否符合要求。如果不符合要求,請給出提示;如果符合要求,分别輸出其與1到10的乘積。

1 #!/bin/bash
  2 
  3 num=0
  4 if [ $1 -gt 20 ]
  5 then    
  6         for((i=1;i<11;i++))
  7         do      
  8                 num=`expr $1 \* $i`
  9                 echo "$1 * $i = $num "
 10         done    
 11 else    
 12         echo "the argument is too small "
 13 fi      
~      
Loong:/home/yee/shell/test# sh  mul.sh 30
30 * 1 = 30 
30 * 2 = 60 
30 * 3 = 90 
30 * 4 = 120 
30 * 5 = 150 
30 * 6 = 180 
30 * 7 = 210 
30 * 8 = 240 
30 * 9 = 270 
30 * 10 = 300 
Loong:/home/yee/shell/test# sh  mul.sh 14
the argument is too small 
Loong:/home/yee/shell/test#      

6.編寫一個名為move的腳本程式,格式move  <file1>  <file2>。如果file1不存在,給出提示;否則移動file1至file2。

1 #!/bin/bash
  2 
  3 if [ ! -f file1 ]
  4 then
  5         echo "the file1 doesnot exist !"
  6 else
  7         mv file1 file2
  8 fi      
Loong:/home/yee/shell/test# sh mv.sh 
the file1 doesnot exist !
Loong:/home/yee/shell/test# touch file1
Loong:/home/yee/shell/test# sh -x mv.sh 
+ '[' '!' -f file1 ']'
+ mv file1 file2
Loong:/home/yee/shell/test#      

7.編寫一個shell腳本,能夠顯示下面序列的前25個數字。0,1,1,2,3,5,8,13…,前二個數字之和為第三個數字,即著名的Fibonacci序列。

1 #!/bin/bash
  2 
  3 n=0
  4 m=1
  5 num=2
  6 total=1
  7 while [ $num -lt 25 ]
  8 do
  9         total=`expr $n + $m`
 10         echo "$total= $n + $m"
 11         num=`expr $num + 1`
 12         n=$m
 13         m=$total
 14 done      
Loong:/home/yee/shell/test# sh fibonacci.sh 
1= 0 + 1
2= 1 + 1
3= 1 + 2
5= 2 + 3
8= 3 + 5
13= 5 + 8
21= 8 + 13
34= 13 + 21
55= 21 + 34
89= 34 + 55
144= 55 + 89
233= 89 + 144
377= 144 + 233
610= 233 + 377
987= 377 + 610
1597= 610 + 987
2584= 987 + 1597
4181= 1597 + 2584
6765= 2584 + 4181
10946= 4181 + 6765
17711= 6765 + 10946
28657= 10946 + 17711
46368= 17711 + 28657
Loong:/home/yee/shell/test#      

8.編寫一個名為square的腳本程式,參數為一大于10的正整數。先檢查參數是否符合要求。如果不符合要求,請給出提示;如果符合要求,輸出從1到該正整數的平方值。

1 #!/bin/bash
  2 
  3 num=0
  4 if [ $1 -gt 10 ]
  5 then
  6         for((i=1;i<=$1;i++))
  7         do
  8                 num=`expr $i \* $i`
  9                 echo "$i * $i = $num "
 10         done
 11 else
 12         echo "the argument is too small "
 13 fi      
Loong:/home/yee/shell/test# sh square.sh 9
the argument is too small 
Loong:/home/yee/shell/test# sh square.sh 14
1 * 1 = 1 
2 * 2 = 4 
3 * 3 = 9 
4 * 4 = 16 
5 * 5 = 25 
6 * 6 = 36 
7 * 7 = 49 
8 * 8 = 64 
9 * 9 = 81 
10 * 10 = 100 
11 * 11 = 121 
12 * 12 = 144 
13 * 13 = 169 
14 * 14 = 196 
Loong:/home/yee/shell/test#      

繼續閱讀