防止Android刷机变砖

了解名词术语

Bootloader

Bootloader是一个启动加载程序,负责系统硬件的初始化工作,也是刷机工作的第一道坎。大部分厂商采用了锁Bootloader的方式 来阻止用户刷机,用户如果想要刷第三方固件就必须对Bootloader进行解锁,由于Bootloader加密算法较为复杂,所以解锁就变成了一件困难事,部分厂商因用户不满最终妥协,提供了官方的解锁途径,比如HTC和索尼爱立信,用户需要到官方网站进行申请然后解锁,但解锁后会失去保修,而另外一部分厂商则不提供解锁,那么解锁Bootloader就变成了一件很复杂的事,虽然有些黑客通过内外相勾结的方式破解了Bootloader算法,但没有破解是绝对完美的,也有很多朋友的手机在解锁时变成了砖头。

如何判断Bootloader解锁呢?当Bootloader解锁后,启动手机会出现开锁图标,或者再次进入Bootloader界面有Unlock标志,就证明Bootloader顺利解锁。

Recovery

Recovery是手机的工程模式,主要用来执行手机的复位和升级工作,大部分厂商都在手机中提供了进入Recovery的方法,所以用户可以在官方下载Update.zip升级包在Recovery下进行升级,这种方式也是我们俗称的卡刷,当然也有部分厂商不提供,比如中兴. 官方提供的Recovery一般来讲功能有限,而且尽可以进行Update升级包升级的操作,所以对于刷机的用户来讲官方的Recovery是没太大用 的,大家一般都会选择使用第三方的Recovery来代替官方的。对于Recovery的操作,每家厂商的机型都有所不同,但是都大同小异,手机上的物理 按键仅有几颗,所以Recovery的操作一般都是集中在开关机键、音量键或者确定键上。

root

在类UNIX系统中,root为超级用户,拥有系统的最高权限,如果我们把玩和使用分开算的话,那么玩Android手机的第一步就是获取root权限,只有有了系统的最高权限,我们才能在Android系 统中释放最大的能量,干一些用户权限完不成的事。关于刷机方面,root主要是为了获取权限后让手机能够顺利的刷入第三方的Recovery,各个手机也 都提供了各种不同的root方法,大体上可以分为两种,一种是临时root,而另一种是永久root,临时root会在系统重启后超级用户权限消失,而永久root不会。

Wipe

Wipe是主清除、复位的意思,这个字眼也是刷机的朋友经常听到的,很多朋友都是按照刷机教程去做了,但并不知道做的目的,Wipe存在于手机的Recovery之中,默认系统提供了两个Wipe方式,第三方的Recovery可能提供了三个,其中一个为执行两个Wipe(用户数据和虚拟机缓存),Wipe只是为了清除干净手机的用户数据以及虚拟机缓存,如果没有Wipe,可能会造成手机刷机后无法正常启动, 在启动界面出现死循环等状况,所以刷机的朋友应该养成双Wipe的好习惯。

了解自己手机

有没有解锁?有没有第三方固件资源?手机版本型号?手机版本对应的基带和固件包?

明白线刷与卡刷哪个好?

目前的Android手机刷机大体分为两种刷机方式,线刷和卡刷,线刷指使用USB线作为刷机工具进行刷机,而卡刷则是把固件或者升级包拷贝到手机SD卡中进行刷机升级操作。线刷一般是官方所采取的升级方式,主要用来刷固件,如果手机故障造成无法开机等情况,我们可以考虑使用线刷来拯救手机,线刷一般需要刷机工具,也有和刷机包整合好的。

固件如何选择

Android机型所谓刷机就是更换固件,那么固件的选择就尤为重要了,目前的固件总共可以分几大类:一个官方定制固件,也就是我们俗称的官方包、底包,刷过以后手机就回到了买时候的样子,刷官方固件需要注意,要对应自己手机的版本,不仅仅是型号问题,同时还有地区版本,不少朋友变砖都是因为错刷了地区版本导致的,刷固件一般都在Fastboot下进行,这是一个更为底层的刷机界面,需要用线刷。

第二类固件是使用官方固件改的民间固件,这类固件采用官方底包作为基础,进行优化,外观与官方固件类似,内部所应用到的程序也会不有太大变动, 此类固件依托官方驱动,可以达到很好的稳定性(当然也有修改内核或参数造成不稳定的情况),并在官方固件上进行了精简,去掉了很多没必要的应用。

第三类固件是鼎鼎有名的CyanogenMod出品的固件,是我们俗称的CM,CM团队对于Android爱 好者贡献非常大,CM固件在Android源代码上进行制作,并在其中加入了大量新功能,比如DSP、主题等等,允许用户对一些系统级的设置进行修 改,CM团队也利用自身优势,编写了大量机型的驱动,让CM可以适配市面上绝大多数主流机型,使用CM作为基础进行改造的固件也相当制作,大部分为民间作品。目前CM主流的固件有CM7和CM9。

CM9界面样式 最后一类固件就是像MIUI和点心OS这样有着中国特色的固件,这些固件由固定团队制作,质量很有保障,固件对于中国人使用进行了大量优化,Android原生的应用也被改的“面目全非”,为了达到各机型适配的目的,这些固件大多使用了CM的底层代码,如果没有CM的贡献,那么这些固件只能是谷歌的Nexus系列才有福享用了。

刷机的基本步骤

刷机基本上可以分为几步,首先解锁手机的Bootloader,部分手机可略过此步。每个品牌,甚至是每个型号在这步操作上都有所不同,对于一些Bootloader锁得比较好的手机,大家还是谨慎为妙,比如摩托罗拉的Bootloader,虽然有非官方解锁,但由于破解不完美,造成了很多朋友的手机变砖。

SuperUser

解锁后的手机需要root,这一步相对比较简单,风险也相对较小,但由于网上各品牌、各型号、甚至是各版本的手机都有不同的root方法,所以在这里就不一一介绍了。

ClockworkMod Recovery

接下来我们需要往root了的手机内刷入第三方Recovery,目前采用的较主流的方法是使用ClockWorkMod Recovery+固件管理器(ROM Manager)的方法,可以直接在手机上安装固件管理器进行第三方ClockWorkMod Recovery安装或者连接数据线,在电脑端使用CMD命令行程序敲命令进行安装。

