GNU/Linux Tips

来自Jack's Lab
(版本间的差异)
跳转到: 导航, 搜索
(Static IP)
(Static IP)
第161行: 第161行:
  
 
<source lang=bash>
 
<source lang=bash>
sudo nano /etc/netplan/01-netcfg.yaml
+
$ sudo nano /etc/netplan/01-netcfg.yaml
 +
 
 +
# This file describes the network interfaces available on your system
 +
# For more information, see netplan(5).
 +
network:
 +
  version: 2
 +
  renderer: networkd
 +
  ethernets:
 +
    ens33:
 +
      dhcp4: no
 +
      dhcp6: no
 +
      addresses: [192.168.1.100/24]
 +
      gateway4: 192.168.1.1
 +
      nameservers:
 +
        addresses: [8.8.8.8,8.8.4.4]
 +
 
 +
$ sudo netplan apply
 +
$ sudo netplan --debug apply
 
</source>
 
</source>
  

2019年11月12日 (二) 17:05的版本

目录

1 XPS 9560

1.1 Dual Video Card


1.2 Buffer I/O error


1.3 BIOS

开机按“F2”,进入到Bios界面,修改bios :

  • System Configuration - SATA Operation 改成 AHCI


1.4 Thunderbolt to DP

  • USB-C to DisplayPort adapter/cable: Allows you to use a single DisplayPort 1.2 output, which can drive a single 4K 60 Hz display, dual 2560x1600 60 Hz displays, or up to 3x 1080p displays. The system only supports 3 independent active displays total, fyi.


  • USB-C to HDMI 2.0 adapter/cable: Allows you to run a single display up to 4K at 60 Hz, assuming the display itself supports that via its HDMI input, and not all 4K 60 Hz displays do.


  • USB-C docking station (such as the WD15): This would only allow up to dual 1080p displays or a single 2560x1600 displays at 60 Hz, or a single 4K display only at 30 Hz, since now the USB-C port has to carry both display and USB data, which reduces available display bandwidth. But obviously you get more connectivity with a dock than a simple cable/adapter, and you'd also be able to charge the system with this single cable. If you get the WD15, you'll need the one with the 180W adapter.


  • Thunderbolt 3 to Dual DisplayPort adapter: Allows you to use two DisplayPort 1.2 outputs, which means up to dual 4K 60 Hz displays or 3x 2560x1600 60 Hz displays (if you use DisplayPort daisy chaining). If needed, you can connect active DisplayPort to HDMI/DVI/VGA adapters to this adapter, but not the less expensive and more common passive adapters.


  • TB3 docking station (such as the TB16): Same display capabilities as above since you're now using Thunderbolt rather than USB-C and there's no "penalty" for carrying the other data at the same time like there is with USB-C. If you get the TB16, you'd be able to use passive DisplayPort adapters if needed, and if you wanted to charge your system, you'd need the TB16 version with the 240W adapter.


2 Mirror


3 grub

修复 windows 双启动:

 sudo grub-probe --target=hints_string /boot/efi/EFI/Microsoft/Boot/bootmgfw.efi
 sudo grub-probe --target=fs_uuid /boot/efi/EFI/Microsoft/Boot/bootmgfw.efi
 sudo grub-mkconfig -o /tmp/grub.cfg

其会自动检测所有硬盘上的系统,自动生成配置文件

把新生成的 /tmp/grub.cfg 覆盖 /boot/grub/grub.cfg 即可

参考:https://www.jianshu.com/p/5007e555ec12


4 Bash

4.1 Notes

$ set -- one two
$ echo $1
one
$ echo ${1}
1
$ eval echo ${1}
one

$ n=2
$ echo ${n}
2
$ echo ${!n}
two
$ eval echo \${$n}
two



4.2 Bash readline

#!/bin/bash 
count=1
cat X1A001.dat | while read line        # 从文本文件 X1A001.dat 一行一行的读 
do 
   echo "$count:$line" 
   count=$(($count + 1))
done
exit 0



4.3 命令行历史

1. 忽略当前这条命令
 $ export HISTCONTROL=ignorespace
 $ [space]echo "this is a top secret"


