Docker基础之二: Linux快速入门

网友投稿 768 2023-03-08

本站部分文章、图片属于网络上可搜索到的公开信息,均用于学习和交流用途,不能代表睿象云的观点、立场或意见。我们接受网民的监督,如发现任何违法内容或侵犯了您的权益,请第一时间联系小编邮箱jiasou666@gmail.com 处理。

Docker基础之二: Linux快速入门

Linux快速使用教程

由于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 passwdEnter new UNIX password:Retype new UNIX password:passwd: password updated successfully

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

根据提示输入当前用户的密码,然后再输入root的密码。sudo是以管理员身份运行命令。然后通过su 命令切换到root用户.

5.2 使用vim

vim是ubuntu默认的文本编辑器,学习使用linux第一步就是学会使用vi。有的时候vim可能是没有安装的,我们需要手动来进行安装:

安装成功之后,我们就可以使用vim了。vim 是vi的升级版,有了很多优化。常用的命令有:

i – 从当前位置开始插入数据a – 在当前位置后面插入数据esc – 退出编辑模式: - 在vim中执行一条指令,比如wq就是保存加退出/ - 搜索文字上下左右键 – 移动光标,vi 里面不能用方向键,但是vim里面是可以使用的

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 eth0iface eth0 inet staticaddress 192.168.1.10netmask 255.255.0.0gateway 192.168.0.1dns-nameservers 61.139.2.69 218.6.200.139

# 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 eth0iface 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 -a

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

5.4 启用SSH Server

SSH是Secure Shell的缩写,是linux的标准远程连接工具,通过这个工具我们可以以命令行的方式远程连接到 linux主机之上。首先我们需要检查在主机上是否安装了ssh server

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

root@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`

# vim /etc/ssh/sshd_config#允许Root登录PermitRootLogin yes#允许通过密码进行验证登录PasswordAuthentication yes`

保存退出后,执行

root@gctest:~# restart ssh

root@gctest:~# restart ssh

然后验证能否本地登录

root@shev:~# ssh root@localhostThe 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)?

root@shev:~# ssh root@localhostThe 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机器。

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一样的系统——apt。apt就是一个软件仓库,你只需要指定仓库地址,然后就可以进行搜索和安装.

添加源:默认的ubuntu源是指向国外的,位于/etc/apt/sources.list,我们可以在网上搜索国内的源,比如:网易源,阿里源等

搜索软件

安装软件

root@shev:/etc/apt# apt-get install apache2Reading 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-cert0 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 install apache2Reading 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-cert0 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# apt-get purge apache2

查找本地安装的软件

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

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

上一篇:无监控不运维——Prometheus 快速入门
下一篇:运维告警关联分析(运维平台告警规则的级别)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~