Lnx Flashcards
Remote command execution
Linux: How to run a command on a remote machine.
Use ssh,
> ssh laeeq@192.168.0.49 ls /tmp > ssh laeeq@192.168.0.49 date
you may have to give absolute path of the command you want to execute.
How to find state of a port (3000)?
> sudo lsof -i:9950 # Linux > netstat -an | grep 9550 # Linux > netstat -an | find /I "9550" # Windows
tcpdump
What is tcpdump command to get traffic to/from port 3000
> tcpdump -i any port 3000
vimdiff
What are vimdiff commands?
- CTL-ww swhitches windows
- do: diff obtain, get difference from other window
- dp: diff put: send difference to other window
- ]c: go to next diff
- [c: go to previous diff
- zo: open zip
- zc: close zip
core file
How to force core file generation from a crashing program.
> ulimit -c unlimited
How to display current resource limits for a user?
> ulimit -a
gdb
How to set a conditional break point in gdb.
b fileName.c:56 if x==50
Dangling symbolic links
What happens when you delete a symbolic link.
The target or actual file remains unaffected.
However, if u delete the target, the symbolic link continues to point to non-existing target file (this called dangling/orphaned symbolic links).
Soft link = Symbolic link
what does the tee
command do?
tee
cmd is used to copy the output of a command to a file.
In addition to displaying normal output of the command, the output is also written to a file.
It is used with pipe symbol
> wc -l prog.c | tee file1.txt
to append to a file instead of overwriting,
> wc -l prog.c | tee -a file1.txt
How is HereDocument used?
When a large amount of textual multiline data is needed as argument to a command, we can use Here Docuement
and provide data inside shell script itself, rather than providing data in a file.
cat << ABCD line 1111 line 222 line 333 ABCD
Pass envirenment variable to a script
How to make a variable available inside a shell script?
> TARGET=x86 ./script.sh
Value of TARGET will be available inside script.sh
Assign values to env variables just before calling the script on the same line.
How to read a variable inside a shell script from keyboard?
Use read
command,
read variableName echo $variableName
What is a common usage of xargs
?
When a command generates a list of files, such as find
command. We can make this file list available to another command like grep
or rm
etc, so that these commands can work on the files list generated by the first command.
Following can be used to delete all header files found by cmd find
> find . -name "*.h" | xargs rm -v
args is normally used after a pipe symbol
Errors in make file recipes
In make file what happens when an error occurs at any time?
Default behaviour: Make program is aborts immediately, no further commands are executed.
If you pre-append minus sign ‘-‘ to any command, error in that command will be ignored and next command will be executed.
If you pre-append ‘@’ to any command, make will not print that command before executing.
What are common automatic variables in makefiles?
- $@ : Name of current target.
- $^ : List of all dependencies
- $< : First dependency in the list of dependencies.
gdb: How to get the sequence of function calls leading to current point of execution?
bt backtrace
Each stack frame will have a number associated with it. Top most stack frame is the most recent one.
gdb: How to get the local variables in current function?
info locals
Command line editing: Go to begining of line.
CTL a
a = begining
Command line editing: Go to end of line.
CTL e
e = end
Command line editing
Command line editing: Delete from curser positionn to the end of line?
CTL k
k = Kill
Command line editing
Command line editing: Clear the whole current line
CTL ak
ak = All Kill
iptables: name three chains
INPUT chain, OUTPUT chain, FORWARD chain
What is an iptables policy?
Each chain has a policy, ACCEPT or DROP.
ACCEPT: Allow all pkts unless a specific rule forbids that kind of pkt.
DROP: Disallow all pkts unless a specific rule allows that kind of pkt.
What are primary and secondary groups in Linux?
Primary group: Has the same name as user name. Created at the time when user is created. Newly created user has membership of this group immediately.
Secondary group: Other groups created separately. Users need to be made members of secondary groups explictly using cmd gpasswd
> gpasswd groupName -a userName
Linux user groups
How to find to which groups a user belongs to?
> groups laeeq > groups # will give info about current user.
You can also use,
~~~
> id laeeq
> id # Lists groups which logged in user is member of
```
You can also read /etc/group file.