Các câu lệnh Linux phải biết để làm việc với server

  Feb 4, 2018      2m      0   
 

Tổng hợp các câu lệnh phổ biến nhất trên Linux

Các câu lệnh Linux phải biết để làm việc với server

Kết nối đến server bằng SSH

$ ssh username@server.ip.address
ví dụ:
$ ssh minhng@123.45.678.901

Chuyển nhận file với server Copy thư mục từ PC lên Server (Termnial đang ở PC)

$ rsync -v --stats --progress -a /path/to/folder username@server.address:/path/to/folder/on/server/
hoặc 
$ scp -r /path/to/folder username@server.address:/path/to/folder/on/server/

Copy thư mục từ Server xuống PC (Termnial đang ở PC)

$ rsync -v --stats --progress -a username@server.address:/path/to/folder/on/server /path/to/folder/
hoặc
$ scp -r username@server.address:/path/to/folder/on/server /path/to/folder/

Chương trình vẫn tiếp tục chạy sau khi ngắt kết nối / logoutCách 1

  1. Chạy chương trình như bình thường. Ví dụ: $ python myprogram.py » program.log
  2. Nhấn Ctrl + Z để tạm ngưng chương trình vừa chạy.
     [1]+  Stopped                 myprogram
     $ disown -h %1
     $ bg 1
     [1]+ myprogram &
     $ logout
    

Lưu ý: số 1 ở lệnh disownbg là job number, hiển thị khi ta tạm ngưng chương trình bằng Ctrl + Z
[1]+ Stopped myprogram

Cách 2: đôi khi cách 1 không làm việc được thì ta phải dùng chương trình screen.

$ sudo apt install screen
  1. Tạo screen mới. Ví dụ: ta tạo screen có tên là minhng
    $ screen -S minhng
    
  2. Chạy chương trình như bình thường
  3. Detach khỏi screen đang chạy. Nhấn tổ hợp phím Ctrl + A + D
  4. Đến lúc này cho dù bạn logout server thì screen vẫn được chạy bình thường
  5. Attach lại screen đang chạy
    $ screen -r minhng
    

    Nếu chỉ có 1 screen duy nhất thì ta chỉ cần lệnh

    $ screen -r
    

    Liệt kê các screen đang chạy

    $ screen -list
    

Xóa screen:

$ screen -X -S [session # you want to kill] quit

Ví dụ:

$ screen -X -S minhng quit

1 số lệnh hay dùng trên server (đang remote ssh vào server)

Kiểm tra dung lượng trống của ổ đĩa

$ df

Kiểm tra CPU và RAM

$ htop
# Nếu chưa cài đặt có thể cài htop bằng lệnh:
$ apt-get install htop

Kiểm tra GPU

$ nvidia-smi

Kiểm tra kích thước file và thư mục

$ du -sh /path/to/directory
ví dụ:
$ du -sh /tmp
$ du -sh *

Liệt kê file với kích thước

$ ls -sh

Đếm số lượng file trong thư mục

$ find /path/to/directory -type f | wc -l

Tải file với direct link

$ wget -O file.extension http://direct/url/file.extension
ví dụ:
$ wget -O minhnginfo.jpg https://minhng.info/assets/img/me.jpg

Tải file trên Google Drive (Gdrive)dl_gdrive.py

import requests

def download_file_from_google_drive(id, destination):
    URL = "https://docs.google.com/uc?export=download"

    session = requests.Session()

    response = session.get(URL, params = { 'id' : id }, stream = True)
    token = get_confirm_token(response)

    if token:
        params = { 'id' : id, 'confirm' : token }
        response = session.get(URL, params = params, stream = True)

    save_response_content(response, destination)    

def get_confirm_token(response):
    for key, value in response.cookies.items():
        if key.startswith('download_warning'):
            return value

    return None

def save_response_content(response, destination):
    CHUNK_SIZE = 32768

    with open(destination, "wb") as f:
        for chunk in response.iter_content(CHUNK_SIZE):
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)

if __name__ == "__main__":
    file_id = '1z8x1MMTuTnwsdmMvEmAcej28mPI0zWzh'
    destination = 'dl_gdrive.py'
    download_file_from_google_drive(file_id, destination)

Lưu ý:

  • file_id: id của file cần download, bạn có thể lấy id này trên đường link download. Ví dụ: https://drive.google.com/open?id=1z8x1MMTuTnwsdmMvEmAcej28mPI0zWzh
  • destination: tên file download về.

Chạy download file bằng lệnh:

$ python dl_drive.py

Scan các địa chỉ IP trong mạng LAN

$ ifconfig | grep inet
...
inet addr:192.168.100.4  Bcast:192.168.100.255  Mask:255.255.255.0\
...
$ nmap -sP 192.168.100.*


Khám phá xử lý ảnh - GVGroup




-->