欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

Linux学习之路(一)

程序员文章站 2022-07-04 20:19:12
导语: 早前为了方便日常开发,建立跟生产环境类型的环境的时候考虑使用docker作为模拟生产环境,结果没想到给自己的学习挖了一个大坑。其他关于docker容器技术的坑先不在这里赘述,有时间的话在其他文章记录下来,这里先从最基本的工作环境开始拉开这条漫长的学习之路。 docker虽然提供了很多好用并且 ......

导语:

早前为了方便日常开发,建立跟生产环境类型的环境的时候考虑使用docker作为模拟生产环境,结果没想到给自己的学习挖了一个大坑。其他关于docker容器技术的坑先不在这里赘述,有时间的话在其他文章记录下来,这里先从最基本的工作环境开始拉开这条漫长的学习之路。

docker虽然提供了很多好用并且效率很高的镜像给我们使用,基本都不需要自己动手配置容器环境但是由于过于注重镜像纯净跟效率的原因大部分镜像里面基本就只有跟当前需要环境相关的工具跟linux配置,一点多余的东西都没有。这就苦逼了我这种二手的运维菜鸡了,容器已启动一进去发现linux环境下连一个普通linux环境必备的vi命令都没有更别说其他的网络查看工具了。迫于处女座的洁癖,*把除了服务器环境意外的所有常用工具都得自己挨个爬一次坑吧。毕竟如果把这些东西弄一遍我就可能不再是二手的菜鸡了。

在爬坑的过程中可能甚至必然会遇到发现了一个问题需要解决但是又引起了无数个新的知识点需要补充。所以这条爬坑之路可能会很难去归类,也没有太多时间去归类,唯有爬到哪就记录到哪了!!!!

以下内容大部分都来源于其他博客或者书籍查询到的结果。经过自己实践可以没问题才记录上来方便以后查看!!!

本坑是在docker官方镜像(ubuntu:latest)的环境下完成,ubuntu版本是ubuntu 18.04.1 lts。其他版本的坑可能会略有不同。请慎重

以下观点只代表菜鸟言论,大神路过勿喷


 

 

实际上所谓的linux工具实际上很多人有自己的叫法,大致统计下基本有这几种统称:命令、程序、工具、服务、软件。其实都是通过不同的安装方式安装到linux环境下,直接使用的。一般的简单工具安装好后一般都会在/bin目录下看到它们的身影(还有些特殊的可能会在其他的地方这里不深究,知道这个意思就行),只不过使用的时候不同的命令工具会有自身特色的传参方式而已例如:

root@454009d432a4:/bin# netstat  -anp

 

1.linux工具安装最常用的一种方法就是 apt系列命令

Linux学习之路(一)

最常用的就是apt-get、apt-cache:apt-get 用来更新、删除、安装各种命令用,apt-cache用来搜索、查看各种命令信息。

第一次进来首要任务肯定是先执行apt-get update在线更新下所有工具包的最新列表,保证安装工具的时候使用的是最新的源以免报错掉到无尽的坑里。最后安装常用的网路检测工具net-tools,这个包下面包括常用的netstat、ifconfig等命令(之前没搞懂之前还以为netstat命令是独立安装的,所以通过apt-cache search搜索到的包安装后根本就不是那么回事。后来找了资料才知道是在这个包里)。

root@454009d432a4:/bin# apt-get update              //更新资源列表
root@454009d432a4:/bin# apt-cache search net-tools        //搜索net-tools这个工具包确认存在
net-tools - net-3 networking toolkit
atm-tools - base programs for atm in linux, the net-tools for atm
ddnet-tools - tools for ddnet
root@454009d432a4:/bin# apt-get install net-tools      //安装网络工具包
........
........
root@454009d432a4:/bin# netstat              //安装完成执行netstat命令看下是否正常
active internet connections (w/o servers)
proto recv-q send-q local address           foreign address         state
active unix domain sockets (w/o servers)
proto refcnt flags       type       state         i-node   path


