Thursday, January 14, 2021

Common vim and linux terminal command, and setx for Windows

1. vim:

switch to input mode from command mode


[esc]

switch to command mode from input mode


:wq 

save and quit in command mode


:/

search a text in command mode


:dd

delete a line in command mode


:set number

show line numbers


:u

undo


2. Linux bash

grep textToSearch

Text search in lines


grep -i textToSearch

Text search in lines, ignore case


clear

clear screen


ps -aux

print current running process information, like PID, command line parameters


watch command -n watchinterval

execute command repeatedly in the specified interval (default is 2s)

watch date


cat filename

show file content


pwd

get current directory


ls

list all file and folder in the current directory


env
show all environment variables


mkdir -p path
Create path as well as all missing parent path


sed -i -e "s|old1|new1|g" -e "s\old2|new2|g" path/filename
in place replacement (-i) of strings in path/filename from "old1" to "new1","old2" to "new2"

echo 'some string' | base64 
base64 encode an string

echo 'some encoded string' | base64 --decode
decode base64 encoded string

base64 filename > encodedfilename
base 64 encode a file to a new file

base64 --decode encodedfilename > decodefilename
base64 decode a file to decoded file


3. openssl

openssl genrsa -out ca.key 4096
Create key for root ca

openssl req -x509 -new -nodes -key ca.key -sha256 -days 1024 -out ca.crt

create self signed root ca certificate 

openssl genrsa -out server.key 2048
create key for server

openssl req -new -key server.key -out server.csr

create signing request for server certificate


openssl x509 -req -in server.csr -CAcreateserial  -CA ca.crt -CAkey ca.key -out server.crt

create server certificate from signing request by signed with root CA


openssl x509 -in /etc/kubernetes/pki/apiserver.crt -text
view certificate information


4. network

Route table is used for ip to ip routing. Arp table is used for ip to mac address mapping.


show which ip and port are listening from which process

netstat


ip link
show list of network interface on the host

ip addr
show the address of network interface on the host

ip addr add 192.168.1.10/24 dev ech0
set ip address on a network interface ech0

ip addr del 192.168.1.10/24 dev ech0
remove ip address from network interface ech0

ip route
view routing table

ip route add 192.168.1.0/24 via 192.168.2.1
add an entry into the routing table

IP forwarding config file to decide whether network requests can cross different network interface
/proc/sys/net/ipv4/ip_forward
1 is enabled, 0 is disabled

To persist the above config across system restart, update file 
/etc/sysctl.conf

net.ipv4.ip_forward=1


Host name resolution file

/etc/hosts


DNS config file

/etc/resolv.conf


Change DNS lookup order

/etc/nsswitch.conf

hosts:          files dns


Show dns lookup information

nslookup hostnametolookup


list network namespace

ip netns


add network namespace red

ip netns add red


run ip command from namespace of red

ip -n red link

or 

ip netns exec red ip link



create a virtual link (veth type) to connect two interfaces you named: vethred and vethblue

ip link add vethred type veth peer name vethblue


set interface vethred to a particular namespace red

ip link set vethred netns red

ip link set vethblue netns blue



set ip address for the network interface link vethred and vethblue

ip -n red addr add 192.168.15.1/24 dev vethred

ip -n blue addr add 192.168.15.2/24 dev vethblue


enable the interface link to up state

ip -n red link set vethred up

ip -n blue link set vethblue up


ping from one namespace to another using link interface ip

sudo ip netns exec red ping 192.168.15.2


delete interface link

ip -n red link delete vethred


add a virtual bridge v-net-0 of bridge type as switch

ip link add v-net-0 type bridge


bring bridge interface up

ip link set v-net-0 up


verify the status by 

ip link


create link to the bridge

ip link add vethred type veth peer name veth-red-br

ip link add vethblue type veth peer name veth_blue_br


set link to namespace

ip link set vethred netns red

ip link set vethblue netns blue

ip link set veth-red-br master v-net-0


set ip address for the link

ip -n red addr add 192.168.15.1/24 dev vethred

ip -n blue addr add 192.168.15.2/24 dev vethblue



bring link interface up

ip -n red link set vethred up

ip -n blue link set vethblue up


set ip address for bridge

ip addr add 192.168.15.5/24 dev v-net-0

ip addr add 192.168.15.5/24 dev veth-blue-br


ping from one namespace to another

ip netns exec red ping 192.168.15.2


display and config network configuration

ifconfig



setX for windows

Sometimes IT may disable user to access user account settings from control panel, so users cannot update the environment variables. To bypass the issue, you can use setx command, which is quite often missed by IT
setx JAVA_HOME c:\yourJDKFolder 


No comments:

Post a Comment