SysAdmin - Essential commands
Linux Foundation Certified SysAdmin (LFCS): Essential commands
Log into local and remote consoles
Connect via SSH (text mode)
1
2
3
4
5# no userName takes the one you are logged with on your machine
# machine can be an IP address or a domain
ssh username@machine
# ECDSA key is added the first time
# the insert passwordConnect via VNC (grahical mode - VNC viewer)
- Give the network address
- Check ECDSA key (fingerprint) to connect
- Add username and password
Warning!
The server we want to connect to must have xwindows installed, plus VNC server
Search for files
Linux is case sensitive
Find
- Basic
1
2
3
4
5
6
7touch etc/test.txt
# find on current directory
find -name "test.txt"
# check what is on our current directory
ls -la
# find starting from root
find / -name "test.txt" - Advanced
- by name
1
2
3
4
5
6# find starting from etc
find /etc -name "test.txt"
# -i to ignore case sensitive
find /etc -iname "test.txt"
# reverse search
find /etc -not -name "test.txt" - by type
1
2
3
4
5
6
7
8# find for type (e.g. character devices)
find /etc -type c
# find symbolic links
find /etc -type l
# find symbolic directories
find /etc -type d
# find files whose name ends in ''.log'
find /etc -type -f "*.log" - by size
1
2# files whose size is greater than 27K
find /usr/bin -size +27000 - by creation date
1
2
3
4# created more than 1 day ago
find /usr/bin -type f -mtime 1
# created less than 1 day ago
find /usr/bin -type f -mtime -1 - by user creation date
1
2# owned by myUser, pipe it through more to pause on each screen
find /usr/bin -user myUser | more - by permision
1
2
3
4
5# with permissions 755
find /usr/bin -perm 755
# you may do things which what you find on the same line
# '{} \;' are placeholders
find /usr/bin -name "test.txt" -exec chmod 700 {} \;
- by name
- Basic
Check petitions
1
2
3
4# df stands for disk-free
# - h stands for human readable
df -h
# root, mounted devices, temporary file systems...Find executable in our path
1
2which python
which ifconfigManual
1
man find
Locate
1
2
3# Less powerful, depends on database which should be updated
updatedb
locate test.txt
Basic file system features and options
- Definitions
- block device: set of addressable blocks used to store and retrieve data
- filesystem: where a computer system persists general data for users and/or applications
Filesystems can affect
- Performance pf the system
- Efficiency of the media
- Compatibility with other systems
- Jounaling
- journal prevent data corruption, records metadata
- add a bit of overhead to write files
- some high performance servers might not need it
- often not used on removable media
- Current standard
- EXT
- EXT (EXTended File System)
- EXT2 (extended file attributes)
- EXT3 (journaling)
- EXT4(add backward compatibility)
- EXT
- Coming standard
- BtrFS (substitute to EXT by Oracle)
- B-tree File system
- drive pooling, snapshots, compression, online defragmentation
- BtrFS (substitute to EXT by Oracle)
- Common alternatives
- ReiserFS
- New features that wouldn’t be implemented by EXT (efficiencies for small text files)
- Unlikely to continue development
- ZFS (Sun, for Solaris, acquired by Oracle - opensourced not compatible with Linux kernel license)
- drive pooling, snapshits, dynamic disk striping
- each file has a checksum
- (Ubuntu 16.04 supports it) uses containers by default
- XFS
- similar to Ext4
- can be enlarged (but not shrunk) on the fly (good for backup servers)
- bad for many small files (like web servers)
- JFS (Journaled, by IBM)
- low CPU usage
- partitions can be enlarged (but not shrunk)
- support in most every major distribution
- not widely tested
- ReiserFS
- Crossplatform
- FAT (FAT16, FAT32, exFAT)
- Microsoft File Allocation Table, not journaled
- USB drives on Windows and Apple hardware
- FAT (FAT16, FAT32, exFAT)
- Swap space
- virtual memory ()scratch space)
- hibernating (analogous to Windows Paging File)
Compare and manipulate file content and I/O redirection
- List all files
1
2# list files
ls -la - Read (send to default output) and concatenate
1
2
3
4
5
6
7
8# catenate
cat shopingListfile.txt
# catenate various files as a single file
cat shopingListfile.txt anotherList.txt
# catenate, pipe it to paginate it with 'more'
cat shopingListfile.txt | more
# pip it to navegate using the up and down arrow keys
cat shopingListfile.txt | less - Sort
1
2
3
4# sort the text content in alphabetical order
sort shopingListfile.txt | more
# sort the text content in reverse alphabetical order
sort -r shopingListfile.txt | more - Create a new output file with
>
1
cat shopingListfile.txt anotherList.txt | sort > combined.txt
- Format (check man)
1
2# fix spacing issues on a file
fmt -u format.txt - Numbered lines
1
2# If it si not redirected to other file, the numbers won't be stored
nl format.txt > numberedFormat.txt - Remove delimiters
1
2
3# -d means delimited
# -f1 means first part
cut -d ";" -f1 delimited.txt - Redirection
1
2
3
4# store on a new file, or overwrite if it already exists
ls -la > directory.txt
# append to that file
ls -ls >> directory.txt
Basic Regular Expressions
Anchor characters (
^
,$
)1
2
3
4# starts the line with A
^A
# ends the line with Z
Z$Selectors
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17# Find all the lines that start with 'The'
grep '^The' alice.txt
# Find line with a single character - '.' means any character
grep '^.$' alice.txt
# Find line which starts with a character
grep '^[0123456789]' alice.txt
# Find line which starts with a character, short version
grep '^[0-9]' alice.txt
# With '*' A character appear 0 or more times (e.g at least has 2 digits)
grep '^[0-9]*[0-9][0-9]' alice.txt
# Between 4 and 8 lowercase characters
grep '[a-z]\{4,8\}' alice.txt
# Search for a word with either capital or lowercase letter
grep '\<[tT]he>' alice.text
# back-reference: any 2 lowerkey letters adjoined (e.g. aa, baac)
# [a-z][a-z] won't work for that pairs
grep '\([a-z]\)\1' alice.txt
Archive, backup, compress, unpack, and decompress files
- archive and backup (
tar
)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20cd /home/user
#backup on dir
# create dir
mkdir datab_kup
cd data_bakup
# recursive copy of the '/data' folder
cp -r /data .
# undo hat was done previously
rm *
rm -rf *
# backup the 'data' folder with tar (from 'tape archives')
# concatenate, no compression
# -c create new file
# -v for verbose
# -f for file
tar cvf databkup.tar data
# -t lists content of tar file
tar tvf databkup.tar
# search files on tar for a particular the text `wpa_supplicant`
tar tvf databkup.tar | grep wpa_supplicant - compress (
zip
)1
gzip databkup.tar
- archive plus compress
1
2# add the 'z' directive
tar cvfz databkup.tar.gz /data - extract or unzip/decompress
1
2# extract everything on a new directory
tar xvfz databkup.tar.gz
Create, delete, copy, and move files and dirs
- create
1
2
3
4
5
6
7
8
9# file
touch testFile
# edit the file with pico or nano
nano testFile
# directory
mkdir secondFiles
touch testFileDir
# create several directories (one inside the other)
mk -p secondFiles/textFiles/otherFiles - remove
1
2
3
4
5
6# remove file
rm testFile
# remove directory
rmdir secondFiles
#remove directory tree, recursive, force
rm -rf textFiles - copy
1
2# copy to testfile2
cp testFile testFile2 - move and rename
1
2
3
4# move file
mv testfile2 secondFiles/
# rename file via mv
mv testFile2 testFile
Create and manage hard and soft links
- Hard link: links the original data chunk on disk.Hard links cannot traverse filesystems (physical or shared)
1
2
3# create a hard link to a file
ln ../dir2/file2 hardLinkToFile2
# deleting the original link doesn't lose the data - Soft (symbolic) link: links the original link
1
2
3
4# create a soft link to a file
ln -s ../dir2/file2 softLinkToFile2
# on 'ls' it is represented with an arrow ->
# deleting the original link loses the data, as a direct access
List, Set, and change standard file permissions
Get the list
1
2# list files
ls -laOutput
1
2
3
4
5total 8
drwxr-xr-x 2 user1 user 4096 Feb 28 14:26 .
-rw-r--r-- 1 user1 dbadmin 0 Feb 28 14:26 user1andgroup
-rw-r--r-- 1 user1 user1 0 Feb 28 14:26 user1only
-rw-r--r-- 1 user1 user1 0 Feb 28 14:26 user1yoneSets meaning:
- directory (‘d’ or ‘-‘’)
- ownerPermission-groupPermission-otherPeoplePermission (‘r’, ‘w’, ‘x’ or ‘-‘)
- Owner
Change permissions
1
2
3
4
5
6
7
8# grant permissions for user chad, to grant
chmod u+rwx user1only
# take away group permissions
chmod g-rwx user1only
# take away other users permissions
chmod o-rwx user1only
# grant permissions to everyone else
chmod a+rwx user1only
Permissions via octal numbers
- 4: read
- 2: write
- 1: exec
You may do all 3 commands in a single one with the octal numbers,
so to grant all permissions for user, group and other would be 4+2+1=7 ->chmod 777 user1Only
- Change ownership of files
1
2
3
4
5# find files from lostUser
sudo find / -user lostUser
# supose the results says they are on '/opt/myapp'
# change ownership to cloud_user from files on a certain folder
sudo chown -R cloud_user:cloud_user /opt/myapp
Read and use system documentation
- Manual
1
2
3
4# manual + command we want to know about, q to exit
man ls
# update the man db
mandb - apropos
1
2# commands related to what you asked about
apropos ps - info
1
2# similar to man
info ipc - locate
1
2# its db is updated by a cronjob once a day
info systemctl.conf - the documentation folder:
user/share/doc
Manage access to the Root account
- Get superuser permission
1
sudo ls
- Grant sudo permission
1
2
3
4sudo visudo
# use either vim or nano
# find part to grant and add
# chad ALL=(ALL) ALL - Be added to the WHEEL (CentOS) or SUDO (Ubuntu) group
1
2
3
4
5
6
7
8# show groups for user1
groups user1
# add user1 to group WHEEL
# -a is important: it adds, if not, it will switch group
usermod -a -G wheel user1
# if it fails, do "sudo bang-bang"
sudo!!
sudo usermod -a -G wheel user1
Securely copy a file to another server
1 | # scp file_name userId@destinyServer:destinyFolder |