General Info
uname -a
– Print operating system namelsb_release -a
– print distribution specific informationprintenv
– print enviroment variablesw
– display who is logged in and what they are doingpwd
– display Present Working Directorywhich <CMD>
– print the location of the CMD executable
Hardware Info
Permissions
chmod
– modify permissions of a file.- example:
chmod 600 file.txt
- OGA (Owner, Group and All) O, G and A are numbers from 0 to 7.
- Each number can be seen as the sum of these three numbers:
- 4 - Read (100 in binary)
- 2 - Write (010 in binary)
- 1 - eXecute (001 in binary)
- You can also add Read, Write or eXecution permission for all users:
chmod +[r|w|x] file.txt
- example:
chown
– change owner of a file.- example:
chown user:group /dir
- example:
Basic Functions
-
shutdown
– shutdown the computer date
- get current time:
date +%s
- get time from a specific moment:
date +%s -d "2016-08-17 20:15:03"
- Convert and print number of seconds to “day hour minute second” format:
echo "Duration: $(($d/86400))d $(($d/3600%24))h $(($d/60%60))m $(($d%60))s"
- get current time:
find
– walk a file hierarchy- parameters
-name [<NAME>|"<NAME>"]
– True if last component of the pathname being examined matches pattern.-path [<PATH>|"<PATH>"]
– True if the pathname being examined matches pattern.
- parameters
Processes
Detach scripts/commands from shell:
Reference: nohup Execute Commands After You Exit From a Shell Prompt
Run commands in the background:
You can append &
at the end of a command to run in the background.
This is useful when you have only one terminal and you want to run a command that takes too long to end or you want it to run it indefinitely, e.g.: tcpdump
and you also want to execute another command in parallel.
Reference: Unix job control command list
Also:
jobs
– List background processespgrep <process_name>
– get pid of a processkill PID
– kill the process with this PID. Without parameters equals to pressing Ctrl+C when the process is in the foreground.
Filesystem
Common directories:
/home
– Linux User Accounts Home Directory and FTP server home directory (some distros)/etc
– Linux Server’s and Applications System’s Configuration Files/usr
– Linux System Files (Shareable, Read-only Data)/bin
– Binary Files for user applications/sbin
– Binary Files for system programs
SSH Filesystem:
# Install SSHFS
sudo apt-get install sshfs
#Create a directory to mount the filesystem
mkdir /mnt/remote_sshfs
#Mount the filesystem
sshfs user@host:dir /mnt/remote_sshfs
#Unmount the filesystem
umount /mnt/remote_sshfs
Reference: How To Use SSHFS to Mount Remote File Systems Over SSH
Disks
partition tables:
fdisk
– manipulate disk partition table
Reference: How to delete a partition with fdisk command
info about disks:
lshw
– is a small tool to provide detailed information on the hardware configuration of the machine. It can report exact memory configuration, firmware version, mainboard configuration, CPU version and speed, cache configuration, bus speed, etc.lshw -class disk
– get info about disks
Reference: Retrieve Disk Info from the Command Linelsblk
– view mounted devicesdu
– disk usage
useful parameters:-h
: for human readable sizes-d NUMBER
: the maximum depth in the output
If you want to order the result by size you can send the output to sort like this:
du -h | sort -h
Reference: Size of a directory & Free disk space
modify labels:
e2label
– change the label of an ext2,3,4 partitionntfslabel
– change the label of a ntfs partition (fromntfs-3g
package)
clean disks:
shred
– overwrite a file to hide its contents, and optionally delete it
Reference Why deleting just isn’t enough
Text Editor
vim
vimtutorial
/vimtutor
- Vim Awesome
User Management
adduser <username>
– asking for password, will create home directory and asking lots of information about the user.useradd <username>
– not asking for password, no home directory just only add new user.
Package Manager
APT
apt-get install --only-upgrade <package>
– to only upgrade a specific packageapt show [package]
– info about a packagesudo rm /var/lib/apt/lists/lock
– unlock apt-get when it get locked (sometimes this happens when you Ctrl+C apt-get when it’s running)
logs:
All actions with apt (apt-get) are logged. These files are available in /var/log/apt/
. To view the most recent history log, execute: less /var/log/apt/history.log
.
These logs gets rotated (every month I guess), old files will be suffixed with a number and compressed. So to view the next history log, use: zless /var/log/apt/history.log.1.gz
.
RPM
rpm -qi [package]
– info about a package- RPM naming convention
SSH
ssh-add -K ~/.ssh/<private_key_file>
– to add a private key to the keychain
Debug
view shared/dynamic libraries used by a process:
ldd
(Linux)otool -L
(MacOS)
Multiple terminals
tmux
– terminal multiplexerCtrl
+b
%
– Split pane horizontallyCtrl
+b
"
– Split pane verticallyCtrl
+b
<arrow-key>
– Switch paneCtrl
+d
– Close paneCtrl
+b
d
– Detach current sessionCtrl
+b
D
– Detach specific session (selected from list)tmux ls
– view running sessiontmux attach -t 0
– attach to session 0tmux new -s database
– create a session with nametmux rename-session -t 0 database
– rename existing sessiontmux attach -t database
– attach to a named sessionCtrl
+b
?
– list of available commandsCtrl
+b
z
– go fullscreen (press again to go restore)Ctrl
+b
Ctrl
+<arrow-key>
– Resize pane in direction of key
Reference: A quick and easy guide to tmux
Network
nc
/netcat
– arbitrary TCP and UDP connections and listensnc -l localhost 4000
– listen in localhost:4000
Reference: Netcat – a couple of useful examples