導語: 早前為了方便日常開發,建立跟生產環境類型的環境的時候考慮使用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系列命令
最常用的就是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)
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