当前位置: 首页 > 工具软件 > manual > 使用案例 >

Linux Command Manual

巫马化
2023-12-01

Introduction

我的个人博客:https://phoenixnest.github.io/

本篇将介绍有关Linux终端使用的一些笔记分享。

必要时请使用sudo权限,但不建议所有命令均使用sudo。


参考资料:


网络

获取所有网卡的IP地址

# get the IP address of all interfaces
networkctl status

显示所有主机的IP地址

# display all IP addresses of the host
hostname -I

开启/关闭某个网络接口

# enable/disable interface
ip link set <interface> up
ip link set <interface> down

防火墙规则

# enable firewall:
sudo ufw enable

# list rules:
sudo ufw status

# allow port:
sudo ufw allow <port>
# sudo ufw allow 22

# deny port:
sudo ufw deny <port>

SSH远程连接

# connect remotely through SSH
ssh <user>@<host IP>

安全性

查看当前登录账户

# show which users are logged in
w

查看用户密码逾期时间

# Get password expiration date for <user>
chage -l <user>

设置用户密码逾期时间

# Set password expiration date for <user>
sudo chage <user>

锁定用户

# lock a user account
sudo passwd -l <user>

解锁用户

# unlock a user account
sudo passwd -u <user>

列出开放的端口与其关联的进程

# List open ports and associated processes
sudo netstat -tulpn

自动检测并禁止危险IP

# automatically detect and ban abusive IP addresses
sudo apt install fail2ban

# show banned IP addresses
sudo fail2ban-client status
sudo fail2ban-client status <jail>

内核热更新

# visit ubuntu.com/livepatch to get a free token for up to 3 machines.
sudo snap install canonical-livepatch
sudo canonical-livepatch enable <token>

文件

文件列表

# List files
ls

# List files with permissions and dates
ls -al

文件间操作

# create empty:
touch <filename>

# create with content:
echo "<content>" > <filename>

# append content:
echo "<content>" >> <filename>

# display a text file:
cat <file>

# copy:
cp <file> <target filename>

# move/rename:
mv <file> <target directory/filename>

# delete:
rm <file>

# find files modified in the last n minutes
find <directory> -mmin -<n> -type f
# eg. find . -mmin -5 -type f

# display file paginated
less <filename>

# display first n lines
head -n <n> <filename>

# display last n line
tail -n <n> <filename>

# follow file content as it inncreases
tail -f <filename>

压缩文件操作

# Pack a directory into an archive
# zip: 
zip -r <target> <source dir>
# tar.gz: 
tar cvzf <target>.tar.gz <source dir>

# Unpack an archive
# zip: 
unzip <zip file>
# tar.gz: 
tar xf <tar.gz file>

远程服务器文件操作

# Copy file to remote server
scp <filename> <user@server>:<destination>
# eg. scp config.yaml admin@192.0.0.0:/config

# Copy directory recursively from remote server
scp -r <user@server>:<source> <destination>
# eg. scp -r admin@192.0.0.0:/config /tmp

文件夹操作

# create a directory
mkdir <directory>

# create directories recursively
mkdir -p <directory1><directory2>...

# delete a directory recursively
rm -r <directory1><directory2>...

搜索

# quick file search
locate <q>

# search string in file
grep <string> <filename>

# search string recursively in directory
grep -Iris <string> <directory>

系统

软/硬件信息

# display kernel version
uname -r

# get disk usage
df -h

# get memory usage
cat /proc/meminfo

时区

# get system time
timedatectl status

# set system timezone
timedatectl list-timezones
sudo timedatectl set-timezone <zone>

日志

# monitor new logs for a service
journalctl -u <service> --since now -f

登录记录

# get the list of recent logins
last

性能监控

# display running processes
sudo apt install htop
htop

服务/进程/任务管理

# get all running services
systemctl --state running

# start or stop a service
service <service> start/stop

# kill process by id
kill <process id>

# kill process by name
pkill <process name>

# run command in the background
<command> &

# display background commands
jobs

# bring command <n> to the foreground
fg <n>
 类似资料:

相关阅读

相关文章

相关问答