第七章 linux系統(tǒng)用戶以及用戶組管理
【認(rèn)識(shí)/etc/passwd和/etc/shadow】
這兩個(gè)文件可以說是linux系統(tǒng)中最重要的文件之一。如果沒有這兩個(gè)文件或者這兩個(gè)文件出問題,則你是無法正常登錄linux系統(tǒng)的。
![[image]](http://www.92csz.com/study/linux/images/7_1.png)
/etc/passwd由’:’分割成7個(gè)字段,每個(gè)字段的具體含義是:
1)用戶名(如第一行中的root就是用戶名),代表用戶賬號(hào)的字符串。用戶名字符可以是大小寫字母、數(shù)字、減號(hào)(不能出現(xiàn)在首位)、點(diǎn)以及下劃線,其他字符不合法。雖然用戶名中可以出現(xiàn)點(diǎn),但不建議使用,尤其是首位為點(diǎn)時(shí),另外減號(hào)也不建議使用,因?yàn)槿菀自斐苫煜?/span>。
2)存放的就是該賬號(hào)的口令,為什么是’x’呢?早期的unix系統(tǒng)口令確實(shí)是存放在這里,但基于安全因素,后來就將其存放到/etc/shadow中了,在這里只用一個(gè)’x’代替。
3)這個(gè)數(shù)字代表用戶標(biāo)識(shí)號(hào),也叫做uid。系統(tǒng)識(shí)別用戶身份就是通過這個(gè)數(shù)字來的,0就是root,也就是說你可以修改test用戶的uid為0,那么系統(tǒng)會(huì)認(rèn)為root和test為同一個(gè)賬戶。通常uid的取值范圍是0~65535,0是超級(jí)用戶(root)的標(biāo)識(shí)號(hào),1~499由系統(tǒng)保留,作為管理賬號(hào),普通用戶的標(biāo)識(shí)號(hào)從500開始,如果我們自定義建立一個(gè)普通用戶,你會(huì)看到該賬戶的標(biāo)識(shí)號(hào)是大于或等于500的。
4)表示組標(biāo)識(shí)號(hào),也叫做gid。這個(gè)字段對(duì)應(yīng)著/etc/group 中的一條記錄,其實(shí)/etc/group和/etc/passwd基本上類似。
5)注釋說明,該字段沒有實(shí)際意義,通常記錄該用戶的一些屬性,例如姓名、電話、地址等等。不過,當(dāng)你使用finger的功能時(shí)就會(huì)顯示這些信息的(稍后做介紹)。
6)用戶的家目錄,當(dāng)用戶登錄時(shí)就處在這個(gè)目錄下。root的家目錄是/root,普通用戶的家目錄則為/home/username,這個(gè)字段是可以自定義的,比如你建立一個(gè)普通用戶test1,要想讓test1的家目錄在/data目錄下,只要修改/etc/passwd文件中test1那行中的該字段為/data即可。
7)shell,用戶登錄后要啟動(dòng)一個(gè)進(jìn)程,用來將用戶下達(dá)的指令傳給內(nèi)核,這就是shell。Linux的shell有很多種sh, csh, ksh, tcsh, bash等,而Redhat/CentOS的shell就是bash。查看/etc/passwd文件,該字段中除了/bin/bash外還有/sbin/nologin比較多,它表示不允許該賬號(hào)登錄。如果你想建立一個(gè)賬號(hào)不讓他登錄,那么就可以把該字段改成/sbin/nologin,默認(rèn)是/bin/bash。
![[image]](http://www.92csz.com/study/linux/images/7_12.png)
再來看看/etc/shadow這個(gè)文件,和/etc/passwd類似,用”:”分割成9個(gè)字段。
1)用戶名,跟/etc/passwd對(duì)應(yīng)。
2)用戶密碼,這個(gè)才是該賬號(hào)的真正的密碼,不過這個(gè)密碼已經(jīng)加密過了,但是有些黑客還是能夠解密的。所以為了安全,該文件屬性設(shè)置為600,只允許root讀寫。
3)上次更改密碼的日期,這個(gè)數(shù)字是這樣計(jì)算得來的,距離1970年1月1日到上次更改密碼的日期,例如上次更改密碼的日期為2012年1月1日,則這個(gè)值就是365*(2012-1970)+1=15331。
4)要過多少天才可以更改密碼,默認(rèn)是0,即不限制。
5)密碼多少天后到期。即在多少天內(nèi)必須更改密碼,例如這里設(shè)置成30,則30天內(nèi)必須更改一次密碼,否則將不能登錄系統(tǒng),默認(rèn)是99999,可以理解為永遠(yuǎn)不需要改。
6)密碼到期前的警告期限,若這個(gè)值設(shè)置成7,則表示當(dāng)7天后密碼過期時(shí),系統(tǒng)就發(fā)出警告告訴用戶,提醒用戶他的密碼將在7天后到期。
7)賬號(hào)失效期限。你可以這樣理解,如果設(shè)置這個(gè)值為3,則表示:密碼已經(jīng)到期,然而用戶并沒有在到期前修改密碼,那么再過3天,則這個(gè)賬號(hào)就失效了,即鎖定了。
8)賬號(hào)的生命周期,跟第三段一樣,是按距離1970年1月1日多少天算的。它表示的含義是,賬號(hào)在這個(gè)日期前可以使用,到期后賬號(hào)作廢。
9)作為保留用的,沒有什么意義。
【新增/刪除用戶和用戶組】
a. 新增一個(gè)組 groupadd [-g GID] groupname
![[image]](http://www.92csz.com/study/linux/images/7_13.png)
不加-g 則按照系統(tǒng)默認(rèn)的gid創(chuàng)建組,跟用戶一樣,gid也是從500開始的
![[image]](http://www.92csz.com/study/linux/images/7_14.png)
-g選項(xiàng)可以自定義gid
b. 刪除組 gropudel groupname
![[image]](http://www.92csz.com/study/linux/images/7_15.png)
沒有特殊選項(xiàng)。
c. 增加用戶 useradd [-u UID] [-g GID] [-d HOME] [-M] [-s]
-u 自定義UID
-g 使其屬于已經(jīng)存在的某個(gè)GID
-d 自定義用戶的家目錄
-M 不建立家目錄
-s 自定義shell
![[image]](http://www.92csz.com/study/linux/images/7_16.png)
你會(huì)發(fā)現(xiàn),創(chuàng)建test11時(shí),加上了-M選項(xiàng)后,在/etc/passwd文件中test11那行的第六字段依然有/home/test11,可是ls查看該目錄時(shí),會(huì)提示該目錄不存在。
![[image]](http://www.92csz.com/study/linux/images/7_17.png)
-M選項(xiàng)的作用就是不創(chuàng)建用戶的家目錄。
-d. 刪除用戶 userdel [-r] username
![[image]](http://www.92csz.com/study/linux/images/7_18.png)
-r 選項(xiàng)的作用是刪除用戶時(shí),連同用戶的家目錄一起刪除。
【chfn 更改用戶的finger (不常用)】
前面內(nèi)容中提到了findger,即在/etc/passwd文件中的第5個(gè)字段中所顯示的信息,那么如何去設(shè)定這個(gè)信息呢?
![[image]](http://www.92csz.com/study/linux/images/7_19.png)
就是chfn這個(gè)命令了。修改完后,就會(huì)在/etc/passwd文件中的test的那一行第五個(gè)字段中看到相關(guān)信息了,默認(rèn)是空的。
【創(chuàng)建/修改一個(gè)用戶的密碼 “passwd [username]”】
等創(chuàng)建完賬戶后,默認(rèn)是沒有設(shè)置密碼的,雖然沒有密碼,但該賬戶同樣登錄不了系統(tǒng)。只有設(shè)置好密碼后方可登錄系統(tǒng)。
為用戶創(chuàng)建密碼時(shí),為了安全起見,請(qǐng)盡量設(shè)置復(fù)雜一些。你可以按照這樣的規(guī)則來設(shè)置密碼:a. 長度大于10個(gè)字符;b. 密碼中包含大小寫字母數(shù)字以及特殊字符(*&等);c. 不規(guī)則性(不要出現(xiàn)root, happy, love, linux, 123456, 111111等等單詞或者數(shù)字);d. 不要帶有自己名字、公司名字、自己電話、自己生日等。
![[image]](http://www.92csz.com/study/linux/images/7_20.png)
passwd 后面不跟用戶名則是更改當(dāng)前用戶的密碼,當(dāng)前用戶為root,所以此時(shí)修改的是root的密碼,后面跟test則修改的是test的密碼。
【用戶身份切換】
Linux系統(tǒng)中,有時(shí)候普通用戶有些事情是不能做的,除非是root用戶才能做到。這時(shí)就需要臨時(shí)切換到root身份來做事了。
![[image]](http://www.92csz.com/study/linux/images/7_21.png)
用test賬號(hào)登錄linux系統(tǒng),然后使用su - 就可以切換成root身份,前提是知道root的密碼。
![[image]](http://www.92csz.com/study/linux/images/7_29.png)
你可以使用echo $LOGNAME來查看當(dāng)前登錄的用戶名
![[image]](http://www.92csz.com/study/linux/images/7_30.png)
su 的語法為: su [-] username
后面可以跟”-”也可以不跟,普通用戶su不加username時(shí)就是切換到root用戶,當(dāng)然root用戶同樣可以su到普通用戶。
![[image]](http://www.92csz.com/study/linux/images/7_31.png)
加”-“后會(huì)連同用戶的環(huán)境變量一起切換過來。su test 后雖然切換到了test用戶,但是當(dāng)前目錄還是切換前的/root目錄,然后當(dāng)用su - test時(shí)切換用戶后則到了test的家目錄/home/test。當(dāng)用root切換普通用戶時(shí),是不需要輸入密碼的。這也體現(xiàn)了root用戶至高無上的權(quán)利。
用su是可以切換用戶身份,如果每個(gè)普通用戶都能切換到root身份,如果某個(gè)用戶不小心泄漏了root的密碼,那豈不是系統(tǒng)非常的不安全?沒有錯(cuò),為了改進(jìn)這個(gè)問題,產(chǎn)生了sudo這個(gè)命令。使用sudo執(zhí)行一個(gè)root才能執(zhí)行的命令是可以辦到的,但是需要輸入密碼,這個(gè)密碼并不是root的密碼而是用戶自己的密碼。默認(rèn)只有root用戶能使用sudo命令,普通用戶想要使用sudo,是需要root預(yù)先設(shè)定的,即,使用visudo命令去編輯相關(guān)的配置文件/etc/sudoers。如果沒有visudo這個(gè)命令,請(qǐng)使用” yum install -y sudo”安裝。
![[image]](http://www.92csz.com/study/linux/images/7_32.png)
默認(rèn)root能夠sudo是因?yàn)檫@個(gè)文件中有一行” root ALL=(ALL) ALL” 在該行下面加入” test ALL=(ALL) ALL”就可以讓test用戶擁有了sudo的權(quán)利。如果每增加一用戶就設(shè)置一行,這樣太麻煩了。所以你可以這樣設(shè)置。
![[image]](http://www.92csz.com/study/linux/images/7_33.png)
把這一行前面的”#”去掉,讓這一行生效。它的意思是,wheel這個(gè)組的所有用戶都擁有了sudo的權(quán)利。接下來就需要你把想讓有sudo權(quán)利的所有用戶加入到wheel這個(gè)組中即可。
![[image]](http://www.92csz.com/study/linux/images/7_34.png)
你不妨跟筆者試一下,非常有意思。
http://blog.sina.com.cn/s/blog_5038ce7a0100qfyj.html
Thinkpad T410 安裝 Ubuntu10.04 (2011-03-06 14:43:06)轉(zhuǎn)載▼
標(biāo)簽: linux ubuntu 10.04 thinkpadt410 雜談 分類: Linux
Thinkpad T410 下安裝 Ubuntu10.04
移動(dòng)硬盤上安裝Ubuntu備忘
========================================
1. 系統(tǒng)安裝
========================================
下載安裝鏡像一步步裝即可,沒有太多復(fù)雜的,安裝完后系統(tǒng)后:
******************************
1.1 更改源
******************************
編輯/etc/apt/sources.list 文件,網(wǎng)上有好多源,比如163,骨頭源等,注意先把官方默認(rèn)的源備份一下
sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
以便日后要用。網(wǎng)絡(luò)不受限制的話默認(rèn)的源速度就可以了。
******************************
1.2 更新語文支持
******************************
更新語文支持,把oOo什么的漢化
******************************
1.3 更新系統(tǒng)
******************************
用更新管理器更新系統(tǒng)內(nèi)核,模塊,軟件等
========================================
2. 安裝必要驅(qū)動(dòng)
========================================
ubuntu10.04 對(duì) ThinkpadT410 大部分硬件支持良好,但無線網(wǎng)絡(luò)能發(fā)現(xiàn)卻不能用
******************************
2.1 無線網(wǎng)卡驅(qū)動(dòng)安裝
******************************
參考網(wǎng)址:http://www.realtek.com.tw/downloads/
T410無線網(wǎng)卡型號(hào):
* Chipset: Realtek RTL8192SE
* IEEE Standards: 802.11b, 802.11g, 802.11n
* 1x2 MIMO
* PCI ID: 10ec:8172
到realtek 官網(wǎng)下載RTL8192SE對(duì)應(yīng)的linux驅(qū)動(dòng),之后安裝重啟就可以了。安裝過程見安裝包中的readme.txt,很簡單。
******************************
2.2 安裝電源軟件和硬盤保護(hù)
******************************
參考文獻(xiàn):http://www.thinkwiki.org/wiki/Tp_smapi
到thinkwiki上下載安裝tp_smapi,安裝過程可參考網(wǎng)頁說明,可從源碼安裝,對(duì)ubuntu10.04,也可用apt-get install安裝。
源碼安裝方式:
# tar xzvf tp_smapi-0.40.tgz
# cd tp_smapi-0.40
# make install
如果你使用HDAPS驅(qū)動(dòng)(HDAPS - IBM Active Protection System Linux Driver),因?yàn)閠p_smapi已經(jīng)patch了與其相容的模塊,所以安裝方式改為:
# make install HDAPS=1
按官方的說明下述安裝不是必須的,hdaps已集成在tp_smapi中,但裝一下也無妨
sudo apt-get install hdaps-utils hdapsd
查看模塊是否運(yùn)行:
lsmod | grep hdaps
對(duì)于ubuntu10.04,可按如下安裝:
# aptitude install tp-smapi-dkms
# modprobe tp_smapi
設(shè)置充電閾值:
# echo 40 > /sys/devices/platform/smapi/BAT0/start_charge_thresh
# echo 70 > /sys/devices/platform/smapi/BAT0/stop_charge_thresh
# cat /sys/devices/platform/smapi/BAT0/*_charge_thresh
注意:運(yùn)行以上命令時(shí)即使用sudo,也可能提示權(quán)限不夠,可先執(zhí)行: sudo -i
其它設(shè)置:
17 分鐘后禁止充電(不論充電閾值為多少):
# echo 17 > /sys/devices/platform/smapi/BAT0/inhibit_charge_minutes
取消充電保護(hù):
# echo 0 > /sys/devices/platform/smapi/BAT0/inhibit_charge_minutes
強(qiáng)制停止充電(即使連接了AC):
# echo 1 > /sys/devices/platform/smapi/BAT0/force_discharge
取消強(qiáng)制停止充電:
# echo 0 > /sys/devices/platform/smapi/BAT0/force_discharge
開機(jī)時(shí)自動(dòng)加載官方方法:
it is not necessary to create your own init.d script or modify /etc/rc.local - you just need the package sysfsutils:
# apt-get install sysfsutils
# sudo gedit /etc/sysfs.conf
修改文件 /etc/sysfs.conf 即可:
# cat >> /etc/sysfs.conf << EOF
# For a LiIon battery in a Thinkpad
devices/platform/smapi/BAT0/start_charge_thresh = 50
devices/platform/smapi/BAT0/stop_charge_thresh = 80
EOF
開機(jī)時(shí)自動(dòng)加載其它方法:
增加開機(jī)啟動(dòng)模塊
sudo gedit /etc/modules
添加以下內(nèi)容:
thinkpad_ec
tp_smapi
hdaps
另外編輯文件:
sudo gedit /etc/rc.local
加入:
# Make battery live longer
echo 50 > /sys/devices/platform/smapi/BAT0/start_charge_thresh
echo 80 > /sys/devices/platform/smapi/BAT0/stop_charge_thresh
上面方法未經(jīng)試驗(yàn),還是用官方方法好
問題:重啟后在platform下沒有發(fā)現(xiàn)smpi文件夾,在源碼包里執(zhí)行make load HDAPS=1后方出現(xiàn),是否需在啟動(dòng)模塊中加載?已解決,見上。
******************************
2.3 顯卡驅(qū)動(dòng)
******************************
注意:不要更新顯卡驅(qū)動(dòng),用開源驅(qū)動(dòng)即可,更新后可能會(huì)出現(xiàn)一系列的問題,如開機(jī)畫面LOGO分辨率,亮度調(diào)節(jié)等,并且感覺看字的時(shí)候分辨率不對(duì)了,網(wǎng)上的解決方案不是很完美,有好幾次按網(wǎng)上的更改后直接黑屏進(jìn)不去系統(tǒng)了,默認(rèn)驅(qū)動(dòng)除了不能開啟特效外也沒什么不好的,再說特效這東西是給別看的,不用無所謂。官方還沒有 NVS 3100M 相對(duì)應(yīng)的顯卡程序,所以還是等等吧。
經(jīng)后來測(cè)試可以裝,但得做一些設(shè)置,查看博文: http://blog.sina.com.cn/s/blog_5038ce7a0100r8lf.html
2.3.1 調(diào)節(jié)屏幕亮度方法:
A:(快捷的方法,臨時(shí)的)
a. 按Ctrl+Alt+F1進(jìn)入命令行
b. 用快捷鍵調(diào)節(jié)亮度
c. 按Ctrl+Alt+F7回到GUI
B:nvclock
sudo apt-get install nvclock
nvclock -S -10
C:/etc/X11/xorg.conf
添加Option "RegistryDwords" "EnableBrightnessControl=1"
2.3.2 開關(guān)機(jī)分辨率:
A. 確認(rèn)grub和plymouth的系統(tǒng)配置文件是原始狀態(tài),沒有應(yīng)用過其它的修改方案。
B. 安裝v86d和hwinfo包
sudo apt-get install v86d hwinfo
C. 查看自己顯卡支持的分辨率:
sudo hwinfo --framebuffer
顯示的結(jié)果中沒有支持的最大分辨率1440x900,但下面設(shè)置了是可以啟用的。
D. 修改/etc/default/grub文件,使用uvesafb的framebuffer來啟動(dòng)
sudo gedit /etc/default/grub
把下面的兩行:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
#GRUB_GFXMODE=640x480
修改為:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash nomodeset video=uvesafb:mode_option=1440x900-24,mtrr=3,scroll=ywrap"
GRUB_GFXMODE=1440x900
E. 修改/etc/initramfs-tools/modules文件
sudo gedit /etc/initramfs-tools/modules
添加下面這行:
uvesafb mode_option=1440x900-24 mtrr=3 scroll=ywrap
F. 運(yùn)行下面的命令,啟用framebuffer:
代碼:
echo FRAMEBUFFER=y | sudo tee /etc/initramfs-tools/conf.d/splash
G. 運(yùn)行下面的命令,更新grub配置文件:
sudo update-grub
sudo update-initramfs -u
H. 重啟,開關(guān)機(jī)分辨率,tty1-tty6分辨率問題均得到解決。
******************************
2.4 安裝小紅點(diǎn)驅(qū)動(dòng)
******************************
sudo apt-get install gpointing-device-settings
******************************
2.5 安裝thinkfan
******************************
打開thingkpad_acpi的風(fēng)扇控制
首先要打開thinpad_acpi的風(fēng)扇控制支持,具體做法是在加載這個(gè)內(nèi)核模塊的時(shí)候給它傳遞一
個(gè)打開風(fēng)扇控制的變量。具體做法如下:
modprobe thinkpad_acpi fan_control=1 experimental=1
要實(shí)現(xiàn)開機(jī)加載模塊時(shí)設(shè)置,則需要在/etc/modprobe.d/下增加一個(gè)配置文件thinkpad-acpi.conf,內(nèi)容:
options thinkpad_acpi experimental=1 fan_control=1
安裝ubuntu軟件倉庫里面的thinkfan和sysfsutils(讀取和設(shè)置sysfs屬性)。
sudo aptitude install thinkfan sysfsutils
thinkfan會(huì)在系統(tǒng)的運(yùn)行級(jí)別中添加thinkfan的后臺(tái)服務(wù),這樣就可實(shí)現(xiàn)開機(jī)運(yùn)行。
配置和激活thinkfan
閱讀/etc/thinkfan.conf可以知道它是通過thinkpad_acpi提供的風(fēng)扇控制接口來實(shí)現(xiàn)風(fēng)扇控制的,它給風(fēng)扇的轉(zhuǎn)速級(jí)別對(duì)應(yīng)了一個(gè)上下限溫度,在這個(gè)上下限內(nèi)就設(shè)置對(duì)應(yīng)的轉(zhuǎn)速。
安裝后發(fā)現(xiàn)thinkfan并沒有直接生效,
腳本:/etc/init.d/thinkfan 查看怎樣設(shè)置自動(dòng)啟動(dòng)
自動(dòng)啟動(dòng):/etc/default/thinkfan 文件中的START=yes。
風(fēng)扇控制溫度輸入為:/proc/acpi/ibm/thermal
cat /proc/acpi/ibm/fan可以得到控制風(fēng)扇的方法
安裝完成后手動(dòng)更新下載后的源碼版本:0.7.1
sudo make
將開機(jī)自動(dòng)運(yùn)行關(guān)閉,重啟后進(jìn)入源碼文件夾,執(zhí)行
sudo cp thinkfan /usr/sbin
然后運(yùn)行thinkfan, 并將開機(jī)自運(yùn)行打開。
手動(dòng)更改風(fēng)扇轉(zhuǎn)速:
sudo -i
echo "level 6" > /proc/acpi/ibm/fan
但過幾秒鐘后會(huì)變?yōu)樽詣?dòng)控制。
重新加載配置文件
# kill -HUP $(</var/run/thinkfan.pid)
卸載:
sudo aptitude remove thinkfan,注意不要把sysfsutils也卸載了
rm /etc/modprobe.d/thinkpad-acpi.conf
其余配置文件可暫時(shí)保留
參考網(wǎng)址: http://vmlinz.is-programmer.com/posts/25834.html
http://sourceforge.net/projects/thinkfan/
http://www.thinkwiki.org/wiki/Fan_control_scripts
******************************
2.6 配置laptop-model
******************************
sudo apt-get install laptop-mode-tools
修改文件: /etc/laptop-mode/laptop-mode.conf
ENABLE_LAPTOP_MODE_ON_AC=1
ENABLE_LAPTOP_MODE_WHEN_LID_CLOSED=1
CONTROL_HD_IDLE_TIMEOUT=0
# ArchLinux/Debian 默認(rèn) 20 ,導(dǎo)致硬盤經(jīng)常停轉(zhuǎn)起轉(zhuǎn),Ubuntu 默認(rèn) 60 ,用了幾天沒感覺??傊谊P(guān)掉了這項(xiàng)功能。
即在使用交流電源和合上蓋時(shí)啟用LatTopMode,關(guān)于LapTopMode信息查看
http://hi.baidu.com/skubuntu/blog/item/c70ad563e1bf93d38db10d07.html
另外可解決發(fā)熱大的問題:
http://www.cnblogs.com/droi/archive/2011/01/20/1940300.html
******************************
2.7 安裝sensors
******************************
參考文獻(xiàn):http://forum.ubuntu.org.cn/viewtopic.php?t=55438
sudo apt-get install lm-sensors sensors-applet
sudo sensors-detect 并一路YES 回車
找到確認(rèn)添加的模塊
sudo gedit /etc/modules 添加進(jìn)去,但我的沒有找到
安裝界面顯示程序:
sudo apt-get install xsensors
sudo apt-get install gdesklets gkrellm
安裝硬盤溫度監(jiān)控:
sudo apt-get install hddtemp
******************************
2.8 安裝cpufrequtils
******************************
因?yàn)閁buntu 默認(rèn)的 CPU 式作方式為ondemand,這樣只要一有 cpu 計(jì)算量的任務(wù),就會(huì)立即達(dá)到最大頻率運(yùn)行,等執(zhí)行完后立即回到最低頻率,因此會(huì)出現(xiàn) cpu 溫度的急驟升高,如果采用 tinkfan 控制風(fēng)扇轉(zhuǎn)速,則風(fēng)扇會(huì)再現(xiàn)突然猛轉(zhuǎn)的情況。右鍵單擊面板,選擇“添加到面板”,里面找到“cpu頻率范圍監(jiān)視器”, cpu 有幾個(gè)核就加幾個(gè),單擊可改變 cpu 工作模式,幾種工作模式分別如下:
powersave,是無論如何都只會(huì)保持最低頻率的所謂“省電”模式;
ondemand,默認(rèn)模式.一有cpu計(jì)算量的任務(wù),就會(huì)立即達(dá)到最大頻率運(yùn)行,等執(zhí)行完畢就立即回到最低頻率;
conservative,翻譯成保守(中庸)模式,會(huì)自動(dòng)在頻率上下限調(diào)整,和ondemand的區(qū)別在于它會(huì)按需分配頻率,而不是一味追求最高頻率;
performance,顧名思義只注重效率,無論如何一直保持以最大頻率運(yùn)行.
將四個(gè)核均改為 conservative 后,cpu 溫度降到了 46 度左右,比以前有大大降低。
另外,安裝:
sudo apt-get install cpufrequtils
用法:
代碼:cpufreq-info, cpufreq-selector, cpufreq-set
具體用法可以用 -h 或 man 查看。
A. 查看cpu類型、當(dāng)前頻率、支持頻率、運(yùn)行模式等
cpufreq-info
B. 選擇需要調(diào)整的cpu
sudo cpufreq-selector -c cpu號(hào)
sudo cpufreq-set -c cpu號(hào)
C. 調(diào)整cpu頻率
sudo cpufreq-selector -f 你所需要的頻率
sudo cpufreq-set -f 你所需要的頻率
注意,此處的頻率必須是以KHz為單位,并且是可以達(dá)到的頻率(也就是用cpufreq-info查看到的各個(gè)頻率),cpu頻率=倍頻x外頻。以下凡是涉及頻率的一律如此。
D. 調(diào)整cpu頻率上下限
sudo cpufreq-set -d 頻率下限
sudo cpufreq-set -u 頻率上限
因此,可能可以對(duì)cpu進(jìn)行降頻或者超頻
E. 調(diào)整cpu運(yùn)行模式
sudo cpufreq-selecotr -g 模式
sudo cpufreq-set -g 模式
eg: sudo cpufreq-set -c 0 -g powersave
這里,模式就是執(zhí)行cpufreq-info后看到的所支持的模式。比如我的支持以下幾種:powersave, userspace, ondemand, conservative, performance。
powersave,是無論如何都只會(huì)保持最低頻率的所謂“省電”模式;
userspace,是自定義頻率時(shí)的模式,這個(gè)是當(dāng)你設(shè)定特定頻率時(shí)自動(dòng)轉(zhuǎn)變的;
ondemand,默認(rèn)模式。一有cpu計(jì)算量的任務(wù),就會(huì)立即達(dá)到最大頻率運(yùn)行,等執(zhí)行完畢就立即回到最低頻率;
conservative,翻譯成保守(中庸)模式,會(huì)自動(dòng)在頻率上下限調(diào)整,和ondemand的區(qū)別在于它會(huì)按需分配頻率,而不是一味追求最高頻率;
performance,顧名思義只注重效率,無論如何一直保持以最大頻率運(yùn)行。
F. 添加cpu監(jiān)視器
監(jiān)視cpu頻率,右鍵單擊面板,選擇“添加到面板”,里面找到“cpu頻率范圍監(jiān)視器”。
另外,如果嫌命令行麻煩,可以這么做
sudo chmod +s /usr/bin/cpufreq-selector
然后,cpu頻率范圍監(jiān)視器的首選項(xiàng)里會(huì)多出一個(gè)“頻率選擇器”,顯示菜單選擇“頻率和調(diào)速器”。鼠標(biāo)左鍵單擊cpu頻率范圍監(jiān)視器,會(huì)發(fā)現(xiàn)“頻率”和“調(diào)速器”兩個(gè)菜單,就可以隨便調(diào)了。當(dāng)然不需要隨便調(diào),這里只是說明一下方法。
添加開機(jī)自啟動(dòng):修改/etc/rc.local 文件,在 exit 0 前面增加如下內(nèi)容:
cpufreq-set -c 0 -g conservative
cpufreq-set -c 1 -g conservative
cpufreq-set -c 2 -g conservative
cpufreq-set -c 3 -g conservative
但開機(jī)后不久會(huì)自動(dòng)改為ondemand, 這時(shí)再運(yùn)行桌面腳本即可
laptop-mode 下的配置文件位置:
/etc/laptop-mode/conf.d/cpufreq.conf
可更改是否啟用及CPU工作模式。
更改后發(fā)現(xiàn)啟動(dòng)后頻率監(jiān)視器上仍為ondemand, 但cpu溫度降低了,是不是在內(nèi)部自己調(diào)整的,因?yàn)閱⒂昧薬c-laptopmod
========================================
3. 主要應(yīng)用軟件安裝
========================================
******************************
3.1 安裝方式
******************************
sudo apt-get install ...
軟件中心
新立得軟件包
******************************
3.2 主要軟件列表
******************************
ubuntu-restricted-extras 其他Ubuntu應(yīng)用程序可能需要的軟件或者編碼器
audacious 注:需裝音頻解碼器(打開文件自動(dòng)安裝),亂碼問題:播放列表,自定義:%f
mplayer 注:需裝解碼器
stardict
發(fā)音不好,干脆卸載了:
sudo apt-get install stardict-plugin-espeak stardict-plugin-festival
goldendict 注:推薦,有好多詞典
g++
vim 注:用源碼編譯時(shí)gvim 有問題。另screen下不同標(biāo)簽間的"*"寄存器可用,太好了
cscope
ctags
cgdb
rar unrar p7zip
flashplayer
fonts 注:將Win下的TrueType字體放入$HOME/.fonts目錄即可,也可打開安裝
xrgsu->mentohust
scilab
octave
pdf comment: xournal,okular
FoxitReader
texlive
OpenFOAM 注:源碼編譯,安裝方式見別一文檔
gnuplot 注:畫圖程序,octave用的就是她
octave
noflushd 注:硬盤控制程序
ailurus 注:推薦
Jabref
Bibus
========================================
4. 主要問題
========================================
******************************
4.1 中文支持
******************************
pdf中文支持:sudo apt-get install xpdf-chinese-simplified xpdf-chinese-traditional poppler-data
gedit亂碼:運(yùn)行 gconf-editor,在/apps/gedit-2/preferences/encodings/auto_detected 下面加入 GB18030,GB2312 ,GBK( GB18030 兼容GB2312 ,GBK的,所以只添加有一個(gè) GB18030就可以了的)
******************************
4.2 Chrome中一旦退格IBus無法輸入
******************************
一旦按了backspace刪除之前輸入的文字,就要重新啟動(dòng)ibus才能輸入,不然輸入什么都是不能顯示的,
已經(jīng)證實(shí)是iBus的key snooper特性導(dǎo)致了這個(gè)問題??梢钥紤]將iBus升級(jí)到1.3.5以上(添加PPA)
$sudo add-apt-repository ppa:shawn-p-huang/ppa
$sudo apt-get update
$sudo apt-get dist-upgrade
******************************
4.3 更改密碼問題
******************************
密碼太短或太簡單,用sudo更改:
sudo passwd terry
******************************
4.4 面版重置
******************************
打開終端,終端窗口打開之后,立即在提示符后面輸入下列命令: gconftool --recursive-unset /apps/panel (注意:每個(gè)斜杠 “/” 后面沒有空格)接下來輸入下列命令: rm -rf ~/.gconf/apps/panel 最后還需要執(zhí)行: pkill gnome-panel 接下來,Ubuntu 默認(rèn)的上下兩欄面板就會(huì)恢復(fù)默認(rèn)值,出現(xiàn)頂部+底部各一面板的默認(rèn)布局。
******************************
4.5. 安裝出錯(cuò)
******************************
dpkg:處理 tex-common (--configure)時(shí)出錯(cuò):
子進(jìn)程 已安裝的 post-installation 腳本 返回了錯(cuò)誤號(hào) 1
dpkg:依賴關(guān)系問題使得 texlive-binaries 的配置工作不能繼續(xù):
texlive-binaries 依賴于 tex-common (>= 2.00);然而:
軟件包 tex-common 還沒有被配置。
dpkg:處理 texlive-binaries (--configure)時(shí)出錯(cuò):
依賴關(guān)系問題 - 仍未被配置
在處理時(shí)有錯(cuò)誤發(fā)生:
tex-common
texlive-binaries
解決辦法: 直接去掉那兩個(gè)包
******************************
4.6 時(shí)區(qū)設(shè)置問題
******************************
解決linux與windows的時(shí)間同步問題
sudo vim /etc/default/rcS
設(shè)置 UTC=no
******************************
4.7 修改默認(rèn)打開程序
******************************
這個(gè)問題跟兩個(gè)文件有關(guān):
/etc/gnome/defaults.list (這個(gè)文件保存了全局的打開方式)
~/.local/share/applications/mimeapps.list (這個(gè)文件保存了個(gè)人的打開方式)
建議只更改當(dāng)前用戶的配置文件
也可用在Nautilus中選擇屬性,從打開方式標(biāo)簽更改
******************************
4.8 相關(guān)命令
******************************
查看uvesafb支持的分辨率:/sys/bus/platform/drivers/uvesafb/uvesafb.0/vbe_modes
關(guān)于framebuffer的說明可以查看Gentoo文檔,另外/usr/src/linux/Documentation/fb/uvesafb.txt列出了所有變量
/etc/X11/xorg.conf可查看顯卡驅(qū)動(dòng)及相關(guān)信息
讓硬盤進(jìn)入休眠:sudo hdparm -Y /dev/sda
七、Dockerfile
Docker 可以通過 Dockerfile 的內(nèi)容來自動(dòng)構(gòu)建鏡像。Dockerfile 是一個(gè)包含創(chuàng)建鏡像所有命令的文本文件,通過docker build命令可以根據(jù) Dockerfile 的內(nèi)容構(gòu)建鏡像,在介紹如何構(gòu)建之前先介紹下 Dockerfile 的基本語法結(jié)構(gòu)。
Dockerfile 有以下指令選項(xiàng):
- FROM
- MAINTAINER
- RUN
- CMD
- EXPOSE
- ENV
- ADD
- COPY
- ENTRYPOINT
- VOLUME
- USER
- WORKDIR
- ONBUILD
7.1 FROM
用法:
或者
- FROM指定構(gòu)建鏡像的基礎(chǔ)源鏡像,如果本地沒有指定的鏡像,則會(huì)自動(dòng)從 Docker 的公共庫 pull 鏡像下來。
- FROM必須是 Dockerfile 中非注釋行的第一個(gè)指令,即一個(gè) Dockerfile 從FROM語句開始。
- FROM可以在一個(gè) Dockerfile 中出現(xiàn)多次,如果有需求在一個(gè) Dockerfile 中創(chuàng)建多個(gè)鏡像。
- 如果FROM語句沒有指定鏡像標(biāo)簽,則默認(rèn)使用latest標(biāo)簽。
7.2 MAINTAINER
用法:
指定創(chuàng)建鏡像的用戶
RUN 有兩種使用方式
每條RUN指令將在當(dāng)前鏡像基礎(chǔ)上執(zhí)行指定命令,并提交為新的鏡像,后續(xù)的RUN都在之前RUN提交后的鏡像為基礎(chǔ),鏡像是分層的,可以通過一個(gè)鏡像的任何一個(gè)歷史提交點(diǎn)來創(chuàng)建,類似源碼的版本控制。
exec 方式會(huì)被解析為一個(gè) JSON 數(shù)組,所以必須使用雙引號(hào)而不是單引號(hào)。exec 方式不會(huì)調(diào)用一個(gè)命令 shell,所以也就不會(huì)繼承相應(yīng)的變量,如:
這種方式是不會(huì)達(dá)到輸出 HOME 變量的,正確的方式應(yīng)該是這樣的
RUN [ "sh", "-c", "echo", "$HOME" ]
RUN產(chǎn)生的緩存在下一次構(gòu)建的時(shí)候是不會(huì)失效的,會(huì)被重用,可以使用--no-cache選項(xiàng),即docker build --no-cache,如此便不會(huì)緩存。
7.3 CMD
CMD有三種使用方式:
- CMD
- CMD
- CMD command param1 param2 (shell form)
CMD指定在 Dockerfile 中只能使用一次,如果有多個(gè),則只有最后一個(gè)會(huì)生效。
CMD的目的是為了在啟動(dòng)容器時(shí)提供一個(gè)默認(rèn)的命令執(zhí)行選項(xiàng)。如果用戶啟動(dòng)容器時(shí)指定了運(yùn)行的命令,則會(huì)覆蓋掉CMD指定的命令。
CMD會(huì)在啟動(dòng)容器的時(shí)候執(zhí)行,build 時(shí)不執(zhí)行,而RUN只是在構(gòu)建鏡像的時(shí)候執(zhí)行,后續(xù)鏡像構(gòu)建完成之后,啟動(dòng)容器就與RUN無關(guān)了,這個(gè)初學(xué)者容易弄混這個(gè)概念,這里簡單注解一下。
7.4 EXPOSE
EXPOSE <port> [<port>...]
告訴 Docker 服務(wù)端容器對(duì)外映射的本地端口,需要在 docker run 的時(shí)候使用-p或者-P選項(xiàng)生效。
7.5 ENV
ENV <key> <value> # 只能設(shè)置一個(gè)變量
ENV <key>=<value> ... # 允許一次設(shè)置多個(gè)變量
指定一個(gè)環(huán)節(jié)變量,會(huì)被后續(xù)RUN指令使用,并在容器運(yùn)行時(shí)保留。
例子:
ENV myName="John Doe" myDog=Rex\ The\ Dog \
myCat=fluffy
等同于
ENV myName John Doe
ENV myDog Rex The Dog
ENV myCat fluffy
7.6 ADD
ADD復(fù)制本地主機(jī)文件、目錄或者遠(yuǎn)程文件 URLS 從 并且添加到容器指定路徑中 。
支持通過 GO 的正則模糊匹配,具體規(guī)則可參見 Go filepath.Match
ADD hom* /mydir/ # adds all files starting with "hom"
ADD hom?.txt /mydir/ # ? is replaced with any single character
- 路徑必須是絕對(duì)路徑,如果 不存在,會(huì)自動(dòng)創(chuàng)建對(duì)應(yīng)目錄
- 路徑必須是 Dockerfile 所在路徑的相對(duì)路徑
- 如果是一個(gè)目錄,只會(huì)復(fù)制目錄下的內(nèi)容,而目錄本身則不會(huì)被復(fù)制
7.7 COPY
COPY復(fù)制新文件或者目錄從 并且添加到容器指定路徑中 。用法同ADD,唯一的不同是不能指定遠(yuǎn)程文件 URLS。
7.8 ENTRYPOINT
- ENTRYPOINT
- ENTRYPOINT command param1 param2 (shell form)
配置容器啟動(dòng)后執(zhí)行的命令,并且不可被 docker run 提供的參數(shù)覆蓋,而CMD是可以被覆蓋的。如果需要覆蓋,則可以使用docker run --entrypoint選項(xiàng)。
每個(gè) Dockerfile 中只能有一個(gè)ENTRYPOINT,當(dāng)指定多個(gè)時(shí),只有最后一個(gè)生效。
Exec form ENTRYPOINT 例子
通過ENTRYPOINT使用 exec form 方式設(shè)置穩(wěn)定的默認(rèn)命令和選項(xiàng),而使用CMD添加默認(rèn)之外經(jīng)常被改動(dòng)的選項(xiàng)。
FROM ubuntu
ENTRYPOINT ["top", "-b"]
CMD ["-c"]
通過 Dockerfile 使用ENTRYPOINT展示前臺(tái)運(yùn)行 Apache 服務(wù)
FROM debian:stable
RUN apt-get update && apt-get install -y --force-yes apache2
EXPOSE 80 443
VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2"]
ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
Shell form ENTRYPOINT 例子
這種方式會(huì)在/bin/sh -c中執(zhí)行,會(huì)忽略任何CMD或者docker run命令行選項(xiàng),為了確保docker stop能夠停止長時(shí)間運(yùn)行ENTRYPOINT的容器,確保執(zhí)行的時(shí)候使用exec選項(xiàng)。
FROM ubuntu
ENTRYPOINT exec top -b
如果在ENTRYPOINT忘記使用exec選項(xiàng),則可以使用CMD補(bǔ)上:
FROM ubuntu
ENTRYPOINT top -b
CMD --ignored-param1 # --ignored-param2 ... --ignored-param3 ... 依此類推
7.9 VOLUME
創(chuàng)建一個(gè)可以從本地主機(jī)或其他容器掛載的掛載點(diǎn),后續(xù)具體介紹。
7.10 USER
指定運(yùn)行容器時(shí)的用戶名或 UID,后續(xù)的RUN、CMD、ENTRYPOINT也會(huì)使用指定用戶。
7.11 WORKDIR
為后續(xù)的RUN、CMD、ENTRYPOINT指令配置工作目錄??梢允褂枚鄠€(gè)WORKDIR指令,后續(xù)命令如果參數(shù)是相對(duì)路徑,則會(huì)基于之前命令指定的路徑。
WORKDIR /a
WORKDIR b
WORKDIR c
RUN pwd
最終路徑是/a/b/c。
WORKDIR指令可以在ENV設(shè)置變量之后調(diào)用環(huán)境變量:
ENV DIRPATH /path
WORKDIR $DIRPATH/$DIRNAME
最終路徑則為 /path/$DIRNAME。
7.12 ONBUILD
配置當(dāng)所創(chuàng)建的鏡像作為其它新創(chuàng)建鏡像的基礎(chǔ)鏡像時(shí),所執(zhí)行的操作指令。
例如,Dockerfile 使用如下的內(nèi)容創(chuàng)建了鏡像 image-A:
[...]
ONBUILD ADD . /app/src
ONBUILD RUN /usr/local/bin/python-build --dir /app/src
[...]
如果基于 image-A 創(chuàng)建新的鏡像時(shí),新的 Dockerfile 中使用 FROM image-A 指定基礎(chǔ)鏡像時(shí),會(huì)自動(dòng)執(zhí)行 ONBUILD 指令內(nèi)容,等價(jià)于在后面添加了兩條指令。
# Automatically run the following
ADD . /app/src
RUN /usr/local/bin/python-build --dir /app/src
使用ONBUILD指令的鏡像,推薦在標(biāo)簽中注明,例如 ruby:1.9-onbuild。
7.13 Dockerfile Examples
# Nginx
#
# VERSION 0.0.1
FROM ubuntu
MAINTAINER Victor Vieux <victor@docker.com>
RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server
# Firefox over VNC
#
# VERSION 0.3
FROM ubuntu
# Install vnc, xvfb in order to create a 'fake' display and firefox
RUN apt-get update && apt-get install -y x11vnc xvfb firefox
RUN mkdir ~/.vnc
# Setup a password
RUN x11vnc -storepasswd 1234 ~/.vnc/passwd
# Autostart firefox (might not be the best way, but it does the trick)
RUN bash -c 'echo "firefox" >> /.bashrc'
EXPOSE 5900
CMD ["x11vnc", "-forever", "-usepw", "-create"]
# Multiple images example
#
# VERSION 0.1
FROM ubuntu
RUN echo foo > bar
# Will output something like ===> 907ad6c2736f
FROM ubuntu
RUN echo moo > oink
# Will output something like ===> 695d7793cbe4
# You?ll now have two images, 907ad6c2736f with /bar, and 695d7793cbe4 with
# /oink.
7.14 docker build
$ docker build --help
Usage: docker build [OPTIONS] PATH | URL | -
Build a new image from the source code at PATH
--force-rm=false Always remove intermediate containers, even after unsuccessful builds # 移除過渡容器,即使構(gòu)建失敗
--no-cache=false Do not use cache when building the image # 不實(shí)用 cache
-q, --quiet=false Suppress the verbose output generated by the containers
--rm=true Remove intermediate containers after a successful build # 構(gòu)建成功后移除過渡層容器
-t, --tag="" Repository name (and optionally a tag) to be applied to the resulting image in case of success
參考文檔:Dockerfile Reference
7.15 dockerfile 最佳實(shí)踐
為了在docker build過程中更快上傳和更加高效,應(yīng)該使用一個(gè).dockerignore文件用來排除構(gòu)建鏡像時(shí)不需要的文件或目錄。例如,除非.git在構(gòu)建過程中需要用到,否則你應(yīng)該將它添加到.dockerignore文件中,這樣可以節(jié)省很多時(shí)間。
為了降低復(fù)雜性、依賴性、文件大小以及構(gòu)建時(shí)間,應(yīng)該避免安裝額外的或不必要的包。例如,不需要在一個(gè)數(shù)據(jù)庫鏡像中安裝一個(gè)文本編輯器。
在大多數(shù)情況下,一個(gè)容器應(yīng)該只單獨(dú)跑一個(gè)程序。解耦應(yīng)用到多個(gè)容器使其更容易橫向擴(kuò)展和重用。如果一個(gè)服務(wù)依賴另外一個(gè)服務(wù),可以參考 Linking Containers Together。
我們知道每執(zhí)行一個(gè)指令,都會(huì)有一次鏡像的提交,鏡像是分層的結(jié)構(gòu),對(duì)于Dockerfile,應(yīng)該找到可讀性和最小化層之間的平衡。
如果可能,通過字母順序來排序,這樣可以避免安裝包的重復(fù)并且更容易更新列表,另外可讀性也會(huì)更強(qiáng),添加一個(gè)空行使用\換行:
RUN apt-get update && apt-get install -y \
bzr \
cvs \
git \
mercurial \
subversion
鏡像構(gòu)建過程中會(huì)按照Dockerfile的順序依次執(zhí)行,每執(zhí)行一次指令 Docker 會(huì)尋找是否有存在的鏡像緩存可復(fù)用,如果沒有則創(chuàng)建新的鏡像。如果不想使用緩存,則可以在docker build時(shí)添加--no-cache=true選項(xiàng)。
從基礎(chǔ)鏡像開始就已經(jīng)在緩存中了,下一個(gè)指令會(huì)對(duì)比所有的子鏡像尋找是否執(zhí)行相同的指令,如果沒有則緩存失效。在大多數(shù)情況下只對(duì)比Dockerfile指令和子鏡像就足夠了。ADD和COPY指令除外,執(zhí)行ADD和COPY時(shí)存放到鏡像的文件也是需要檢查的,完成一個(gè)文件的校驗(yàn)之后再利用這個(gè)校驗(yàn)在緩存中查找,如果檢測(cè)的文件改變則緩存失效。RUN apt-get -y update命令只檢查命令是否匹配,如果匹配就不會(huì)再執(zhí)行更新了。
為了有效地利用緩存,你需要保持你的 Dockerfile 一致,并且盡量在末尾修改。
Dockerfile 指令
- FROM: 只要可能就使用官方鏡像庫作為基礎(chǔ)鏡像
- RUN: 為保持可讀性、方便理解、可維護(hù)性,把長或者復(fù)雜的RUN語句使用\分隔符分成多行
- 不建議RUN apt-get update獨(dú)立成行,否則如果后續(xù)包有更新,那么也不會(huì)再執(zhí)行更新
- 避免使用RUN apt-get upgrade或者dist-upgrade,很多必要的包在一個(gè)非privileged權(quán)限的容器里是無法升級(jí)的。如果知道某個(gè)包更新,使用apt-get install -y xxx
- 標(biāo)準(zhǔn)寫法
- RUN apt-get update && apt-get install -y package-bar package-foo
例子:
RUN apt-get update && apt-get install -y \
aufs-tools \
automake \
btrfs-tools \
build-essential \
curl \
dpkg-sig \
git \
iptables \
libapparmor-dev \
libcap-dev \
libsqlite3-dev \
lxc=1.0* \
mercurial \
parallel \
reprepro \
ruby1.9.1 \
ruby1.9.1-dev \
s3cmd=1.1.0*
- CMD: 推薦使用CMD [“executable”, “param1”, “param2”…]這種格式,CMD [“param”, “param”]則配合ENTRYPOINT使用
- EXPOSE: Dockerfile 指定要公開的端口,使用docker run時(shí)指定映射到宿主機(jī)的端口即可
- ENV: 為了使新的軟件更容易運(yùn)行,可以使用ENV更新PATH變量。如ENV PATH /usr/local/nginx/bin:$PATH確保CMD ["nginx"]即可運(yùn)行
ENV也可以這樣定義變量:
ENV PG_MAJOR 9.3
ENV PG_VERSION 9.3.4
RUN curl -SL http://example.com/postgres-$PG_VERSION.tar.xz | tar -xJC /usr/src/postgress && …
ENV PATH /usr/local/postgres-$PG_MAJOR/bin:$PATH
- ADDorCOPY:ADD比COPY多一些特性「tar 文件自動(dòng)解包和支持遠(yuǎn)程 URL」,不推薦添加遠(yuǎn)程 URL
如不推薦這種方式:
ADD http://example.com/big.tar.xz /usr/src/things/
RUN tar -xJf /usr/src/things/big.tar.xz -C /usr/src/things
RUN make -C /usr/src/things all
推薦使用 curl 或者 wget 替換,使用如下方式:
RUN mkdir -p /usr/src/things \
&& curl -SL http://example.com/big.tar.gz \
| tar -xJC /usr/src/things \
&& make -C /usr/src/things all
如果不需要添加 tar 文件,推薦使用COPY。
參考文檔:
二、Docker 安裝
docker 的相關(guān)安裝方法這里不作介紹,具體安裝參考 官檔
獲取當(dāng)前 docker 版本
$ sudo docker version
Client version: 1.3.2
Client API version: 1.15
Go version (client): go1.3.3
Git commit (client): 39fa2fa/1.3.2
OS/Arch (client): linux/amd64
Server version: 1.3.2
Server API version: 1.15
Go version (server): go1.3.3
Git commit (server): 39fa2fa/1.3.2
三、Docker 基礎(chǔ)用法
Docker HUB : Docker鏡像首頁,包括官方鏡像和其它公開鏡像
因?yàn)閲榈脑?,國?nèi)下載 Docker HUB 官方的相關(guān)鏡像比較慢,可以使用 docker.cn 鏡像,鏡像保持和官方一致,關(guān)鍵是速度塊,推薦使用。
3.1 Search images
$ sudo docker search ubuntu
3.2 Pull images
$ sudo docker pull ubuntu # 獲取 ubuntu 官方鏡像 $ sudo docker images # 查看當(dāng)前鏡像列表
3.3 Running an interactive shell
$ sudo docker run -i -t ubuntu:14.04 /bin/bash
- docker run - 運(yùn)行一個(gè)容器
- -t - 分配一個(gè)(偽)tty (link is external)
- -i - 交互模式 (so we can interact with it)
- ubuntu:14.04 - 使用 ubuntu 基礎(chǔ)鏡像 14.04
- /bin/bash - 運(yùn)行命令 bash shell
注: ubuntu 會(huì)有多個(gè)版本,通過指定 tag 來啟動(dòng)特定的版本 [image]:[tag]
$ sudo docker ps # 查看當(dāng)前運(yùn)行的容器, ps -a 列出當(dāng)前系統(tǒng)所有的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c9129e9df10 ubuntu:14.04 /bin/bash 6 minutes ago Up 6 minutes cranky_babbage
3.4 相關(guān)快捷鍵
- 退出:Ctrl-Dorexit
- detach:Ctrl-P + Ctrl-Q
- attach:docker attach CONTAINER-ID
四、Docker 命令幫助
4.1 docker help
docker command
$ sudo docker # docker 命令幫助
Commands:
attach Attach to a running container # 當(dāng)前 shell 下 attach 連接指定運(yùn)行鏡像
build Build an image from a Dockerfile # 通過 Dockerfile 定制鏡像
commit Create a new image from a container's changes # 提交當(dāng)前容器為新的鏡像
cp Copy files/folders from the containers filesystem to the host path
# 從容器中拷貝指定文件或者目錄到宿主機(jī)中
create Create a new container # 創(chuàng)建一個(gè)新的容器,同 run,但不啟動(dòng)容器
diff Inspect changes on a container's filesystem # 查看 docker 容器變化
events Get real time events from the server # 從 docker 服務(wù)獲取容器實(shí)時(shí)事件
exec Run a command in an existing container # 在已存在的容器上運(yùn)行命令
export Stream the contents of a container as a tar archive
# 導(dǎo)出容器的內(nèi)容流作為一個(gè) tar 歸檔文件[對(duì)應(yīng) import ]
history Show the history of an image # 展示一個(gè)鏡像形成歷史
images List images # 列出系統(tǒng)當(dāng)前鏡像
import Create a new filesystem image from the contents of a tarball
# 從tar包中的內(nèi)容創(chuàng)建一個(gè)新的文件系統(tǒng)映像[對(duì)應(yīng) export]
info Display system-wide information # 顯示系統(tǒng)相關(guān)信息
inspect Return low-level information on a container # 查看容器詳細(xì)信息
kill Kill a running container # kill 指定 docker 容器
load Load an image from a tar archive # 從一個(gè) tar 包中加載一個(gè)鏡像[對(duì)應(yīng) save]
login Register or Login to the docker registry server
# 注冊(cè)或者登陸一個(gè) docker 源服務(wù)器
logout Log out from a Docker registry server # 從當(dāng)前 Docker registry 退出
logs Fetch the logs of a container # 輸出當(dāng)前容器日志信息
port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT
# 查看映射端口對(duì)應(yīng)的容器內(nèi)部源端口
pause Pause all processes within a container # 暫停容器
ps List containers # 列出容器列表
pull Pull an image or a repository from the docker registry server
# 從docker鏡像源服務(wù)器拉取指定鏡像或者庫鏡像
push Push an image or a repository to the docker registry server
# 推送指定鏡像或者庫鏡像至docker源服務(wù)器
restart Restart a running container # 重啟運(yùn)行的容器
rm Remove one or more containers # 移除一個(gè)或者多個(gè)容器
rmi Remove one or more images
# 移除一個(gè)或多個(gè)鏡像[無容器使用該鏡像才可刪除,否則需刪除相關(guān)容器才可繼續(xù)或 -f 強(qiáng)制刪除]
run Run a command in a new container
# 創(chuàng)建一個(gè)新的容器并運(yùn)行一個(gè)命令
save Save an image to a tar archive # 保存一個(gè)鏡像為一個(gè) tar 包[對(duì)應(yīng) load]
search Search for an image on the Docker Hub # 在 docker hub 中搜索鏡像
start Start a stopped containers # 啟動(dòng)容器
stop Stop a running containers # 停止容器
tag Tag an image into a repository # 給源中鏡像打標(biāo)簽
top Lookup the running processes of a container # 查看容器中運(yùn)行的進(jìn)程信息
unpause Unpause a paused container # 取消暫停容器
version Show the docker version information # 查看 docker 版本號(hào)
wait Block until a container stops, then print its exit code
# 截取容器停止時(shí)的退出狀態(tài)值
Run 'docker COMMAND --help' for more information on a command.
docker option
Usage of docker:
--api-enable-cors=false Enable CORS headers in the remote API # 遠(yuǎn)程 API 中開啟 CORS 頭
-b, --bridge="" Attach containers to a pre-existing network bridge # 橋接網(wǎng)絡(luò)
use 'none' to disable container networking
--bip="" Use this CIDR notation address for the network bridge's IP, not compatible with -b
# 和 -b 選項(xiàng)不兼容,具體沒有測(cè)試過
-d, --daemon=false Enable daemon mode # daemon 模式
-D, --debug=false Enable debug mode # debug 模式
--dns=[] Force docker to use specific DNS servers # 強(qiáng)制 docker 使用指定 dns 服務(wù)器
--dns-search=[] Force Docker to use specific DNS search domains # 強(qiáng)制 docker 使用指定 dns 搜索域
-e, --exec-driver="native" Force the docker runtime to use a specific exec driver # 強(qiáng)制 docker 運(yùn)行時(shí)使用指定執(zhí)行驅(qū)動(dòng)器
--fixed-cidr="" IPv4 subnet for fixed IPs (ex: 10.20.0.0/16)
this subnet must be nested in the bridge subnet (which is defined by -b or --bip)
-G, --group="docker" Group to assign the unix socket specified by -H when running in daemon mode
use '' (the empty string) to disable setting of a group
-g, --graph="/var/lib/docker" Path to use as the root of the docker runtime # 容器運(yùn)行的根目錄路徑
-H, --host=[] The socket(s) to bind to in daemon mode # daemon 模式下 docker 指定綁定方式[tcp or 本地 socket]
specified using one or more tcp://host:port, unix:///path/to/socket, fd://* or fd://socketfd.
--icc=true Enable inter-container communication # 跨容器通信
--insecure-registry=[] Enable insecure communication with specified registries (no certificate verification for HTTPS and enable HTTP fallback) (e.g., localhost:5000 or 10.20.0.0/16)
--ip="0.0.0.0" Default IP address to use when binding container ports # 指定監(jiān)聽地址,默認(rèn)所有 ip
--ip-forward=true Enable net.ipv4.ip_forward # 開啟轉(zhuǎn)發(fā)
--ip-masq=true Enable IP masquerading for bridge's IP range
--iptables=true Enable Docker's addition of iptables rules # 添加對(duì)應(yīng) iptables 規(guī)則
--mtu=0 Set the containers network MTU # 設(shè)置網(wǎng)絡(luò) mtu
if no value is provided: default to the default route MTU or 1500 if no default route is available
-p, --pidfile="/var/run/docker.pid" Path to use for daemon PID file # 指定 pid 文件位置
--registry-mirror=[] Specify a preferred Docker registry mirror
-s, --storage-driver="" Force the docker runtime to use a specific storage driver # 強(qiáng)制 docker 運(yùn)行時(shí)使用指定存儲(chǔ)驅(qū)動(dòng)
--selinux-enabled=false Enable selinux support # 開啟 selinux 支持
--storage-opt=[] Set storage driver options # 設(shè)置存儲(chǔ)驅(qū)動(dòng)選項(xiàng)
--tls=false Use TLS; implied by tls-verify flags # 開啟 tls
--tlscacert="/root/.docker/ca.pem" Trust only remotes providing a certificate signed by the CA given here
--tlscert="/root/.docker/cert.pem" Path to TLS certificate file # tls 證書文件位置
--tlskey="/root/.docker/key.pem" Path to TLS key file # tls key 文件位置
--tlsverify=false Use TLS and verify the remote (daemon: verify client, client: verify daemon) # 使用 tls 并確認(rèn)遠(yuǎn)程控制主機(jī)
-v, --version=false Print version information and quit # 輸出 docker 版本信息
4.2 docker search
$ sudo docker search --help
Usage: docker search TERM
Search the Docker Hub for images # 從 Docker Hub 搜索鏡像 --automated=false Only show automated builds
--no-trunc=false Don't truncate output
-s, --stars=0 Only displays with at least xxx stars
示例:
$ sudo docker search -s 100 ubuntu # 查找 star 數(shù)至少為 100 的鏡像,找出只有官方鏡像 start 數(shù)超過 100,默認(rèn)不加 s 選項(xiàng)找出所有相關(guān) ubuntu 鏡像 NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Official Ubuntu base image 425 [OK]
4.3 docker info
$ sudo docker info
Containers: 1 # 容器個(gè)數(shù) Images: 22 # 鏡像個(gè)數(shù) Storage Driver: devicemapper # 存儲(chǔ)驅(qū)動(dòng) Pool Name: docker-8:17-3221225728-pool
Pool Blocksize: 65.54 kB
Data file: /data/docker/devicemapper/devicemapper/data
Metadata file: /data/docker/devicemapper/devicemapper/metadata
Data Space Used: 1.83 GB
Data Space Total: 107.4 GB
Metadata Space Used: 2.191 MB
Metadata Space Total: 2.147 GB
Library Version: 1.02.84-RHEL7 (2014-03-26) Execution Driver: native-0.2 # 存儲(chǔ)驅(qū)動(dòng) Kernel Version: 3.10.0-123.el7.x86_64
Operating System: CentOS Linux 7 (Core)
4.4 docker pull && docker push
$ sudo docker pull --help # pull 拉取鏡像 Usage: docker pull [OPTIONS] NAME[:TAG] Pull an image or a repository from the registry
-a, --all-tags=false Download all tagged images in the repository $ sudo docker push # push 推送指定鏡像 Usage: docker push NAME[:TAG] Push an image or a repository to the registry
示例:
$ sudo docker pull ubuntu # 下載官方 ubuntu docker 鏡像,默認(rèn)下載所有 ubuntu 官方庫鏡像 $ sudo docker pull ubuntu:14.04 # 下載指定版本 ubuntu 官方鏡像
$ sudo docker push 192.168.0.100:5000/ubuntu # 推送鏡像庫到私有源[可注冊(cè) docker 官方賬戶,推送到官方自有賬戶] $ sudo docker push 192.168.0.100:5000/ubuntu:14.04 # 推送指定鏡像到私有源
4.5 docker images
列出當(dāng)前系統(tǒng)鏡像
$ sudo docker images --help
Usage: docker images [OPTIONS] [NAME] List images
-a, --all=false Show all images (by default filter out the intermediate image layers) # -a 顯示當(dāng)前系統(tǒng)的所有鏡像,包括過渡層鏡像,默認(rèn) docker images 顯示最終鏡像,不包括過渡層鏡像 -f, --filter=[] Provide filter values (i.e. 'dangling=true') --no-trunc=false Don't truncate output
-q, --quiet=false Only show numeric IDs
示例:
$ sudo docker images # 顯示當(dāng)前系統(tǒng)鏡像,不包括過渡層鏡像 $ sudo docker images -a # 顯示當(dāng)前系統(tǒng)所有鏡像,包括過渡層鏡像 $ sudo docker images ubuntu # 顯示當(dāng)前系統(tǒng) docker ubuntu 庫中的所有鏡像 REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu 12.04 ebe4be4dd427 4 weeks ago 210.6 MB
ubuntu 14.04 e54ca5efa2e9 4 weeks ago 276.5 MB
ubuntu 14.04-ssh 6334d3ac099a 7 weeks ago 383.2 MB
4.6 docker rmi
刪除一個(gè)或者多個(gè)鏡像
$ sudo docker rmi --help
Usage: docker rmi IMAGE [IMAGE...] Remove one or more images
-f, --force=false Force removal of the image # 強(qiáng)制移除鏡像不管是否有容器使用該鏡像 --no-prune=false Do not delete untagged parents # 不要?jiǎng)h除未標(biāo)記的父鏡像
4.7 docker run
$ sudo docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] Run a command in a new container
-a, --attach=[] Attach to stdin, stdout or stderr.
-c, --cpu-shares=0 CPU shares (relative weight) # 設(shè)置 cpu 使用權(quán)重 --cap-add=[] Add Linux capabilities
--cap-drop=[] Drop Linux capabilities
--cidfile="" Write the container ID to the file # 把容器 id 寫入到指定文件 --cpuset="" CPUs in which to allow execution (0-3, 0,1) # cpu 綁定 -d, --detach=false Detached mode: Run container in the background, print new container id # 后臺(tái)運(yùn)行容器 --device=[] Add a host device to the container (e.g. --device=/dev/sdc:/dev/xvdc) --dns=[] Set custom dns servers # 設(shè)置 dns --dns-search=[] Set custom dns search domains # 設(shè)置 dns 域搜索 -e, --env=[] Set environment variables # 定義環(huán)境變量 --entrypoint="" Overwrite the default entrypoint of the image # ? --env-file=[] Read in a line delimited file of ENV variables # 從指定文件讀取變量值 --expose=[] Expose a port from the container without publishing it to your host # 指定對(duì)外提供服務(wù)端口 -h, --hostname="" Container host name # 設(shè)置容器主機(jī)名 -i, --interactive=false Keep stdin open even if not attached # 保持標(biāo)準(zhǔn)輸出開啟即使沒有 attached --link=[] Add link to another container (name:alias) # 添加鏈接到另外一個(gè)容器 --lxc-conf=[] (lxc exec-driver only) Add custom lxc options --lxc-conf="lxc.cgroup.cpuset.cpus = 0,1" -m, --memory="" Memory limit (format: <number><optional unit>, where unit = b, k, m or g) # 內(nèi)存限制 --name="" Assign a name to the container # 設(shè)置容器名 --net="bridge" Set the Network mode for the container # 設(shè)置容器網(wǎng)絡(luò)模式 'bridge': creates a new network stack for the container on the docker bridge 'none': no networking for this container 'container:<name|id>': reuses another container network stack 'host': use the host network stack inside the container. Note: the host mode gives the container full access to local system services such as D-bus and is therefore considered insecure.
-P, --publish-all=false Publish all exposed ports to the host interfaces # 自動(dòng)映射容器對(duì)外提供服務(wù)的端口 -p, --publish=[] Publish a container's port to the host # 指定端口映射 format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort (use 'docker port' to see the actual mapping) --privileged=false Give extended privileges to this container # 提供更多的權(quán)限給容器 --restart="" Restart policy to apply when a container exits (no, on-failure[:max-retry], always) --rm=false Automatically remove the container when it exits (incompatible with -d) # 如果容器退出自動(dòng)移除和 -d 選項(xiàng)沖突 --security-opt=[] Security Options
--sig-proxy=true Proxify received signals to the process (even in non-tty mode). SIGCHLD is not proxied.
-t, --tty=false Allocate a pseudo-tty # 分配偽終端 -u, --user="" Username or UID # 指定運(yùn)行容器的用戶 uid 或者用戶名 -v, --volume=[] Bind mount a volume (e.g., from the host: -v /host:/container, from docker: -v /container) # 掛載卷 --volumes-from=[] Mount volumes from the specified container(s) # 從指定容器掛載卷 -w, --workdir="" Working directory inside the container # 指定容器工作目錄
示例:
$ sudo docker images ubuntu
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu 14.04 e54ca5efa2e9 4 weeks ago 276.5 MB
... ... $ sudo docker run -t -i -c 100 -m 512MB -h test1 -d --name="docker_test1" ubuntu /bin/bash # 創(chuàng)建一個(gè) cpu 優(yōu)先級(jí)為 100,內(nèi)存限制 512MB,主機(jī)名為 test1,名為 docker_test1 后臺(tái)運(yùn)行 bash 的容器 a424ca613c9f2247cd3ede95adfbaf8d28400cbcb1d5f9b69a7b56f97b2b52e5 $ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a424ca613c9f ubuntu:14.04 /bin/bash 6 seconds ago Up 5 seconds docker_test1 $ sudo docker attach docker_test1
root@test1:/# pwd /
root@test1:/# exit exit
關(guān)于cpu優(yōu)先級(jí):
By default all groups have 1024 shares. A group with 100 shares will get a ~10% portion of the CPU time - archlinux cgroups
4.8 docker start|stop|kill... ...
dockerstart|stop|kill|restart|pause|unpause|rm|commit|inspect|logs
- docker start CONTAINER [CONTAINER...]
- # 運(yùn)行一個(gè)或多個(gè)停止的容器
- docker stop CONTAINER [CONTAINER...]
- # 停掉一個(gè)或多個(gè)運(yùn)行的容器-t選項(xiàng)可指定超時(shí)時(shí)間
- docker kill [OPTIONS] CONTAINER [CONTAINER...]
- # 默認(rèn) kill 發(fā)送 SIGKILL 信號(hào)-s可以指定發(fā)送 kill 信號(hào)類型
- docker restart [OPTIONS] CONTAINER [CONTAINER...]
- # 重啟一個(gè)或多個(gè)運(yùn)行的容器-t選項(xiàng)可指定超時(shí)時(shí)間
- docker pause CONTAINER
- docker unpause CONTAINER
- docker rm [OPTIONS] CONTAINER [CONTAINER...]
- # 移除一個(gè)或多個(gè)容器
- -f, --force=false Force removal of running container
- -l, --link=false Remove the specified link and not the underlying container
- -v, --volumes=false Remove the volumes associated with the container
- docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
- # 提交指定容器為鏡像
- -a, --author="" Author (e.g., "John Hannibal Smith hannibal@a-team.com")
- -m, --message="" Commit message
- -p, --pause=true Pause container during commit
- # 默認(rèn) commit 是暫停狀態(tài)
- docker inspect CONTAINER|IMAGE [CONTAINER|IMAGE...]
- docker logs CONTAINER
- # 輸出指定容器日志信息
- -f, --follow=false Follow log output
- -t, --timestamps=false Show timestamps
- --tail="all" Output the specified number of lines at the end of logs (defaults to all logs)
參考文檔:Docker Run Reference
4.9 Docker 1.3 新增特性和命令
Digital Signature Verification
Docker 1.3 版本將使用數(shù)字簽名自動(dòng)驗(yàn)證所有官方庫的來源和完整性,如果一個(gè)官方鏡像被篡改或者被破壞,目前 Docker 只會(huì)對(duì)這種情況發(fā)出警告而并不阻止容器的運(yùn)行。
Inject new processes withdocker exec
docker exec --help
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...] Run a command in an existing container
-d, --detach=false Detached mode: run command in the background
-i, --interactive=false Keep STDIN open even if not attached
-t, --tty=false Allocate a pseudo-TTY
為了簡化調(diào)試,可以使用docker exec命令通過 Docker API 和 CLI 在運(yùn)行的容器上運(yùn)行程序。
$ docker exec -it ubuntu_bash bash
上例將在容器 ubuntu_bash 中創(chuàng)建一個(gè)新的 Bash 會(huì)話。
Tune container lifecycles withdocker create
我們可以通過docker run <image name>命令創(chuàng)建一個(gè)容器并運(yùn)行其中的程序,因?yàn)橛泻芏嘤脩粢髣?chuàng)建容器的時(shí)候不啟動(dòng)容器,所以docker create應(yīng)運(yùn)而生了。
$ docker create -t -i fedora bash
6d8af538ec541dd581ebc2a24153a28329acb5268abe5ef868c1f1a261221752
上例創(chuàng)建了一個(gè)可寫的容器層 (并且打印出容器 ID),但是并不運(yùn)行它,可以使用以下命令運(yùn)行該容器:
$ docker start -a -i 6d8af538ec5
bash-4.2#
Security Options
通過--security-opt選項(xiàng),運(yùn)行容器時(shí)用戶可自定義 SELinux 和 AppArmor 卷標(biāo)和配置。
$ docker run --security-opt label:type:svirt_apache -i -t centos \ bash
上例只允許容器監(jiān)聽在 Apache 端口,這個(gè)選項(xiàng)的好處是用戶不需要運(yùn)行 docker 的時(shí)候指定--privileged選項(xiàng),降低安全風(fēng)險(xiǎn)。
參考文檔:Docker 1.3: signed images, process injection, security options, Mac shared directories
4.10 Docker 1.5 新特性
參考文檔:Docker 1.5 新特性