最后下载要刷的固件包,使用CWM Recovery进行安装,这里只要了解了自己的机型,下载了对应的包,那么刷机过程就比较简单了,在刷机时,大家需要注意刷包前双Wipe,清除虚拟机缓存与用户数据,以免造成无法开机的情况。

大多数机型的刷机步骤都是如此,因为原理相同,只不过会在界面操作或者进入Recovery方法等地方有所不同,只要我们在需要注意的环节多做些功课,手机变砖的可能性还是非常小的。 刷完进入新系统后,还会出现一些问题,比如没有信号,进入系统信息查看一下基带版本,如果未知,那么可以刷基带解决,类似问题还会有不少,比如使用了过高主频的内核而无法进入系统,我们都可以下载指定的内核或者基带在CWM Recovery下刷入。

由于各机型的刷机方法不同,所以在此我们没法一个个展现给大家,Android的刷机是门学问,但方法不同原理相同,本文的目的也是告诉大家刷机要谨慎,只要了解了刷机的关键点,刷机其实并不可怕,可怕的是盲目。在刷机之前一定要弄清楚自己是为什么刷机,不要盲目,哪怕是千元智能机刷成砖了也可惜了。


2013-09-12 Android , Rom , SuperUser , ClockworkMod , Fastboot , adb

AOKP vs. CyanogenMod 10.1

Similarities between both the ROMs

  • Both are based on the latest version of Android
  • The ability to customize Quick Settings
  • T9 Dialer with Call Statistics
  • Stock AOSP browser
  • Stock Messaging App with expandable notifications
  • LED Notification Control (Like Light Flow)
  • Theme Chooser
  • Custom Brightness control
  • Volume Rocker Wake
  • Volume Rocker Music Control
  • Ability to disable IME switcher
  • Battery % in the status bar 在状态栏显示电量百分比
  • Advanced Sound panel
  • Advanced Power Menu
  • Expanded Desktop mode
  • Improved Camera app with features like voice control
  • Quiet Hours
  • Recents RAM Bar
  • Notification bar widgets
  • Quick Launch Shortcuts
  • Lock Screen Shortcuts
  • The ability to always keep lock screen widgets in maximized states
  • Vibration when a call is picked/on call waiting/call is disconnected

CyanogenMod 10.1 Pros

  • Inbuilt OTA updater (Not Delta though)
  • Stock Android with just the right amount of customizable options to attract new users
  • Comes pre-installed with a beautiful lock screen widget – cLock
  • In-built Profiles support
  • Comes with a limited version of Pie Controls as seen on Paranoid Android
  • The ability to disable Root access to apps or over adb temporarily
  • Daily nightly builds that are much more stable than even AOKP’s Builds

The customization options in AOKP ROM are just insane. Please take a deep breath before reading the list below.

  • Lock screen ribbon
  • AOKP Ribbon – An Ubuntu Mobile like sidebar that you can swipe from the corner of the screen to get quick access to your favorite apps. You can even directly access all the apps installed on your phone via this, thus providing the ability to use your phone without a launcher.
  • While even CM10.1 has lock screen shortcuts, AOKP ROM allows for more shortcuts (5 vs. 7). The same is true for Quick launch shortcuts (3 vs. 5).
  • Navigator bar customization – You can add a shortcut on the navigation bar like on CM10.1, but AOKP ROM allows for far more control over what you want to do. It also allows you to set shortcuts on long pressing of a key on the navigation bar 导航栏自定义化
  • The ability to re-size the navigation bar
  • The ability to auto-hide the navigation bar after a certain period of inactivity
  • Ability to change the navigation bar icons
  • Navigation bar widgets
  • While CM10.1 also allows you to customize Quick Settings, AOKP allows much more customization options
  • Dual Panel (Tablet UI) mode for the navigation bar
  • Dual panel mode for certain inbuilt apps
  • Notification bar ribbon and widgets
  • Ability to display the battery status as a small bar on the navigation bar
  • Custom notification background
  • Custom boot animation
  • Custom vibrations for each app or contact
  • The ability to make the Status bar and navigation bar transparent 状态栏和导航栏透明
  • Ability to set custom toggles in Quick Settings
  • Permissions Management
  • Ability to put the Status bar clock in the Center
  • Ability to set long press actions to items in the Quick Settings menu
  • Swagger
  • Phew! That’s not all though. There are a lot of other customization options that AOKP has, which I have not listed above. If the above options don’t convince you, I doubt the rest will. However, the ROM does have a few cons that are listed below. AOKP Cons

Nightly builds can be a bit unstable, but then they are nightly builds. However, for some people this can be a deal breaker

  • No in-built Pie Launcher or any other similar implementation
  • No Profiles feature
  • Limited device support
  • No central download center for nightly and milestone releases

Conclusion

If after reading the above post, you are still confused, I would suggest you to simply try out both the ROMs. Also, keep in mind that the pros of one ROM automatically becomes the cons of another, and vice-versa. There is no difference in battery life between both the ROMs, since both the ROMs are generally based on a slightly modified stock kernel.

However, users may slight performance difference between both the ROMs on the same device. Ideally, there should not really be any performance difference between both the ROMs, since AOKP is based on CyanogenMod’s device tree. From the above comparison, it should be quite clear that the CyanogenMod team is no longer in the race to offer the most customizable custom ROM.

Instead, their goal is now to offer a ROM that behaves and looks like stock, but comes with some basic customization options. On the other hand, one of main goals of AOKP ROM is to offer its users the ability to customize each and every aspect of the Android OS running on their device.


2013-09-11 aokp , cyanogenmod

固态硬盘知识总结

固态硬盘(Solid State Disk、Solid State Drive,简称 SSD)是一种以存储器作为永久性存储器的计算机存储设备。 虽然 SSD 已不是使用“碟”来记存数据,而是使用 NAND Flash,但是人们依照命名习惯,仍然称为固态硬盘(Solid-State Disk)或固态驱动器(Solid-State Drive)。当然,SSD 内也没有用来驱动(Drive)旋转的马达。

