引言
本篇将介绍我在学习有关
Linux终端使用
的一些笔记分享。必要时请使用
sudo
权限,但不建议
所有命令均使用sudo。
Linux,全称GNU/Linux,是一种免费使用和自由传播的类UNIX操作系统,其内核由林纳斯·本纳第克特·托瓦兹于1991年10月5日首次发布,它主要受到Minix和Unix思想的启发,是一个基于POSIX的多用户、多任务、支持多线程和多CPU的操作系统。它能运行主要的Unix工具软件、应用程序和网络协议。它支持32位和64位硬件。Linux继承了Unix以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。Linux有上百种不同的发行版,如基于社区开发的debian、archlinux,和基于商业开发的Red Hat Enterprise Linux、SUSE、Oracle Linux等。
网络
获取所有网卡的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>