root@454009d432a4:/bin# ifconfig            //安装完成执行ifconfig命令看下是否正常
eth0: flags=4163<up,broadcast,running,multicast>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (ethernet)
        rx packets 21  bytes 1598 (1.5 kb)
        rx errors 0  dropped 0  overruns 0  frame 0
        tx packets 0  bytes 0 (0.0 b)
        tx errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<up,loopback,running>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1  (local loopback)
        rx packets 0  bytes 0 (0.0 b)
        rx errors 0  dropped 0  overruns 0  frame 0
        tx packets 0  bytes 0 (0.0 b)
        tx errors 0  dropped 0 overruns 0  carrier 0  collisions 0


附上一张基本网络工具图(http://www.docin.com/p-1852435799.html)

Linux学习之路(一)

 

2.linux后台定时任务进程工具cron

这是一个服务器实现定时任务或者

root@454009d432a4:/bin# apt-cache search cron        //搜索cron资源
anacron - cron-like program that doesn't go by time
cron - process scheduling daemon                //要找的正主
dict-vera - dictionary of computer related acronyms -- dict format
python-croniter - provides iteration for datetime object with cron like format -
 python 2.x
......
......
......
root@454009d432a4:/bin# apt-get install cron      //安装cron组件
reading package lists... done
building dependency tree
reading state information... done
...
...
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of start.
root@454009d432a4:/bin# cron            //确认是否已经命令(输入cron双击tab按键自动提示)
cron     crontab


root@454009d432a4:/bin# crontab -e      //编辑定时任务文件
no crontab for root - using an empty one           //what?报错,没有文本编辑器,这就是docker镜像的坑,缺少很多基本的linux工具,都需要自己一个一个安装
update-alternatives: error: no alternatives for editor
/usr/bin/sensible-editor: 25: /usr/bin/sensible-editor: editor: not found
/usr/bin/sensible-editor: 28: /usr/bin/sensible-editor: nano: not found
/usr/bin/sensible-editor: 31: /usr/bin/sensible-editor: nano-tiny: not found
/usr/bin/sensible-editor: 34: /usr/bin/sensible-editor: vi: not found
couldn't find an editor!
set the $editor environment variable to your desired editor.
crontab: "/usr/bin/sensible-editor" exited with status 1    //报错后自动退出程序
root@454009d432a4:/bin#
//忽略掉文本编辑器接着cron工具使用方法

root@454009d432a4:/# service cron status  //检查cron服务是否已开启
 * cron is not running
root@454009d432a4:/bin# crontab -e      //编辑定时任务文件如果已经安装过文本编辑器会是直接进入文本编辑模式进行编辑定时任务
#
# each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# to define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# for example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# for more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
#定时任务命令格式
#
# m h  dom mon dow  command
# m:分钟 取值范围: 0-59 当为*时表示每分钟
# h:小时 取值范围: 0-23 当为*时表示每小时
# dom:天 取值范围: 1-31 当为*时表示每天
# mon:月 取值范围: 1-12 当为*时表示每月
# dow:星期 取值范围: 1-6 0表示星期天 当为*时表示每周
# command:当符合当前时间条件的时候要执行的命令,可以是纯命令也可以是执行固定的shell脚本
# 以上m h  dom mon dow  command格式中的变量必须都要有值,每个变量中以空格分割详细说明见下面的链接
# https://www.cnblogs.com/angel_kiki/p/5266699.html
# 如下:每分钟将当前时间写入指定文本中
* * * * * date >> ~/check.log



root@454009d432a4:/# service cron start //启动指定服务
root@454009d432a4:/# service cron restart //重启指定服务
root@454009d432a4:/# service cron status
 * cron is running        //服务正在运行中,找到指定的文件打开看看有没有数据就知道有没有执行成功了

root@454009d432a4:/# service cron stop   //停止指定服务

以上资料来源:https://www.cnblogs.com/angel_kiki/p/5266699.html

 

 

 

3.linux文本编辑器vi、vim基于vim是vi的升级版所以这里选择vim

 

 

root@454009d432a4:/# apt-get install vim  //安装vim
root@454009d432a4:/# vim /root/check.log  //编辑指定文件
thu sep 27 10:27:01 utc 2018
thu sep 27 10:28:01 utc 2018
thu sep 27 10:29:01 utc 2018
~
~

 

vi、vim详细操作参考以下地址(个人感觉描述的挺详细具体的):https://blog.csdn.net/williamfan21c/article/details/56495261