Friday, August 30, 2019

Linux Users Configuration 使用者設定


Create a new user called 'jenkins' (create account)
sudo adduser jenkins

Delete a user called 'jenkins' (delete account)
sudo deluser jenkins

Add 'jenkins' to sudoer group (join to group)
sudo usermod -aG sudo jenkins

Remove 'jenkins' from sudoer group (delete from group)
sudo gpasswd -d jenkins sudo

Check any groups joined by this user (check joined group)
id jenkins

Check 'jenkins' user id (check userid)
id -u jenkins

Check group file
sudo vi /etc/group

Add 'jenkins' to the sudoers without requiring password all the time

sudo vi /etc/sudoers
jenkins   ALL=(ALL) NOPASSWD: ALL      #add this line
:wq                                    #save and exit 

Thursday, August 29, 2019

Linux Networking 網絡功能

> ifconfig
Bring network up
ifconfig <interface> up

Bring network up with specific IP
ifconfig eth0 172.16.25.125

Bring network up with all parameters
ifconfig eth0 172.16.25.125 netmask 255.255.255.224 broadcast 172.16.25.63

Bring network down
ifconfig <interface> down

Simulate network outrage for 500 ms

ifconfig eth0 down; usleep 500; ifconfig eth0 up 

> DNS
Check currently-using DNS server
cat /etc/resolv.conf

Make simple DNS request

host yahoo.com
dig yahoo.com
nslookup yahoo.com 

> IGMP Group + AWK
Complicated Log Simplification with AWK
cat /proc/net/igmp
awk -F' ' 'NF==5 {print "INT " $2} NF==4 {print "GP " $1}' /proc/net/igmp
#explanation: if row num of field = 5, show INT and the second variable

#explanation: if row num of field = 4, show GP and the first variable

 

Tuesday, August 27, 2019

Vim Notes 筆記

Up / Down / Left / Right
h - move left (←) (not high!)
l - move right (→) (not low!)
j - move up ()
k - move down ()

H - move to the top of the screen (High)
L - move to the end of the screen (Low)
Move between windows
Ctrl+W then ↑ or ↓ or ← or → - move cursor to next window
Ctrl+W then H or L           - move vertical left right
Ctrl+W then J or K           - move horizontally up down

Move to last line / first line
:$     - move to last line
:0     - move to the first line
:<num> - move to <number> line

Move in the line
HOME or 0 - move to the beginning of the line
$ or END  - move to the end of the line

Switch between tabs
gt - go to next tab
gT - go to previous tab

Ctrl+PgUp - go to previous tab (will conflict with GUI)
Ctrl+PgDown - go to next tab (will conflict with GUI)

Search ignore case
/\ckeyword

Search with case
/\CkEyWord

Search Next
/ or n

Search Previous
? or N

Search keyword under cursor and Jump
*

Jump to variable declaration (a.k.a. go declaration)
gd     #goto declaration

Jump back to previous cursor
g;

Undo / Redo
u / Ctrl+U

Insert new line
o

Replace
Insert or R or r

Reload without saving
:edit! or :e!

Forgot to sudo - gain sudo vim access when you are readonly
:w !sudo tee %

To delete all lines matching a certain regular expression pattern, use
:g/pattern/d

To do the opposite, i.e. only keep those lines matching the pattern, use
:v/pattern/d

And to only temporarily print matching lines, use
:g/pattern

Set mouse enable in .vimrc
set mouse=a #enable
set mouse=c #disable

Set display line number
set number

Get current file path
:echo @%
:echo expand('%:p')

Set Syntax Highlights on bash scripts without shebang
:set syn=sh

During typing, you want autocomplete
ctrl-N in insert mode

Copy even if you're using set mouse=a
This is actually simple: by accessing register "+" or "*" in Linux 
  1. select text using any mode
  2. yank the text to register +
"+y

Paste even if you're using set mouse=a
This is actually simple: by accessing register "+" or "*" in Linux
  1. go to insert mode
  2. paste the text from register + (during insert mode)
Ctrl-R and then +
or you can do this (during command mode)
"+p

Find and Replace (Substitute)
Full command
:substitute

Find all occurrence of 'foo' and replace it with 'bar'.
:%s/foo/bar/g

Find each occurrence of 'foo' (in the current line only), and replace it with 'bar'.
:s/foo/bar/g