由于固态硬盘技术与传统硬盘技术不同,所以产生了不少新兴的存储器厂商。厂商只需使用闪存(NAND),再配合适当的控制芯片,就可以制造固态硬盘了。新一代的固态硬盘普遍采用 SATA-3 接口. 1

固态硬盘的优势

SSD 固态硬盘价格更高,容量相对普通硬盘要低,相同容量的 SSD 比 HDD 的要贵一倍不止,但是读取速度要比 HDD 的快,抗震性能强,而且体积比 HDD 硬盘要小,发热量也要小的多,节能省电也是 SSD 的优势。

  • 启动快。
  • 快速随机读取,读取延迟小。
  • 写入速度更快。
  • 无噪音,功耗和发热相对较低。
  • 不怕碰撞和冲击,无论任何的安装位置甚至悬空都不影响性能。
  • 体积更小,重量很轻。

4K 对齐

4K 对齐是一种高级硬盘使用技术,用特殊方法将文件系统格式与硬盘物理层上进行契合,为提高硬盘寿命与高效率使用硬盘空间提供解决方案。因该技术将物理扇区与文件系统的每簇 4096 字节对齐而得名。2

4K 对齐教程:http://diybbs.zol.com.cn/1/34028_1553.html

固态硬盘接口类型

SATA

SATA 是 Serial ATA 的缩写,即串行 ATA。看维基知道,SATA 接口是用来取代 IDE 接口的.SATA 接口目前有三代 3

  • SATA 1.5Gb/s
  • SATA 3.0Gb/s
  • SATA 6.0Gb/s

非官方民间称呼 SATA2,SATA3,看维基百科上面还有一代 Express 版本的.

mSATA

mSATA (mini-SATA)是迷你版本 SATA 接口


2013-08-20 SSD , Knowledge

Bash 命令小技巧

Bash 使用过程中一些非常小的技巧,记录一下。

使用 less 代替 tailf

在习惯了 less +F /var/log/syslog 之后可以完全抛弃 tailf .

Alt + . 上一个命令的参数

按下 Alt + . 会在命令行中插入上一个命令的参数,非常适合参数比较复杂时直接插入参数。

Ctrl x Ctrl e

当你发现在命令行中输入太多内容了,那么尝试一下 Ctrl + x + Ctrl + e ,Bash 会打开 $EDITOR 然后编辑你的命令,一旦输入保存退出,那么 Bash 会立即执行该命令。


2013-04-04 linux , commands , bash , less , tips

2013 年 Google 愚人节玩笑

Google 每年愚人节都会弄一系列的愚人游戏,去年整理的在这里,http://www.einverne.tk/2012/04/2012-april-fool-day-google.html

##Google Nose Google 灵鼻子 Google Nose 是一款能够告诉你气味的产品,在 Google 搜索中搜索关键词,在右边就会显示出闻一闻

搜索中新增气味查询功能

气味扑鼻:在打字、说话和触摸的基础上,新增一种感官体验。 您的互联网侍酒师:专家精心设计的知识面板,配有图片、说明和气味介绍。 左闻右嗅:Google 百味库由超过 1500 万个气味字节组成。 不闻不问:如果您不希望查询中出现某些内容,请启用安全搜索功能。

那是什么气味?

Google 灵鼻子 Beta 将新技术与现有技术相结合,可提供最敏锐的嗅觉体验: 街味嗅闻汽车已经嗅遍了数百万英里的空气,并将它们编入了索引。 Android 周边气味检测工具可通过全世界最敏感的移动操作系统收集气味。 SMELLCD™ 1.8+ 具有很高的分辨率,能精确地控制气味并将气味散播开来。

Link Link

##Google Maps 藏宝图 Google 的街景拍摄团队在拍摄时偶然在海底发现一箱藏宝图

##Gmail Blue Everything is Blue

https://www.gmail.com/mail/help/intl/en/promos/blue/

##Google+Photo Emotion

https://plus.google.com/+ErikMurphyChutorian/posts/6cbXigttnUL

##YouTube 寻找全球最佳视频

##Google Schmick

##Google Fiber Poles

##Google Apps 提供 Levity Algorithm

##Google Analytics 提供来自国际空间站的访客信息

##Google 日语输入法发布

##Google Wallet Mobile ATM Google Wallet Mobile ATM 帮你随时随地从手机里获得现金 Link


2013-04-02 Google , 愚人节,Web

Wifi 是如何工作的?

要了解 WiFi 那么则先要分清楚这几个名词,WiFi,WLAN:

  • WiFi 的全称是 Wireless Fidelity,目前我们见到的 Wifi 其实是隶属于 Wifi 联盟的商标,表示的是建立于 IEEE 802.11 标准的无线局域网技术
  • 而 WLAN 是无线局域网络 (Wireless Local Area Networks) 的缩写, Wifi 是 WLAN 的一种实现

WiFi 的技术优势

  • 无线电波相较于蓝牙覆盖面积广,WiFi 的理论半径可以达到 100 米
  • 商家接入成本低

无线组网的节点

在 WLAN 中有几个常见的名词,比如:

  • AP,表示的是 Access Point,无线接入点
  • STA, 其实是 Station 的缩写,指代的是连接到无线网络中的设备

信道

如果稍微留意一下家用路由器的设置后台就会发现,除了网络名称,加密方式之外还有无线模式,和信道之别。大部分家用路由器是无法设置信道的,但是如果用 OpenWrt 或者刷了其他固件可能会有调整设置。而这里的信道就是指的在 2.4GHz 或者 5GHz 下不同的频率的传输通道,2.4 GHz 频段频率范围为 2.400—2.4835GHz,下面划分了 14 个信道。现在的家用网络环境越来越复杂,所以导致相同频率之间的设备可能会有干扰,所以手动先观察周围的信道 (Android 下可以使用开源的 WiFiAnalyzer),再选择一个不干扰的信道可以优化一下家用网络环境。

2.4GHz 和 5GHz 区别

802.11 划分了四个独立频段,2.4 GHz、3.6 GHz、4.9 GHz 和 5.8 GHz 1

