Today I am focusing on Linux server administrator Snippets and commands. These are quick pieces of code and commands that you can call upon within a server environment to get a job done quickly.
Many years ago, I was taught that the command line whether it be Linux or Windows is a powerful tool. The advice given here was although the command line is powerful it is also unforgiving. So, be very careful when using certain commands on the command line. This applies to the Windows command line as well.
Did you know that some folks who do not want to type in the code use snippet managers? These programs allow you to save your snippets and reuse them as needed. Check this out for reference and maybe a new resource for you to use.
So if you’re a new admin or a seasoned veteran I have a few snippets for you to add to your library.
1. Free Memory in Linux
free -h && sudo sysctl -w vm.drop_caches=3 && sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches && free -h
2. Free Page Cache in Linux
1. Clear PageCache only
sync; echo 1 > /proc/sys/vm/drop_caches
2. Clear dentries and inodes
sync; echo 2 > /proc/sys/vm/drop_caches
3. Clear PageCache, dentries and inodes
sync; echo 3 > /proc/sys/vm/drop_caches
3. Check the Uptime
uptime [options]
Options:
-p, --pretty show uptime in pretty format
-h, --help display this help and exit
-s, --since system up since
-V, --version output version information and exit
4. List All Users in Linux
cat /etc/passwd
5. Clear the Linux Command Line
clear
or
Ctrl + L
6. Top IPs Connecting to Your Linux Server
netstat -ntu -4 -6 | awk '/^tcp/{ print $5 }' | sed -r 's/:[0-9]+$//' | sort | uniq -c | sort -n
7. Reboot Linux
#(General Linux)
sudo reboot
or
sudo shutdown –r
#(Cento OS Linux)
reboot -h now
8. Force Log Rotate in Linux
$ logrotate -vf CONFIG_FILE
9. Scan for Active Devices on your Linux Network
#Get IP address to mac conversion and the allocation type of devices on your network.
arp -a
Usage:
arp [-vn] [] [-i ] [-a] [] <-Display ARP cache
arp [-v] [-i ] -d [pub] <-Delete ARP entry
arp [-vnD] [] [-i ] -f [] <-Add entry from file
arp [-v] [] [-i ] -s [temp] <-Add entry
arp [-v] [] [-i ] -Ds [netmask ] pub <-''-
-a display (all) hosts in alternative (BSD) style
-e display (all) hosts in default (Linux) style
-s, --set set a new ARP entry
-d, --delete delete a specified entry
-v, --verbose be verbose
-n, --numeric don't resolve names
-i, --device specify network interface (e.g. eth0)
-D, --use-device read from given device
-A, -p, --protocol specify protocol family
-f, --file read new entries from file or from /et
10. Flush Linux DNS Cache
sudo systemd-resolve --flush-caches
11. Remove a Folder and all Contents in Linux
rm -rf /path/to/directory
-f, --force ignore nonexistent files and arguments, never prompt
-i prompt before every removal
-I prompt once before removing more than three files, or
when removing recursively; less intrusive than -i,
while still giving protection against most mistakes
--interactive[=WHEN] prompt according to WHEN: never, once (-I), or
always (-i); without WHEN, prompt always
--one-file-system when removing a hierarchy recursively, skip any
directory that is on a file system different from
that of the corresponding command line argument
--no-preserve-root do not treat '/' specially
--preserve-root do not remove '/' (default)
-r, -R, --recursive remove directories and their contents recursively
-d, --dir remove empty directories
-v, --verbose explain what is being done
--help display this help and exit
--version output version information and exit
By default, rm does not remove directories. Use the --recursive (-r or -R)
option to remove each listed directory, too, along with all of its contents.
To remove a file whose name starts with a '-', for example '-foo',
use one of these commands:
rm -- -foo
rm ./-foo
Note that if you use rm to remove a file, it might be possible to recover
some of its contents, given sufficient expertise and/or time. For greater
assurance that the contents are truly unrecoverable, consider using shred.
12. Create a Folder in Linux
mkdir [folder_name]
Mandatory arguments to long options are mandatory for short options too.
-m, --mode=MODE set file mode (as in chmod), not a=rwx - umask
-p, --parents no error if existing, make parent directories as needed
-v, --verbose print a message for each created directory
-Z set SELinux security context of each created directory
to the default type
--context[=CTX] like -Z, or if CTX is specified then set the SELinux
or SMACK security context to CTX
--help display this help and exit
--version output version information and exit
13. View Contents of a Folder in Linux
cd to (foldername)
ls
or
dir
14. Create a File in Linux
touch newfilename.txt -- extensions eg .js .html .txt
Update the access and modification times of each FILE to the current time.
A FILE argument that does not exist is created empty, unless -c or -h
is supplied.
A FILE argument string of - is handled specially and causes touch to
change the times of the file associated with standard output.
Mandatory arguments to long options are mandatory for short options too.
-a change only the access time
-c, --no-create do not create any files
-d, --date=STRING parse STRING and use it instead of current time
-f (ignored)
-h, --no-dereference affect each symbolic link instead of any referenced
file (useful only on systems that can change the
timestamps of a symlink)
-m change only the modification time
-r, --reference=FILE use this file's times instead of current time
-t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time
--time=WORD change the specified time:
WORD is access, atime, or use: equivalent to -a
WORD is modify or mtime: equivalent to -m
--help display this help and exit
--version output version information and exit
Note that the -d and -t options accept different time-date formats.
15. ZIP and Compress Files in Linux
zip filename.zip etcfile1 etcfile2 etcfile3 [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
The default action is to add or replace zipfile entries from list, which
can include the special name - to compress standard input.
If zipfile and list are omitted, zip compresses stdin to stdout.
-f freshen: only changed files -u update: only changed or new files
-d delete entries in zipfile -m move into zipfile (delete OS files)
-r recurse into directories -j junk (don't record) directory names
-0 store only -l convert LF to CR LF (-ll CR LF to LF)
-1 compress faster -9 compress better
-q quiet operation -v verbose operation/print version info
-c add one-line comments -z add zipfile comment
-@ read names from stdin -o make zipfile as old as latest entry
-x exclude the following names -i include only the following names
-F fix zipfile (-FF try harder) -D do not add directory entries
-A adjust self-extracting exe -J junk zipfile prefix (unzipsfx)
-T test zipfile integrity -X eXclude eXtra file attributes
-y store symbolic links as the link instead of the referenced file
-e encrypt -n don't compress these suffixes
-h2 show more help
16. Display Core System Information in Linux
uname -?
-a, --all print all information, in the following order,
except omit -p and -i if unknown:
-s, --kernel-name print the kernel name
-n, --nodename print the network node hostname
-r, --kernel-release print the kernel release
-v, --kernel-version print the kernel version
-m, --machine print the machine hardware name
-p, --processor print the processor type or "unknown"
-i, --hardware-platform print the hardware platform or "unknown"
-o, --operating-system print the operating system
--help display this help and exit
--version output version information and exit
17. Display a list of running processes in Linux
top -hv | -bcHiOSs -d secs -n max -u|U user -p pid(s) -o field -w [cols]
18. Look up a Word in the Dictionary using Linux
look YOURWORD [options] string [file]
Options:
-a, --alternative use alternative dictionary
-d, --alphanum compare only alphanumeric characters
-f, --ignore-case ignore case differences when comparing
-t, --terminate define string termination character
-V, --version output version information and exit
-h, --help display this help and exit
19. Display how much RAM is used and free in Linux
free [options]
Options:
-b, --bytes show output in bytes
-k, --kilo show output in kilobytes
-m, --mega show output in megabytes
-g, --giga show output in gigabytes
--tera show output in terabytes
--peta show output in petabytes
-h, --human show human-readable output
--si use powers of 1000 not 1024
-l, --lohi show detailed low and high memory statistics
-t, --total show total for RAM + swap
-s N, --seconds N repeat printing every N seconds
-c N, --count N repeat printing N times, then exit
-w, --wide wide output
--help display this help and exit
-V, --version output version information and exit
20. Displays how much Disk Space is Used and Free in Linux
df [options]
Options:
Show information about the file system on which each FILE resides,
or all file systems by default.
Mandatory arguments to long options are mandatory for short options too.
-a, --all include pseudo, duplicate, inaccessible file systems
-B, --block-size=SIZE scale sizes by SIZE before printing them; e.g.,
'-BM' prints sizes in units of 1,048,576 bytes;
see SIZE format below
--direct show statistics for a file instead of mount point
--total produce a grand total
-h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)
-H, --si likewise, but use powers of 1000 not 1024
-i, --inodes list inode information instead of block usage
-k like --block-size=1K
-l, --local limit listing to local file systems
--no-sync do not invoke sync before getting usage info (default)
--output[=FIELD_LIST] use the output format defined by FIELD_LIST,
or print all fields if FIELD_LIST is omitted.
-P, --portability use the POSIX output format
--sync invoke sync before getting usage info
-t, --type=TYPE limit listing to file systems of type TYPE
-T, --print-type print file system type
-x, --exclude-type=TYPE limit listing to file systems not of type TYPE
-v (ignored)
--help display this help and exit
--version output version information and exit
Display values are in units of the first available SIZE from --block-size,
and the DF_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.
Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).
It is my hope that this resource will be a refresher to some of you vets and a learning experience for you beginners in Linux.