2. 禁用当前会话的所有历史记录
 $ export HISTSIZE=0


3. 工作结束后清除整个历史
 $ history -cw


4. 只针对你的工作关闭历史记录
 $ [space]set +o history

要重新开启:

 $ [space]set -o history


5. 从历史记录中删除指定的命令
 $ history
 $ history -d 3



5 User Add

$ userdel comcat
$ useradd comcat -m -s /bin/bash -p



6 ngrok


7 Static IP

$ sudo nano /etc/netplan/01-netcfg.yaml

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    ens33:
      dhcp4: no
      dhcp6: no
      addresses: [192.168.1.100/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8,8.8.4.4]

$ sudo netplan apply
$ sudo netplan --debug apply


8 Network Monitor

8.1 iftop

$ sudo apt-get install iftop
$ sudo iftop -P -i eth1


8.2 ifstat

$ sudo apt-get install ifstat
$ sudo ifstat -t -i wlan0 2
  Time          wlan0       
HH:MM:SS   KB/s in  KB/s out
11:04:42      0.00      0.00
11:04:44      0.02      0.03
11:04:46      0.12      1.29
11:04:48     32.94      9.75
11:04:50     63.31     12.23


refter to: http://www.binarytides.com/linux-commands-monitor-network/



9 HTTP 抓包

HTTP 协议分析:

$ tshark tcp port 80 or tcp port 443 -V -R "http.request || http.response" -i eth1 |
 sed -rn '/Hypertext Transfer Protocol/{:a;N;/    \\r\\n:?/{/.*/p;d};ba}'


只显示 GET 类请求的头:

  $ tshark 'tcp port 80' -R 'http.request.method == "GET" || http.request.method == "HEAD"' -i eth1



10 Port Scan

# TCP Connect scanning
# nmap -v -sT localhost
# nmap -v -sT 192.168.31.0/24

# nmap TCP SYN (half-open) scanning
# nmap -v -sS localhost

# nmap TCP FIN scanning
# nmap -v -sF localhost

# nmap TCP Xmas tree scanning
# nmap -v -sX localhost

# nmap TCP Null scanning
# nmap -v -sN localhost

nmap TCP RPC scanning
# nmap -v -sR localhost

nmap UDP scanning
# nmap -v -O localhost

nmap remote software version scanning
# nmap -v -sV localhost



11 netcat

11.1 简单web服务器

web 查看系统状态 (TOP):

#!/bin/bash
top -b -n 1 > /tmp/top.txt
app = "nc -l 996"
count=1;

while [ count>0 ]
do
        pn = `ps -wef|grep "${app}" |grep -v grep |grep " 1 "|wc -l`
        echo $pn
        if  test $pn -lt 1
        then
                top -b -n 1 >/tmp/top.txt
                nc -l 996   </tmp/top.txt
        fi
done    



11.2 传送文件

server: nc -v -l -p 1234 > 1.txt

client: nc -v -n 192.168.0.1 1234 < 1.txt
server: $ cat backup.iso | nc -l 3333

client: $ nc 192.168.0.1 3333 > backup.iso


Show the progress of the data transfer:

server: $ cat backup.iso | pv -b | nc -l 3333

client: $ nc 192.168.0.1 3333 | pv -b o> backup.iso



11.3 Other

S: $ dd if=/dev/hdb5 | gzip -9 | nc -l 3333
C: $ nc 192.168.0.1 3333 | pv -b > myhdb5partition.img.gz

S: $ tar -czf - /etc/ | nc -l 3333
C: $ nc 192.168.0.1 3333 | pv -b > mybackup.tar.gz



12 WiFi Config

12.1 wireless tools

$ sudo ip link set wlan0 up
$ sudo iw dev wlan0 scan | less 

$ sudo iw dev wlan0 connect [网络 SSID]
$ sudo iw dev wlan0 connect [网络 SSID] key 0:[WEP 密钥]   # For WEP


$ cat /etc/wpa_supplicant/my.conf
network={
    ssid="[网络 ssid]"
    psk="[密码]"
    priority=1
}