2.4 GHz 频段频率范围为 2.400—2.4835GHz,共 83.5M 带宽,在 2.4 GHz 下又划分了 14 个信道,每个子信道宽度为 22MHz,相邻信道的中心频点间隔 5MHz。观察信道的频率重合就能发现,信道 1,2,3,4,5 频率有重叠,整个 2.4 GHz 频段内只有 1,6,11 互不干扰。

  • 第一代 802.11:1997 年制定,只使用 2.4GHz,最快 2Mbit/s
  • 第二代 802.11b,只使用 2.4GHz,最快 11Mbit/s,正逐渐被淘汰
  • 第三代 802.11g/a,分別使用 2.4GHz 和 5GHz,最快 54Mbit/s
  • 第四代 802.11n(Wi-Fi 4),可使用 2.4GHz 或 5GHz,20 和 40MHz 頻寬下最快 72 和 150Mbit/s
  • 第五代 802.11ac(Wi-Fi 5),工作频率 5GHz,频道带宽支持 20, 40, 80, 160MHz,速率最高可以单条 866.7 Mbit/s
  • 第六代 802.11ax(Wi-Fi 6),WiFi-6 在目前 802.11ac 标准的基础上,WiFi-6 是为了解决局域网中连接设备的增多而诞生

IEEE 802.11b 是无线局域网的一个标准,载波的频率为 2.4GHz,传送速度为 11Mbit/s . IEEE 802.11b 的后继标准是 IEEE 802.11g,其传送速度为 54Mbit/s。所以我们日常提及的 2.4G 和 5GHz 的区别就在与路由器发射频率的区别,实现的 802.11 协议的区别。当我们距离路由器相同距离,5G 信号相比于 2.4G 信号较弱,这是由于电磁波的物理特性决定,波越长衰减越少,更容易绕过障碍物传播。5G 信号频率高,波长短,所以 5G 信号绕过障碍物衰减更多。但是信号衰减并不代表影响网络速度,网络速度还与信道相关,上文提及 2.4G 下互不干扰的信道只有 3 个,但是 5G 下有 20 多个,并且 5G 可以支持更高的网络传输速率。所以如果追求更加稳定不受干扰的信号就用 5GHz。

IEEE 802.11n 是 802.11a 和 802.11g 改良,改进的地方:

  • channel 可以使用 40MHz 频宽
  • 使用 MIMO(multiple-input and multiple-output) 技术,可以一次多个通道发送数据,其实就是用多根天线。

WiFi 的加密方式

常见的 WiFi 加密方式有:WPA 、WPA2、WPA/WPA2、WEP、802.1x(EAP)

WPA

WPA 是 Wi-Fi Protected Access(WiFi 安全存取), 有 WPA 和 WPA2 两个标准,WPA 继承了 WEP 基本原理,同时加强了生成密钥的算法,追加了防止数据中途被篡改的功能。

WPA 于 2003 年推出,它是 Wi-Fi 联盟针对 WEP 加密日益明显的漏洞而推出的新一代加密协议。最常见的 WPA 配置是WPA-PSK(预共享密钥)。WPA 使用的密钥是,比WEP 系统中使用的和密钥256-bit显着增加。

完整的 WPA 实现复杂,一般家用采用的是 WPA 的简化版本:WPA-PSK(预共享密钥)。

WPA 加密方式有四种认证方式:WPA,WPA-PSK,WPA2,WPA2-PSK。

WPA2

WPA2 是 WPA 的增强型版本,WPA2 新增了对 AES 的加密方式的支持。

WPA-PSK

WPA-PSK 适用于个人或普通家庭网络,使用预先共享密钥,支持 TKIP 和 AES 两种加密方式。

WPA3

WPA3 是比 WPA2 更加安全的加密协议。WPA3 发布于 2018 年。

WEP

WEP 全称是 Wired Equivalent Privacy(有线等效保密),是对两台设备间无线传输的数据加密的一种。WEP 是一种加密方法,安全性没有 WPA 高。

WEP 作为 1997 年批准的原始 802.11 标准的一部分引入,是过去最常用的 WiFi 安全协议。它的 10 位或 26 位十六进制数字(40 位或 104 位)的密钥非常容易识别。2004 年,WEP-40 和 WEP-104 都被宣布弃用。

外延

Android 下有款 APP 叫做 WiFiAnalyzer 可以扫描周围 WiFi 占用的信道,以便于调整路由器信道。

802.11 协议规定,不同的无线网络可以具有相同的网络名称(也就是 SSID/ESSID),但是必须对应一个唯一的 BSSID 地址。非法入侵者可以通过建立具有相同的 SSID/ESSID 的无线网络的方法,使得网络中的 STA 联接到非法的 AP 上,从而造成网络的泄密。通过 BSSID 地址绑定的方式,可以防止 STA 接入到非法的网络,从而提高无线网络的安全性。

reference

  1. https://zh.wikipedia.org/wiki/WLAN%E4%BF%A1%E9%81%93%E5%88%97%E8%A1%A8 


2013-02-19 wifi , tcp , network

每天学习一个命令:tee 读取标准输入并输出

在执行 Linux 命令时,我们可以把输出重定向到文件中,比如 ls > a.txt,这时我们就不能看到输出了,如果我们既想把输出保存到文件中,又想在屏幕上看到输出内容,就可以使用 tee 命令了。tee 命令读取标准输入,把这些内容同时输出到标准输出和(多个)文件中(read from standard input and write to standard output and files. Copy standard input to each FILE, and also to standard output. If a FILE is -, copy again to standard output.)。

