Linux Cheatsheet
Users managementHistory
Network
Permissions
Redirection
Network sniffer
systemd
text manipulation - awk and sed
System info
What | How |
---|---|
name of host | hostname |
FQDN of host | hostname -A |
IP address of host | hostname -i |
memory usage | free -h |
Linux flavor | cat /etc/*-release cat /proc/version lsb_release -a |
disk usage | df -h |
folders size | du -ah |
total disk usage off the current directory | du -sh |
History tool
What | How |
---|---|
Run last command starting with abc | !abc |
Print last command starting with abc | !abc:p |
Last argument of previous command | !$ |
Search history | CONTROL-R |
All arguments of last command | !* |
First argument of last command | !^ |
Take second argument of last cp command | ls -l !cp:2 |
Network
netstat examples
View Only Established Connection | netstat -natu | grep 'ESTABLISHED' -a = Show both listening and non-listening (for TCP this means established connections) sockets -t = tcp -u = tudp -n = Show numerical addresses instead of trying to determine symbolic host |
Listing all the LISTENING Ports of TCP and UDP connections | netstat -a |
Listing all LISTENING Connections | netstat -l -l = Show only listening sockets |
Listing TCP Ports connections | netstat -at |
Listing all TCP Listening Ports | netstat -lt |
Listing UDP Ports connections | netstat -au |
View Port Number used by PID | netstat -anlp | grep $PID |
which process using particular port | netstat -anlp | grep portnumber |
Users management
Groups
What | How |
---|---|
/etc/group | [Group name]:[Group password]:[GID]:[Group members] |
Group of current user | groups |
Groups an user is a member of | groups tecmint id tecmint (first listed group is primary group) |
List existing groups | getent group |
Deleting a group | groupdel [group_name] |
Create group | groupadd mynewgroup |
Users
What
|
How
|
---|---|
/etc/passwd
|
[username]:[x]:[UID]:[GID]:[Comment]:[Home directory]:[Default shell]
|
Change user shell
| usermod --shell /bin/sh tecmint |
Adding a user to a primary group and supplementary group | useradd -g “head” -G “faculty” anirban -g = primary group |
Add user to group | usermod -a -G group user |
Define primary group of user | usermod -g groupname username |
View the numerical IDs associated with each group | id |
Other:
What | How |
---|---|
Creating a new group for read and write access to files that need to be accessed by several users | groupadd common_group # Add a new group chown :common_group common.txt # Change the group owner of common.txt to common_group usermod -aG common_group user1 # Add user1 to common_group usermod -aG common_group user2 # Add user2 to common_group usermod -aG common_group user3 # Add user3 to common_group ======================================================= |
Look for files and replace strings in it | find . -type f -exec grep -q "opt/octane" {} \; -print -exec sed -i "s+/opt/octane+/temp/octane+g" {} \; |
Permissions
Unix Permissions Calculator 1
Unix Permissions Calculator 2
0=--- 1=--x 2=-w- 3=-wx 4=r- 5=r-x 6=rw- 7=rwx
644 u+rw-, g+r, o+r
750 u+rwx, g+rx
755 u+rwx, g+rx, o+rx
760 u+rwx, g+rw
770 u+rwx, g+rwx
Unix Permissions Calculator 2
0=--- 1=--x 2=-w- 3=-wx 4=r- 5=r-x 6=rw- 7=rwx
644 u+rw-, g+r, o+r
750 u+rwx, g+rx
755 u+rwx, g+rx, o+rx
760 u+rwx, g+rw
770 u+rwx, g+rwx
What | How |
---|---|
owner | (u) The Owner permissions apply only the owner |
group | (g) The Group permissions apply only to the group that has been assigned to the file or directory |
all users | (o) The All Users permissions apply to all other users |
add permissions | chmod a+rw file1 |
remove permissions | chmod a-rw file1 |
binary represntation of permissions | read = 4 write = 2 execute = 1 |
changing groups of files & directories | chgrp <group> <file> |
changing ownership | chown -R tom <folder> chown -R tom:sales <folder> |
IO Redirection
What | How |
---|---|
Error output (stderr) of cmd to file | cmd 2> file |
stdout to same place as stderr | cmd 1>&2 |
stderr to same place as stdout | cmd 2>&1 |
Every output of cmd to file | cmd &> file |
Network sniffer
What | How |
---|---|
Capture and display all packets on interface eth0 | tcpdump -i eth0 |
Monitor all traffic on port 80 | tcpdump -i eth0 'port 80' |
systemd
What | How |
---|---|
Start service | systemctl start foobar |
Stop service | systemctl stop foobar |
low-level properties of a unit | systemctl show foobar |
Loaded services | systemctl -t service |
Installed services | systemctl list-unit-files -t service systemctl list-units --all |
low-level properties of a unit | systemctl show foobar |
Loaded services | systemctl -t service |
Read all the service options | man systemd.service |
Run if .service file has changed | systemctl daemon-reload |
See all systemd logs | journalctl journalctl -f journalctl -u my_daemon.service |
Display an overview of overridden or modified unit files | systemd-delta |
Text manipulation
What | How |
---|---|
print only certain fields | awk '/a/ {print $3 "\t" $4}' marks.txt |
count and print the number of lines | awk '/a/{++cnt} END {print "Count = ", cnt}' marks.txt |
print lines that contain more than 18 characters | awk 'length($0) > 18' marks.txt |
Split by different character | awk -F":" '{ print $1 " " $3 }' /etc/passwd |
rint only lines that contain a floating point number | /[0-9]+\.[0-9]*/ { print } |
sed delete | second line: sed '2d' myfile range: sed '2,3d' myfile third line to the end: sed '3,$d' myfile by pattern: sed '/test 1/d' myfile range: sed '/second/,/fourth/d' myfile |
sed replace | specifying occurrence number: sed 's/test/another test/2' myfile prints line with pattern, -n = to print modified lines only: sed -n 's/test/another test/p' myfile use the exclamation mark (!) as string delimiter: sed 's!/bin/bash!/bin/csh!' /etc/passwd limit to range of lines: sed '2,3s/test/another test/' myfile |
Comments