$ sudo wpa_supplicant -i wlan0 -c /etc/wpa_supplicant/my.conf &
$ sudo dhclient wlan0


12.2 nmcli

# list your connections
$ sudo nmcli c

$ sudo nmcli c up uuid <paste essid uuid here>



12.3 Reference



13 Sed

1. 关于 -n 的说明 The default operation of sed is to output every input line. The -n option suppresses the automatic output. When specifying this option, each instruction intended to produce output must contain a print command, p. Look at the following example.


2. debian 下读取指定软件包的所有信息(实质上是读取相邻两个空行之间的所有数据)

   sed -n '/^Package: libc6-udeb/{:next;N;//n$/!b next;p}' Packages

PS: 每 N 一行,缓冲区会加入 /n<newline> ,当读入一空行时,缓冲区的最后一个字符就是 /n


3. 读取所有软件包的名字

   sed -n 's/^Package: \(.*\)/\1/p' Packages

PS: \1 表示保留 pattern 中第一个子模式,即匹配括号中模式的予以保留


4. 提取含有特定信息的数据块(比如获取所有 gnome 用到的包)

   sed '/^Package:/{:next;N;//n$/!b next;/Section: gnome/b;d}' Packages


5. sed 中使用 bash 中定义的变量

   sed -n "/^Package: $file/{:next;N;//n$/! b next;p}" Packages

PS: 注意 sed 的命令部分是用“”括起来的,这样可以引用 bash中定义的变量 file;另外!和 b之间 要留有空格否则bash 会报错,因为在bash中!有特殊用途。



14 Grep

1. 提取文件中所有符合模式的字符串:

 egrep -or "MJD[0-9]{12}" /tmp/alldevid



15 Kernel代码格式化

kernel source tree 的 scripts/ 下有一个 Lindent 的脚本:

#!/bin/sh
PARAM="-npro -kr -i8 -ts8 -sob -l80 -ss -ncs -cp1"
RES=`indent --version`
V1=`echo $RES | cut -d' ' -f3 | cut -d'.' -f1`
V2=`echo $RES | cut -d' ' -f3 | cut -d'.' -f2`
V3=`echo $RES | cut -d' ' -f3 | cut -d'.' -f3`
if [ $V1 -gt 2 ]; then
  PARAM="$PARAM -il0"
elif [ $V1 -eq 2 ]; then
  if [ $V2 -gt 2 ]; then
    PARAM="$PARAM -il0";
  elif [ $V2 -eq 2 ]; then
    if [ $V3 -ge 10 ]; then
      PARAM="$PARAM -il0"
    fi
  fi
fi
indent $PARAM "$@"



16 udev

$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 003 Device 002: ID 413c:2003 Dell Computer Corp. Keyboard
Bus 002 Device 003: ID 413c:3016 Dell Computer Corp. Optical 5-Button Wheel Mouse
Bus 002 Device 016: ID 18d1:4e22 Google Inc. Nexus S (debug)

Bus 002 Device 016 就是我的手机,对应的设备节点是 /dev/bus/usb/002/016

如下命令查看设备信息:

$ udevadm info -q all -n /dev/bus/usb/002/016 
P: /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.6
N: bus/usb/002/016
E: BUSNUM=002
E: DEVNAME=/dev/bus/usb/002/016
E: DEVNUM=016
E: DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.6
E: DEVTYPE=usb_device
E: DRIVER=usb
E: ID_BUS=usb
E: ID_MEDIA_PLAYER=google_nexus-s
E: ID_MODEL=SGH-T959
E: ID_MODEL_ENC=SGH-T959
E: ID_MODEL_ID=4e22
E: ID_REVISION=0231
E: ID_SERIAL=samsung_SGH-T959_32334071DA1D00EC
E: ID_SERIAL_SHORT=32334071DA1D00EC
E: ID_USB_INTERFACES=:080650:ff4201:
E: ID_VENDOR=samsung
E: ID_VENDOR_ENC=samsung
E: ID_VENDOR_ID=18d1
E: MAJOR=189
E: MINOR=143
E: PRODUCT=18d1/4e22/231
E: SUBSYSTEM=usb
E: TAGS=:udev-acl:
E: TYPE=0/0/0
E: UDEV_LOG=3
E: USEC_INITIALIZED=599355697049