在 tee 中说道:tee 命令可以重定向标准输出到多个文件(tee': Redirect output to multiple files. The tee’ command copies standard input to standard output and also to any files given as arguments. This is useful when you want not only to send some data down a pipe, but also to save a copy.)。要注意的是:在使用管道时,前一个命令的标准错误输出不会被 tee 读取。

实例

格式:

tee file

输出到标准输出的同时,保存到文件 file 中。如果文件不存在,则创建;如果已经存在,则覆盖之。当使用 -a 参数时不会覆盖,而是附加。

格式:

tee -a file

输出到标准输出的同时,追加到文件 file 中。如果文件不存在,则创建;如果已经存在,就在末尾追加内容,而不是覆盖。

格式:

tee -

输出到标准输出两次。(A FILE of -' causes tee’ to send another copy of input to standard output, but this is typically not that useful as the copies are interleaved.)

格式:

tee file1 file2 -

输出到标准输出两次,同时保存到 file1 和 file2 中。

使用 tee 命令重复输出字符串

echo 12345 | tee
12345

echo 12345 | tee -
12345
12345
echo 12345 | tee - -
12345
12345
12345

使用 tee 命令把标准错误输出也保存到文件,默认情况下 tee 是不会将标准错误内容也保存到文件的,这个时候需要使用 2>$1

ls "*" 2>&1 | tee ls.txt

文件中就是

ls: cannot access '*': No such file or directory

2013-01-26 linux , command

每天学习一个命令:ss socket 数据

ss 命令可以用来获取 socket 信息,可以用来显示和 netstat 类似的信息,但 ss 能够显示更多 TCP 和状态的信息,包括 PACKET,TCP,UDP,DCCP,RAW,和 Unix domain sockes 等等。

通过 ss ,可以获取当前机器如何和外界通信非常详细的信息,包括网络连接信息,网络协议,Linux socket 连接状态等等。通过这些信息,可以非常轻松和方便的定位网络问题。当服务器 Socket 连接数量比较大时,netstat 可能就罢工了,这个时候 ss 还是能够应付的,ss 使用了 TCP 协议栈中的 tcp_diagtcp_diag 是一个用于分析统计的模块。

命令格式

ss [options] [ FILTER ]

当没有选项使用时,ss 会显示一组已经建立 open non-listening sockets (TCP/UNIX/UDP) 连接的 sockets。

选项

-h, --help	帮助信息
-V, --version	程序版本信息
-n, --numeric	不解析服务名称
-r, --resolve        解析主机名
-a, --all	显示所有套接字(sockets)
-l, --listening	显示监听状态的套接字(sockets)
-o, --options        显示计时器信息
-e, --extended       显示详细的套接字(sockets)信息
-m, --memory         显示套接字(socket)的内存使用情况
-p, --processes	显示使用套接字(socket)的进程
-i, --info	显示 TCP 内部信息
-s, --summary	显示套接字(socket)使用概况
-4, --ipv4           仅显示 IPv4 的套接字(sockets)
-6, --ipv6           仅显示 IPv6 的套接字(sockets)
-0, --packet	        显示 PACKET 套接字(socket)
-t, --tcp	仅显示 TCP 套接字(sockets)
-u, --udp	仅显示 UCP 套接字(sockets)
-d, --dccp	仅显示 DCCP 套接字(sockets)
-w, --raw	仅显示 RAW 套接字(sockets)
-x, --unix	仅显示 Unix 套接字(sockets)
-f, --family=FAMILY  显示 FAMILY 类型的套接字(sockets),FAMILY 可选,支持  unix, inet, inet6, link, netlink
-A, --query=QUERY, --socket=QUERY
      QUERY := {all|inet|tcp|udp|raw|unix|packet|netlink}[,QUERY]
-D, --diag=FILE     将原始 TCP 套接字(sockets)信息转储到文件
 -F, --filter=FILE   从文件中都去过滤器信息
       FILTER := [ state TCP-STATE ] [ EXPRESSION ]

使用实例

显示 TCP 连接,UDP 连接,Unix Sockets

ss -u -a
ss -t
ss -x

显示摘要信息

ss -s

列出当前的 established, closed, orphaned and waiting TCP sockets

查看进程使用的 socket

命令

ss -lp

说明:

  • -l 参数显示当前正在监听的 socket

在结果中可以过滤出端口占用的进程

ss -lp | grep 80

过滤 TCP 状态

ss 命令能够通过 TCP states 来过滤,状态列表

  • established
  • syn-sent
  • syn-recv
  • fin-wait-1
  • fin-wait-2
  • time-wait
  • closed
  • close-wait
  • last-ack
  • listening
  • closing

ss 命令能够识别的其他状态

  • all (all of the above states)
  • connected (all the states with the exception of listen and closed)
  • synchronized (all of the connected states with the exception of syn-sent)
  • bucket (states which are maintained as minisockets, for example time-wait and
  • syn-recv)
  • big (Opposite to bucket state)

对于 tcp ipv4

ss -4 state FILTER
ss -4 state listening

对于 ipv6

ss -6 state FILTER

显示所有状态为 established 的 SMTP 连接

ss -o state established '( dport = :smtp or sport = :smtp )'

显示所有状态为 Established 的 HTTP 连接

ss -o state established '( dport = :http or sport = :http )'

这里一定要注意引号中的写法,该有的空格一定要有。

显示特定目的地的连接

ss 另外一个非常便利的功能就是可以查看特定 IP 地址的连接情况,比如想要查看多少连接从 IP 192.168.1.130 连接到本机,则可以

ss dst 192.168.1.130

对于本地连接同理

ss src 192.168.1.200
ss src 192.168.1.200:80
ss src 192.168.1.200:http
ss src 192.168.1.200:smtp

reference


2013-01-25 linux , command , socket

每天学习一个命令:kill 杀掉进程

Linux 中的 kill 命令用来终止指定的进程(terminate a process)的运行,是 Linux 下进程管理的常用命令。通常,终止一个前台进程可以使用 Ctrl+C 键,但是,对于一个后台进程就须用 kill 命令来终止。此时就需要先使用 ps/pidof/pstree/top 等工具获取进程 PID,然后使用 kill 命令来杀掉该进程。

kill 命令是通过向进程发送指定的信号来结束相应进程的。在默认情况下,采用编号为 15 的 TERM 信号。TERM 信号将终止所有不能捕获该信号的进程。对于那些可以捕获该信号的进程就要用编号为 9 的 kill 信号,强行“杀掉”该进程。

命令格式:

kill [options] [PID]

命令功能:

发送指定的信号到相应进程。不指定将发送 SIGTERM(15)终止指定进程。如果仍然无法终止该程序可用 “-KILL” 参数,其发送的信号为 SIGKILL(9) ,将强制结束进程,使用 ps 命令或者 jobs 命令可以查看进程号。root 用户可以控制用户的进程,非 root 用户只能杀死自己的进程。

命令参数:

-l  信号,若果不加信号的编号参数,则使用“-l”参数会列出全部的信号名称
-a  当处理当前进程时,不限制命令名和进程号的对应关系
-p  指定 kill 命令只打印相关进程的进程号,而不发送任何信号
-s  指定发送信号
-u  指定用户

注意:

kill 命令可以带信号号码选项,也可以不带。如果没有信号号码,kill 命令就会发出终止信号 (15),这个信号可以被进程捕获,使得进程在退出之前可以清理并释放资源。也可以用 kill 向进程发送特定的信号。例如:

kill -2 PID

它的效果等同于在前台运行 PID 的进程时按下 Ctrl+C 键。但是,普通用户只能使用不带 signal 参数的 kill 命令或最多使用 -9 信号。

kill 可以带有进程 ID 号作为参数。当用 kill 向这些进程发送信号时,必须是这些进程的所有者。如果试图撤销一个没有权限撤销的进程或撤销一个不存在的进程,就会得到一个错误信息。

可以向多个进程发信号或终止它们。

当 kill 成功地发送了信号后,shell 会在屏幕上显示出进程的终止信息。有时这个信息不会马上显示,只有当按下 Enter 键使 shell 的命令提示符再次出现时,才会显示出来。

应注意,信号使进程强行终止,这常会带来一些副作用,如数据丢失或者终端无法恢复到正常状态。发送信号时必须小心,只有在万不得已时,才用 kill 信号 9,因为进程不能首先捕获它。要撤销所有的后台作业,可以输入 kill 0。因为有些在后台运行的命令会启动多个进程,跟踪并找到所有要杀掉的进程的 PID 是件很麻烦的事。这时,使用 kill 0 来终止所有由当前 shell 启动的进程,是个有效的方法。

使用实例

列出所有信号名称

命令:

kill -l

输出:

HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT
CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH POLL PWR SYS

说明:

只有第 9 种信号 (SIGKILL) 才可以无条件终止进程,其他信号进程都有权利忽略。

下面是常用的信号:

HUP    1    终端断线
INT     2    中断(同 Ctrl + C)
QUIT    3    退出(同 Ctrl + \)
TERM   15    终止
KILL    9    强制终止
CONT   18    继续(与 STOP 相反, fg/bg 命令)
STOP    19    暂停(同 Ctrl + Z)

获取指定信号的数值

命令:

kill -l KILL
kill -l TERM

用 ps 查找进程,然后用 kill 杀掉

命令:

ps -ef | grep 'program'
kill PID

无条件彻底杀死进程

命令:

kill –9 PID

杀死指定用户所有进程

命令:

kill -9 $(ps -ef | grep username)
kill -u username

init 进程是杀不死的

命令:

kill -9 1

说明:

init 是 Linux 系统操作中不可缺少的程序之一。所谓的 init 进程,它是一个由内核启动的用户级进程。内核自行启动(已经被载入内存,开始运行,并已初始化所有的设备驱动程序和数据结构等)之后,就通过启动一个用户级程序 init 的方式,完成引导进程。所以,init 始终是第一个进程(其进程编号始终为 1)。其它所有进程都是 init 进程的子进程,init 进程是无法杀死的。


2013-01-24 linux , command

Jekyll Introduction

This Jekyll introduction will outline specifically what Jekyll is and why you would want to use it. Directly following the intro we’ll learn exactly how Jekyll does what it does.

Overview

What is Jekyll?

Jekyll is a parsing engine bundled as a ruby gem used to build static websites from dynamic components such as templates, partials, liquid code, markdown, etc. Jekyll is known as “a simple, blog aware, static site generator”.

Examples

This website is created with Jekyll. Other Jekyll websites.

What does Jekyll Do?

Jekyll is a ruby gem you install on your local system. Once there you can call jekyll --server on a directory and provided that directory is setup in a way jekyll expects, it will do magic stuff like parse markdown/textile files, compute categories, tags, permalinks, and construct your pages from layout templates and partials.

Once parsed, Jekyll stores the result in a self-contained static _site folder. The intention here is that you can serve all contents in this folder statically from a plain static web-server.

You can think of Jekyll as a normalish dynamic blog but rather than parsing content, templates, and tags on each request, Jekyll does this once beforehand and caches the entire website in a folder for serving statically.

Jekyll is Not Blogging Software

Jekyll is a parsing engine.

Jekyll does not come with any content nor does it have any templates or design elements. This is a common source of confusion when getting started. Jekyll does not come with anything you actually use or see on your website - you have to make it.

Why Should I Care?

Jekyll is very minimalistic and very efficient. The most important thing to realize about Jekyll is that it creates a static representation of your website requiring only a static web-server. Traditional dynamic blogs like Wordpress require a database and server-side code. Heavily trafficked dynamic blogs must employ a caching layer that ultimately performs the same job Jekyll sets out to do; serve static content.

Therefore if you like to keep things simple and you prefer the command-line over an admin panel UI then give Jekyll a try.

Developers like Jekyll because we can write content like we write code:

  • Ability to write content in markdown or textile in your favorite text-editor.
  • Ability to write and preview your content via localhost.
  • No internet connection required.
  • Ability to publish via git.
  • Ability to host your blog on a static web-server.
  • Ability to host freely on GitHub Pages.
  • No database required.

How Jekyll Works

The following is a complete but concise outline of exactly how Jekyll works.

Be aware that core concepts are introduced in rapid succession without code examples. This information is not intended to specifically teach you how to do anything, rather it is intended to give you the full picture relative to what is going on in Jekyll-world.

Learning these core concepts should help you avoid common frustrations and ultimately help you better understand the code examples contained throughout Jekyll-Bootstrap.

Initial Setup

After installing jekyll you’ll need to format your website directory in a way jekyll expects. Jekyll-bootstrap conveniently provides the base directory format.

The Jekyll Application Base Format

Jekyll expects your website directory to be laid out like so:

.
|-- _config.yml
|-- _includes
|-- _layouts
|   |-- default.html
|   |-- post.html
|-- _posts
|   |-- 2011-10-25-open-source-is-good.markdown
|   |-- 2011-04-26-hello-world.markdown
|-- _site
|-- index.html
|-- assets
    |-- css
        |-- style.css
    |-- javascripts
  • _config.yml Stores configuration data.

  • _includes This folder is for partial views.

  • _layouts This folder is for the main templates your content will be inserted into. You can have different layouts for different pages or page sections.

  • _posts This folder contains your dynamic content/posts. the naming format is required to be @YEAR-MONTH-DATE-title.MARKUP@.

  • _site This is where the generated site will be placed once Jekyll is done transforming it.

  • assets This folder is not part of the standard jekyll structure. The assets folder represents any generic folder you happen to create in your root directory. Directories and files not properly formatted for jekyll will be left untouched for you to serve normally.

(read more: https://github.com/mojombo/jekyll/wiki/Usage)

Jekyll Configuration

Jekyll supports various configuration options that are fully outlined here: (https://github.com/mojombo/jekyll/wiki/Configuration)

Content in Jekyll

Content in Jekyll is either a post or a page. These content “objects” get inserted into one or more templates to build the final output for its respective static-page.

Posts and Pages

Both posts and pages should be written in markdown, textile, or HTML and may also contain Liquid templating syntax. Both posts and pages can have meta-data assigned on a per-page basis such as title, url path, as well as arbitrary custom meta-data.

Working With Posts

Creating a Post Posts are created by properly formatting a file and placing it the _posts folder.

Formatting A post must have a valid filename in the form YEAR-MONTH-DATE-title.MARKUP and be placed in the _posts directory. If the data format is invalid Jekyll will not recognize the file as a post. The date and title are automatically parsed from the filename of the post file. Additionally, each file must have YAML Front-Matter prepended to its content. YAML Front-Matter is a valid YAML syntax specifying meta-data for the given file.

Order Ordering is an important part of Jekyll but it is hard to specify a custom ordering strategy. Only reverse chronological and chronological ordering is supported in Jekyll.

Since the date is hard-coded into the filename format, to change the order, you must change the dates in the filenames.

Tags Posts can have tags associated with them as part of their meta-data. Tags may be placed on posts by providing them in the post’s YAML front matter. You have access to the post-specific tags in the templates. These tags also get added to the sitewide collection.

Categories Posts may be categorized by providing one or more categories in the YAML front matter. Categories offer more significance over tags in that they can be reflected in the URL path to the given post. Note categories in Jekyll work in a specific way. If you define more than one category you are defining a category hierarchy “set”. Example:

---
title :  Hello World
categories : [lessons, beginner]
---

This defines the category hierarchy “lessons/beginner”. Note this is one category node in Jekyll. You won’t find “lessons” and “beginner” as two separate categories unless you define them elsewhere as singular categories.

Working With Pages

Creating a Page Pages are created by properly formatting a file and placing it anywhere in the root directory or subdirectories that do not start with an underscore.

Formatting In order to register as a Jekyll page the file must contain YAML Front-Matter. Registering a page means 1) that Jekyll will process the page and 2) that the page object will be available in the site.pages array for inclusion into your templates.

Categories and Tags Pages do not compute categories nor tags so defining them will have no effect.

Sub-Directories If pages are defined in sub-directories, the path to the page will be reflected in the url. Example:

.
|-- people
    |-- bob
        |-- essay.html

This page will be available at http://yourdomain.com/people/bob/essay.html

Recommended Pages

  • index.html You will always want to define the root index.html page as this will display on your root URL.
  • 404.html Create a root 404.html page and GitHub Pages will serve it as your 404 response.
  • sitemap.html Generating a sitemap is good practice for SEO.
  • about.html A nice about page is easy to do and gives the human perspective to your website.

Templates in Jekyll

Templates are used to contain a page’s or post’s content. All templates have access to a global site object variable: site as well as a page object variable: page. The site variable holds all accessible content and metadata relative to the site. The page variable holds accessible data for the given page or post being rendered at that point.

Create a Template Templates are created by properly formatting a file and placing it in the _layouts directory.

Formatting Templates should be coded in HTML and contain YAML Front Matter. All templates can contain Liquid code to work with your site’s data.

Rending Page/Post Content in a Template There is a special variable in all templates named : content. The content variable holds the page/post content including any sub-template content previously defined. Render the content variable wherever you want your main content to be injected into your template:

...
<body>
  <div id="sidebar"> ... </div>
  <div id="main">
    {{content}}
  </div>
</body>
...

Sub-Templates

Sub-templates are exactly templates with the only difference being they define another “root” layout/template within their YAML Front Matter. This essentially means a template will render inside of another template.

Includes

In Jekyll you can define include files by placing them in the _includes folder. Includes are NOT templates, rather they are just code snippets that get included into templates. In this way, you can treat the code inside includes as if it was native to the parent template.

Any valid template code may be used in includes.

Using Liquid for Templating

Templating is perhaps the most confusing and frustrating part of Jekyll. This is mainly due to the fact that Jekyll templates must use the Liquid Templating Language.

What is Liquid?

Liquid is a secure templating language developed by Shopify. Liquid is designed for end-users to be able to execute logic within template files without imposing any security risk on the hosting server.

Jekyll uses Liquid to generate the post content within the final page layout structure and as the primary interface for working with your site and post/page data.

Why Do We Have to Use Liquid?

GitHub uses Jekyll to power GitHub Pages. GitHub cannot afford to run arbitrary code on their servers so they lock developers down via Liquid.

Liquid is Not Programmer-Friendly.

The short story is liquid is not real code and its not intended to execute real code. The point being you can’t do jackshit in liquid that hasn’t been allowed explicitly by the implementation. What’s more you can only access data-structures that have been explicitly passed to the template.

In Jekyll’s case it is not possible to alter what is passed to Liquid without hacking the gem or running custom plugins. Both of which cannot be supported by GitHub Pages.

As a programmer - this is very frustrating.

But rather than look a gift horse in the mouth we are going to suck it up and view it as an opportunity to work around limitations and adopt client-side solutions when possible.

Aside My personal stance is to not invest time trying to hack liquid. It’s really unnecessary from a programmer’s perspective. That is to say if you have the ability to run custom plugins (i.e. run arbitrary ruby code) you are better off sticking with ruby. Toward that end I’ve built Mustache-with-Jekyll

Static Assets

Static assets are any file in the root or non-underscored subfolders that are not pages. That is they have no valid YAML Front Matter and are thus not treated as Jekyll Pages.

Static assets should be used for images, css, and javascript files.

How Jekyll Parses Files

Remember Jekyll is a processing engine. There are two main types of parsing in Jekyll.

  • Content parsing. This is done with textile or markdown.
  • Template parsing. This is done with the liquid templating language.

And thus there are two main types of file formats needed for this parsing.

  • Post and Page files. All content in Jekyll is either a post or a page so valid posts and pages are parsed with markdown or textile.
  • Template files. These files go in _layouts folder and contain your blogs templates. They should be made in HTML with the help of Liquid syntax. Since include files are simply injected into templates they are essentially parsed as if they were native to the template.

Arbitrary files and folders. Files that are not valid pages are treated as static content and pass through Jekyll untouched and reside on your blog in the exact structure and format they originally existed in.

Formatting Files for Parsing.

We’ve outlined the need for valid formatting using YAML Front Matter. Templates, posts, and pages all need to provide valid YAML Front Matter even if the Matter is empty. This is the only way Jekyll knows you want the file processed.

YAML Front Matter must be prepended to the top of template/post/page files:

---
layout: post
category : pages
tags : [how-to, jekyll]
---

... contents ...

Three hyphens on a new line start the Front-Matter block and three hyphens on a new line end the block. The data inside the block must be valid YAML.

Configuration parameters for YAML Front-Matter is outlined here: A comprehensive explanation of YAML Front Matter

Defining Layouts for Posts and Templates Parsing.

The layout parameter in the YAML Front Matter defines the template file for which the given post or template should be injected into. If a template file specifies its own layout, it is effectively being used as a sub-template. That is to say loading a post file into a template file that refers to another template file with work in the way you’d expect; as a nested sub-template.

How Jekyll Generates the Final Static Files.

Ultimately, Jekyll’s job is to generate a static representation of your website. The following is an outline of how that’s done:

  1. Jekyll collects data. Jekyll scans the posts directory and collects all posts files as post objects. It then scans the layout assets and collects those and finally scans other directories in search of pages.

  2. Jekyll computes data. Jekyll takes these objects, computes metadata (permalinks, tags, categories, titles, dates) from them and constructs one big site object that holds all the posts, pages, layouts, and respective metadata. At this stage your site is one big computed ruby object.

  3. Jekyll liquifies posts and templates. Next jekyll loops through each post file and converts (through markdown or textile) and liquifies the post inside of its respective layout(s). Once the post is parsed and liquified inside the the proper layout structure, the layout itself is “liquified”. Liquification is defined as follows: Jekyll initiates a Liquid template, and passes a simpler hash representation of the ruby site object as well as a simpler hash representation of the ruby post object. These simplified data structures are what you have access to in the templates.

  4. Jekyll generates output. Finally the liquid templates are “rendered”, thereby processing any liquid syntax provided in the templates and saving the final, static representation of the file.

Notes. Because Jekyll computes the entire site in one fell swoop, each template is given access to a global site hash that contains useful data. It is this data that you’ll iterate through and format using the Liquid tags and filters in order to render it onto a given page.

Remember, in Jekyll you are an end-user. Your API has only two components:

  1. The manner in which you setup your directory.
  2. The liquid syntax and variables passed into the liquid templates.

All the data objects available to you in the templates via Liquid are outlined in the API Section of Jekyll-Bootstrap. You can also read the original documentation here: https://github.com/mojombo/jekyll/wiki/Template-Data

Conclusion

I hope this paints a clearer picture of what Jekyll is doing and why it works the way it does. As noted, our main programming constraint is the fact that our API is limited to what is accessible via Liquid and Liquid only.

Jekyll-bootstrap is intended to provide helper methods and strategies aimed at making it more intuitive and easier to work with Jekyll =)

Thank you for reading this far.

Next Steps

Please take a look at or jump right into Usage if you’d like.


2011-12-29 intro , beginner , jekyll , tutorial

电子书

最近文章

  • 时隔 5 年再安装 NextCloud 时隔多年,我再安装了一次 [[NextCloud]],很早之前 就在我的 QNAP TS 453B mini 上安装并使用了多年,这两年也一直在跟随着官方的版本升级 ,但是 QNAP 毕竟在局域网内,虽然可以使用 Tailscale,ZeroTier 等等工具来组件局域网,但毕竟还是不方便,最近入手一台还不错的 VPS,所以想着再搭建一个公有 IP 的 NextCloud,一方面备份一下自己的相册,另一方面也补足一下我自己使用 Syncthing时没有在线预览的页面,导致常常有些时候想访问自己的笔记而找不到。
  • 使用 SyncTV 异地远程一起看视频 因为和女朋友异地,之前我们都一直使用 [[Plex]] 来同步看视频,基本上可以做到秒级别的共享观看。今天看 GitHub 的时候发现一款叫做 SyncTV 的应用,也是可以通过同步的方式了共享观看视频流,那这样就可以一起看视频,看直播了。原来 Plex 的因为放在了一台欧洲的服务器上,在国内播放的时候总是会出现卡顿,一来是带宽限制,二来是经常发现有转码,所以常常需要我预先转码为低码率的视频才能流畅播放,出现的这个 SyncTV ,我可以搭配一台地理位置比较好的,比如日本的机器,然后通过代理的方式播放,不知道会不会提升一下使用体验。
  • 《被讨厌的勇气》读书笔记 怎么知道的这一本书
  • 修复 macOS 时区和时间错误 今天 macOS 上又发生了一个奇怪的问题,昨天晚上因为没有连接充电线,所以导致 MacBook 晚上自动关机了,看起来是因为没电自动关机了,但是早上看的时候还有一点点电,白天的时候没有任何操作就直接充电到满,但是晚一些使用的时候就发现系统的时间不太对了,在系统设置中强制同步系统时间,但没有用处,每一次同步都比当前的时间慢了一天和几个小时,甚至连分钟都对不上。于是就开始了一系列的修复过程。
  • 为播客爱好者制作的工具 Podwise Podwise 是一个针对播客的笔记软件,过去很长的一段时间里面都在寻找能够给播客记笔记的应用,之前在移动端发现了一个 [[Snipd]],可以在听到的时候,记录下播客中的精彩瞬间,还可以做笔记。但今天发现的这一款 Podwise 是基于网页的,可以在网页中播放,并且 Podwise 提供了语音转写的文字稿。