站点图标 IDC铺

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 或者centosUbuntu的优点是:内核更新及时软件安装和更新方便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 passwdEnter new UNIX password:Retype new UNIX password:passwd: password updated successfully

根据提示输入当前用户的密码,然后再输入root的密码sudo是以管理员身份运行命令然后通过su 命令切换到root用户.5.2 使用vimvim是ubuntu默认的文本编辑器,学习使用linux第一步就是学会使用vi。

有的时候vim可能是没有安装的,我们需要手动来进行安装:root@gctest:~# sudo apt-get update && apt-get install vimHit http://mirrors.163.com

precise Release.gpgHit http://mirrors.163.com precise ReleaseHit http://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 interfaceauto loiface lo inet loopback# The primary network interfaceauto eth0

iface eth0 inet staticaddress 192.168.1.10netmask 255.255.0.0gateway 192.168.0.1dns-nameservers 61.139.2.69 218.6.200.139

修改完毕后,我们需要重启网络,一个比较好的方式是禁用再启用网络:root@gctest:~# ifdown -a && ifup -a5.4 启用SSH ServerSSH是Secure Shell的缩写,是linux的标准远程连接工具,通过这个工具我们可以以命令行的方式远程连接到 linux主机之上。

首先我们需要检查在主机上是否安装了ssh serverroot@shev:~# dpkg -l | grep openssh-serverii  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@localhostThe authenticity of host localhost (::1) cant 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 @#如:ssh root@192.168.1.10 表示,以root用户登录192.168.1.10机器5.6 免密码登录linux主机。

免密码登录的原理是在你需要登录的远程主机上,存放当前机器的公钥在当前机器生成公钥和私钥 ssh-keygen根据提示生成以后,会在~/.ssh/目录下生成相关的文件这里的~指的是用户的目录,比如,在linux下abc用户的目录为/home/abc,root用户的目录为/root,在mac下是在/Users/。

将公钥id_rsa.pub拷贝到目标机器上 scp ~/.ssh/id_rsa.pub root@192.168.1.10:~/ 这行命令将当前用户的公钥拷贝到远程机器的root用户目录下ssh root@192.168.1.10

ssh-keygen #在远端产生密钥cat id_rsa.pub >> ~/.ssh/authorized_keys #加入信任列表rm id_rsa.pub #删除公钥exit #退出远程机器 这时已经返回到当前机器,再执行ssh root@192.168.1.10就不再需要输入密码了。

5.7 安装软件Ubuntu软件安装用的是和debian一样的系统——aptapt就是一个软件仓库,你只需要指定仓库地址,然后就可以进行搜索和安装.添加源:默认的ubuntu源是指向国外的,位于/etc/apt/sources.list,我们可以在网上搜索国内的源,比如:网易源,阿里源等。

deb http://mirrors.163.com/ubuntu/ trusty main restricted universe multiversedeb http://mirrors.163.com/ubuntu/

trusty-security main restricted universe multiversedeb http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse

deb http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiversedeb http://mirrors.163.com/ubuntu/

trusty-backports main restricted universe multiversedeb-src http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse

deb-src http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiversedeb-src http://mirrors.163.com/ubuntu/

trusty-updates main restricted universe multiverse搜索软件root@shev:/etc/apt# apt-cache search apache2 | grep ^apache2

apache2 – Apache HTTP Serverapache2-bin – Apache HTTP Server (binary files and modules)apache2-data – Apache HTTP Server (common files)

apache2-dbg – Apache debugging symbolsapache2-dev – Apache HTTP Server (development headers)apache2-doc – Apache HTTP Server (on-site documentation)

apache2-mpm-event – transitional event MPM package for apache2apache2-mpm-prefork – transitional prefork MPM package for apache2

apache2-mpm-worker – transitional worker MPM package for apache2apache2-utils – Apache HTTP Server (utility programs for web servers)

apache2.2-bin – Transitional package for apache2-binapache2-mpm-itk – transitional itk MPM package for apache2

apache2-suexec – transitional package for apache2-suexec-pristineapache2-suexec-custom – Apache HTTP Server configurable suexec program for mod_suexec

apache2-suexec-pristine – Apache HTTP Server standard suexec program for mod_suexec安装软件root@shev:/etc/apt# apt-get install apache2

Reading package lists… DoneBuilding dependency treeReading state information… DoneThe following extra packages will be installed:

apache2-bin apache2-data libapr1 libaprutil1 libaprutil1-dbd-sqlite3libaprutil1-ldap ssl-certSuggested packages:

apache2-doc apache2-suexec-pristine apache2-suexec-custom apache2-utilsopenssl-blacklistThe following NEW packages will be installed:

apache2 apache2-bin apache2-data libapr1 libaprutil1 libaprutil1-dbd-sqlite3libaprutil1-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卸载软件root@shev:/etc/apt# apt-get purge apache2查找本地安装的软件root@shev:/etc/apt# dpkg -l

退出移动版