或者:

$ udevadm info -q path -n /dev/bus/usb/002/016 
/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.6

$ udevadm info -a -p $(udevadm info -q path -n /dev/bus/usb/002/016)

  looking at device '/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.6':
    KERNEL=="2-1.6"
    SUBSYSTEM=="usb"
    DRIVER=="usb"
    ATTR{configuration}==""
    ATTR{bNumInterfaces}==" 2"
    ATTR{bConfigurationValue}=="1"
......
......

根据 http://www.reactivated.net/writing_udev_rules.html#basic 写一个 udev rules 文件 /etc/udev/rules.d/62-android.rules:

# 匹配上设备则将设备节点文件的 mode 改为 0666,建一个 /dev/GalasyS 的符号链接指向它 
SUBSYSTEM=="usb", ENV{ID_VENDOR_ID}=="18d1", ENV{ID_MODEL}=="Galaxy", MODE="0666", SYMLINK+="GalaxyS"
SUBSYSTEM=="usb", ENV{ID_VENDOR_ID}=="18d1", ENV{ID_MODEL_ID}=="4e22", MODE="0666", SYMLINK+="GalaxyS"
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", ATTR{idProduct}=="4e22", ATTR{product}=="Galaxy" MODE="0666", SYMLINK+="GalaxyS"

如对某一行 rule 没底,可用如下命令测试这个 rule:

$ udevadm test --action='"usb", ENV{ID_VENDOR_ID}=="18d1", ENV{ID_MODEL}=="Galaxy", MODE="0666", SYMLINK+=
"GalaxyS"' $(udevadm info -q path -n /dev/bus/usb/002/016)
......
udev_node_add: creating device node '/dev/bus/usb/002/016', devnum=189:143, mode=0664, uid=0, gid=0
udev_node_mknod: preserve file '/dev/bus/usb/002/016', because it has correct dev_t
udev_node_mknod: preserve permissions /dev/bus/usb/002/016, 020664, uid=0, gid=0
node_symlink: preserve already existing symlink '/dev/char/189:143' to '../bus/usb/002/016'
......

确定后重启 udev 即可:

 sudo service udev restart





17 VirtualBox

17.1 Mount VDI image

$ od -j344 -N4 -td4 /work/FreeDos.vdi | awk 'NR==1{print $2;}'
8192

$ sudo losetup -f --show -o 8192 /work/FreeDos.vdi
/dev/loop0

$ sudo fdisk  -lu /dev/loop0

Disk /dev/loop0: 113 MB, 113246208 bytes
16 heads, 63 sectors/track, 219 cylinders, total 221184 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

      Device Boot      Start         End      Blocks   Id  System
/dev/loop0p1   *          63     1023119      511528+   6  FAT16

$ sudo mount -o loop,offset=$((512*63)) /dev/loop0 /mnt

$ ls /mnt/
AUTOEXEC.BAT  BOOTSECT.BIN  COMMAND.COM  FDCONFIG.SYS  FDOS  KERNEL.SYS


$ sudo umount /mnt
$ sudo losetup -d /dev/loop0

$ VBoxManage clonehd /work/FreeDos.vdi /work/freedos.vdi --format VDI --variant Fixe



18 Mount a disk image

$ sudo losetup -fv ./rasper-pi.img 
Loop device is /dev/loop0

$ sudo kpartx -av /dev/loop0
add map loop0p1 (252:0): 0 114688 linear /dev/loop0 8192
add map loop0p2 (252:1): 0 7917568 linear /dev/loop0 122880

$ sudo mount /dev/mapper/loop0p2 /mnt

$ ls /mnt/
bin  boot  dev  etc  home  lib  lost+found  media  mnt  opt  proc  root  run  sbin  selinux  srv  sys  tmp  usr  var



19 GDB Tips

1. 查看寄存器

(gdb) i r
(gdb) i r a                     # 查看所有寄存器(包括浮点、多媒体)
(gdb) i r esp
(gdb) i r pc


