01 - Linux Basic

Basic commands

cat
wc -l file
date
date --date "40 days"
date --date "40 days ago"
cal
cal -3 
cal 7 1996 
 
### Nhan tin nhan
mesg y
mesg n
 
 
type _command_
type ls
 

TTY

  • physical TTY
  • local pseudo tty
  • remote pseudo tty

ls

ls -lrth /etc
ls -ld /etc

# lsblk - ls list block devices
ls -l /dev/sda*

ls -l /etc/*-release
lsb_release -d

Working with files/ folders

cat file
cp 
cp -i xxxx (interactive mode to confirm if file exists)
mv 
rm

---


mkdir xx
mkdir -p sales/test (-p is parent folder)
rmdir 
rm -rf
cp -R folder1 folder2

mkdir -m 777 test


---
touch one/file{1..5}
  • Hard link: tồn tại song song với file gốc
  • Soft link: ln -s link tới địa chỉ file gốc. File gốc mà xóa thì softlink cũng ngỏm.

Permission

QuyềnNhị phânThập phân
r (read)1004
w (write)0102
x (execute)0011

3 loại: owner, group, others

drwxr-x---  2 alice staff 4096 May 31  /home/alice/docs

drwx - d là directory, read, write, execute.

5. Reading files

cat
less file_name
less !$ -> Mở file đã được show bằng lệnh less trước đó
 
head -n 3 /etc/services
tail -n 3 /etc/services

Regular expression and grep

grep '\bserver\b' ntp.conf
 
# Grep with expression
grep -E '[aeiou]{5}' /usr/share/dict/words 

Sed to edit file

sed '/^#/d ; /^$/d' ntp.conf

Compare file

diff file1 file2
 
md5sum file_name

Find file

find /usr/share/doc -name '*.pdf'
 
# Sử dụng -exec command {} để thực hiện lệnh với output của find
find /usr/share -size +10000k -type f -exec du -h {} \;

6. Best Editor in the World

stat file_name
 

7. Piping and Redirection

  • Target: Use input-output redirection

Trong Linux, mỗi chương trình có 3 luồng dữ liệu chính:

LuồngTênMô tảSố hiệu
STDINStandard InputDữ liệu đầu vào (thường từ bàn phím)0
STDOUTStandard OutputDữ liệu đầu ra (kết quả bình thường)1
STDERRStandard ErrorDữ liệu lỗi (thông báo lỗi)2
Nếu muốn ghi output của các lệnh ra file, ta có thể sử dụng số hiệu trước command > để redirect

Ví dụ:

ls existed_file missing_file > out.txt 2> error.txt

hoặc 1 câu lệnh khá phổ biến, ta sẽ thấy đoạn find /etc -type l 2> /dev/null, tức là nếu error thì ghi vào null pointer thôi không cần hiển thị ra/ làm gì.

&> thì sẽ là gộp của các lệnh trên.

  • Redirection of STDOUT
    • ls > newfile
    • Use > - Write to file
    • >> - append to file
  • Using noclobber
    • Khi sử dụng > thì nội dung của các file thường bị ghi đè, nên nếu bật set -o noclobber thì lúc gọi echo "hello" > file.txt, nếu file.txt đã tồn tai, lệnh sẽ raise lỗi, không thể ghi đè.
    • Nếu đang bật noclobber mà vẫn muốn ghi đè, ta có thể sử dụng >| thay vì >
    • Tắt noclobber: set +o noclobber
  • Redirection of STDERR
    • 2> err.txt
  • Reading into STDIN
    • mail
  • Here documents
    • HereDoc giống trong code
  • Using command pipelines
    • cut -f7 -d: /etc/passwd | sort | uniq
  • Using named pipes
    • mkfifo mypipes
  • Using tee
    • command | tee file.txt
    • tee sẽ ghi dữ liệu đầu ra của command vào file.txt, đồng thời hiển thị lên terminal thay vì chỉ redirect vào file như >
    • echo '127.0.0.1 bob' | sudo tee /etc/hosts - Nếu chỉ dùng > thì chỗ này sẽ raise lỗi permission do không có sudo

Archiving Files

  • Using tar to create and expand archives
  • Using gzip, bzip2, gunzip and bunzip2
gzip file
 
  • Using cpio
  • Using dd - Convert and copy a file

File permissions

stat file_name

permision: read: 1, write 2, execute: 4

for: user/ group/ others (everyone)

chmod u=r,g=rw,o=rwx file

Access root acc

id

su 

sudo cat /etc/sudoers

su - đăng nhập vào root terminal, cần mật khẩu root, không mặc định ghi log sudo - cần nhập mật khẩu của chính mình, có lưu log vào /var/log/auth.log

có thể sửa file sudoers để cấp quyền cho từng group.

  • Để edit quyền ssh thì vào vi /etc/ssh/sshd_config Không thể login via ssh vào root account nữa.
    • SSH PermitRootLogin