閱讀555 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Docker基礎之二: Linux快速入門

Linux快速使用教程

(提示:對Docker感興趣的朋友可以加我的微信ghostcloud2016,然後我把你加到我們的一個Docker愛好者群組裏麵。

由於Docker是的容器都依賴於linux 內核,因此這一節主要是快速簡單的介紹一下linux,如果你對linux比較熟悉,可略過。

1 為什麼要使用linux

  • 本身開源免費
  • 支持眾多開源的軟件,諸如mysql, apache, mongodb, nodeJS等
  • 基本上90%以上的互聯網公司都使用linux作為後端服務器
  • 雲主機大多數都是基於linux係統

2 選取什麼發行版本

Linux包含了很多的發行版本,包括ubuntu, centos, redhat, federa等等,但是他們都是基於linux kernel,各個發行版本都會做相應的包裝、優化和簡化,但是基本上內核版本不會有太大的差異。根據我的經驗,我推薦使用ubuntu 或者centos。Ubuntu的優點是:

  • 內核更新及時
  • 軟件安裝和更新方便
  • GUI簡單實用 CentOS就是Red Hat Enterprise 的開源版本,也是不錯的選擇,考慮到Ubuntu對Docker的完美支持,我一般推薦使用Ubuntu.

3 圖形界麵 Or 命令行界麵

在安裝的時候,ubuntu14.04提供了桌麵版和服務器版,如果你要使用eclipse等工具進行開發,肯定要選擇桌麵版;如果你隻是運行後台程序,最好選擇服務器版,多使用服務器版把linux的命令用熟悉也是一種學習和鍛煉。

4 英文 Or 中文

一般來說使用linux的用戶都是相對專業的用戶,我建議一律使用英文的操作係統。使用英文操作係統,可以熟悉英文,同時不會出現奇怪的亂碼字符。

5 安裝ubuntu 14.04

安裝的時候就根據提示一步步的進行就可以了,在中途選擇軟件的時候,至少把SSH server選上,方便後續連接入係統。安裝完畢後,我們通過之前設置的用戶名和密碼登錄,接下來我們做一些常用配置。

5.1 啟用root用戶

root用戶是linux的最高權限用戶,相當於windows的超級管理員。我們可以通過下麵的方式來啟用root用戶:

root@gctest:~# sudo passwd
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

根據提示輸入當前用戶的密碼,然後再輸入root的密碼。sudo是以管理員身份運行命令。然後通過su 命令切換到root用戶.

5.2 使用vim

vim是ubuntu默認的文本編輯器,學習使用linux第一步就是學會使用vi。有的時候vim可能是沒有安裝的,我們需要手動來進行安裝:

root@gctest:~# sudo apt-get update && apt-get install vim
Hit https://mirrors.163.com precise Release.gpg
Hit https://mirrors.163.com precise Release
Hit https://apt.ghostcloud.cn ubuntu-precise Release.gpg

安裝成功之後,我們就可以使用vim了。vim 是vi的升級版,有了很多優化。常用的命令有:

i – 從當前位置開始插入數據
a – 在當前位置後麵插入數據
esc – 退出編輯模式
: - 在vim中執行一條指令,比如wq就是保存加退出
/ - 搜索文字
上下左右鍵 – 移動光標,vi 裏麵不能用方向鍵,但是vim裏麵是可以使用的

雖然還有很多命令,但是用上麵的基本就能操作了。

5.3 配置網絡

ubuntu的網絡配置是放在/etc/network/interfaces下的,我們通過vim來進行查看和修改

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.1.10
netmask 255.255.0.0
gateway 192.168.0.1
dns-nameservers 61.139.2.69 218.6.200.139

修改完畢後,我們需要重啟網絡,一個比較好的方式是禁用再啟用網絡:

root@gctest:~# ifdown -a && ifup -a

5.4 啟用SSH Server

SSH是Secure Shell的縮寫,是linux的標準遠程連接工具,通過這個工具我們可以以命令行的方式遠程連接到 linux主機之上。首先我們需要檢查在主機上是否安裝了ssh server

root@shev:~# dpkg -l | grep openssh-server
ii  openssh-server                      1:6.6p1-2ubuntu2                 amd64        secure shell (SSH) server, for secure access from remote machines

如果沒有安裝就不會有下麵的輸出。接下來,我們需要配置ssh

# vim /etc/ssh/sshd_config

#允許Root登錄
PermitRootLogin yes

#允許通過密碼進行驗證登錄
PasswordAuthentication yes
`

保存退出後,執行

root@gctest:~# restart ssh

然後驗證能否本地登錄

root@shev:~# ssh root@localhost
The authenticity of host 'localhost (::1)' can't be established.
ECDSA key fingerprint is 3a:8c:00:76:4d:4d:62:a7:c7:18:a0:00:e6:d0:17:c7.
Are you sure you want to continue connecting (yes/no)?

根據提示輸入用戶密碼,如果可以登錄說明安裝成功,最後執行exit,退出ssh連接。

5.5 通過客戶端連接linux主機

目前市麵上有很多ssh客戶端,包括免費的XShell, Secure CRT等,如果你使用linux或mac係統,本身就自帶 ssh 客戶端。Ssh 命令登錄,需要指定用戶和ip地址,格式如下:

ssh <用戶名>@<IP>
#如:ssh root@192.168.1.10 表示,以root用戶登錄192.168.1.10機器。

5.6 免密碼登錄linux主機

免密碼登錄的原理是在你需要登錄的遠程主機上,存放當前機器的公鑰。

  1. 在當前機器生成公鑰和私鑰 ssh-keygen
  2. 根據提示生成以後,會在~/.ssh/目錄下生成相關的文件。這裏的~指的是用戶的目錄,比如,在linux下abc用戶的目錄為/home/abc,root用戶的目錄為/root,在mac下是在/Users/<用戶名>
  3. 將公鑰id_rsa.pub拷貝到目標機器上 scp ~/.ssh/id_rsa.pub root@192.168.1.10:~/ 這行命令將當前用戶的公鑰拷貝到遠程機器的root用戶目錄下
  4. ssh root@192.168.1.10
  5. ssh-keygen #在遠端產生密鑰
  6. cat id_rsa.pub >> ~/.ssh/authorized_keys #加入信任列表
  7. rm id_rsa.pub #刪除公鑰
  8. exit #退出遠程機器 這時已經返回到當前機器,再執行ssh root@192.168.1.10就不再需要輸入密碼了。

5.7 安裝軟件

Ubuntu軟件安裝用的是和debian一樣的係統——apt。apt就是一個軟件倉庫,你隻需要指定倉庫地址,然後就可以進行搜索和安裝.

  1. 添加源:默認的ubuntu源是指向國外的,位於/etc/apt/sources.list,我們可以在網上搜索國內的源,比如:網易源,阿裏源等
    deb https://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse
    deb https://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse
    deb https://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse
    deb https://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse
    deb https://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse
    deb-src https://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse
    deb-src https://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse
    deb-src https://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse
    
  2. 搜索軟件
    root@shev:/etc/apt# apt-cache search apache2 | grep ^apache2
    apache2 - Apache HTTP Server
    apache2-bin - Apache HTTP Server (binary files and modules)
    apache2-data - Apache HTTP Server (common files)
    apache2-dbg - Apache debugging symbols
    apache2-dev - Apache HTTP Server (development headers)
    apache2-doc - Apache HTTP Server (on-site documentation)
    apache2-mpm-event - transitional event MPM package for apache2
    apache2-mpm-prefork - transitional prefork MPM package for apache2
    apache2-mpm-worker - transitional worker MPM package for apache2
    apache2-utils - Apache HTTP Server (utility programs for web servers)
    apache2.2-bin - Transitional package for apache2-bin
    apache2-mpm-itk - transitional itk MPM package for apache2
    apache2-suexec - transitional package for apache2-suexec-pristine
    apache2-suexec-custom - Apache HTTP Server configurable suexec program for mod_suexec
    apache2-suexec-pristine - Apache HTTP Server standard suexec program for mod_suexec
    
  3. 安裝軟件
    root@shev:/etc/apt# apt-get install apache2
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    The following extra packages will be installed:
    apache2-bin apache2-data libapr1 libaprutil1 libaprutil1-dbd-sqlite3
    libaprutil1-ldap ssl-cert
    Suggested packages:
    apache2-doc apache2-suexec-pristine apache2-suexec-custom apache2-utils
    openssl-blacklist
    The following NEW packages will be installed:
    apache2 apache2-bin apache2-data libapr1 libaprutil1 libaprutil1-dbd-sqlite3
    libaprutil1-ldap ssl-cert
    0 upgraded, 8 newly installed, 0 to remove and 72 not upgraded.
    Need to get 1285 kB of archives.
    After this operation, 5348 kB of additional disk space will be used.
    Do you want to continue? [Y/n] y
    
  4. 卸載軟件

    root@shev:/etc/apt# apt-get purge apache2
    
  5. 查找本地安裝的軟件

    root@shev:/etc/apt# dpkg -l

最後更新:2017-04-01 13:37:06

  上一篇:go PostgreSQL 秒殺場景優化
  下一篇:go PostgreSQL OLTP高並發請求性能優化