2. 查看内存

(gdb) x /wx 0x80040000    # 以16进制显示指定地址处的数据
(gdb) x /8x $esp
(gdb) x /16x $esp+12
(gdb) x /16s 0x86468700   # 以字符串形式显示指定地址处的数据
(gdb) x /24i 0x8048a51      # 以指令形式显示指定地址处的数据(24条)


3. 修改寄存器的值

(gdb) set $v0 = 0x004000000
(gdb) set $epc = 0xbfc00000


4. 修改内存的值

(gdb) set {unsigned int}0x8048a51=0x0
(gdb) set *(unsigned int*)0x8048a54=0x55aa55aa


5. 内存搜索

Usage: find <start> <end> <count> <value>

(gdb) define find                            
set $ptr = $arg0
set $cnt = 0
while ( ($ptr<=$arg1) && ($cnt<$arg2) )
    if ( *(unsigned int *)$ptr == $arg3 )
        x /wx $ptr
        set $cnt = $cnt + 1
    end
    set $ptr = $ptr + 4
end
end


6. 断点、监测点

(gdb) b *0x80400000
(gdb) watch *(unsigned int *)0xbffff400==0x90909090



20 Strace Tips

1. strace 是一个诊断调试工具,其借助内核 ptrace 机制,跟踪目标进程所执行的系统调用和收到的信号。

2. 常用选项

-o filename: 将输出信息写到文件filename中,默出输出到标准错误(stderr)
-c: 统计输出,包括每个系统调用的执行时间、被调用次数等
-tt: 每行前加上相对时间戳
-e trace=open,close: 只跟踪open, close
-e trace=file: 只跟踪以文件名为参数的的调用,等价于 -e trace=open,stat,chmod,unlink...
-e trace=process: 进程管理相关
-e trace=network: 网络相关
-e trace=ipc: 进程间通信相关
-e trace=signal: 信号相关
-e verbose=close,open: vebose模式输出(结构体成员都输出)
-e abbrev=open: 默认情况下所有调用的显示都是abbreviate,只要给这个选项,就只有open为abbrev
-e raw=open: open 调用以16进制形式输出参数
-e read=3,5: 显示文件描述符3、5的读取数据
-e write=3,5
-p pid: 绑定到pid对应的正在运行的进程。此参数常用来调试后台进程
-f: 除了跟踪当前进程外,还跟踪其子进程
-ff: 当跟踪多个进程且输出到文件时,使用该选项则将各个进程的跟踪信息写入各个filename.pid中



21 Git

git diff的时候忽略换行符的差异:

 git config --global core.whitespace cr-at-eol


http proxy:

 git config --global http.proxy 'socks5://127.0.0.1:8080'


git server:

$ apt-get install git-daemon-sysvinit
$ cat /etc/default/git-daemon
GIT_DAEMON_ENABLE=true
GIT_DAEMON_USER=gitdaemon

GIT_DAEMON_DIRECTORY=/var/data/git
GIT_DAEMON_BASE_PATH=/var/data/git

GIT_DAEMON_OPTIONS="--export-all --enable=upload-pack --enable=upload-archive --enable=receive-pack --informative-errors"


修改.git/config添加如下代码:

[receive]
    denyCurrentBranch = ignore


在初始化远程仓库时最好使用 git --bare init 而不要使用 git init

如果使用了git init初始化,则远程仓库的目录下,也包含work tree,当本地仓库向远程仓库push时, 如果远程仓库正在push的分支上(不在push分支,没有问题), 那么push后的结果不会反应在work tree上, 也即在远程仓库的目录下对应的文件还是之前的内容,必须得使用git reset --hard才能看到push后的内容



21.1 Tag

列显已有的标签:

$ git tag
v0.1
v1.3
$ git tag -l 'v1.4.2.*'
v1.4.2.1
v1.4.2.2
v1.4.2.3


创建 Tag:

$ git tag -a v1.4 -m 'my version 1.4'
$ git tag v1.4-lw
$ git tag -s v1.5 -m 'my signed 1.5 tag'


后期加注标签:

$ git log --pretty=oneline
150279