Linux 常用命令整理

Linux 常用命令整理

这篇是随手记的,仅供自己参考 :-)

 

显示当前版本信息:
lsb_release -da
hostnamectl

 

Fail2Ban 防止暴力攻击的基本配置

#安装fail2ban
apt-get install fail2ban
#配置基本的拦截(监狱)参数
vim /etc/fail2ban/jail.conf
#重启服务器,让配置生效
systemctrl resatrt fail2ban
#查看拦截信息
fail2ban-client status

时间配置和管理

#显示
timedatectrl 

timedatectl set-ntp true

timedatectl set-timezone Asia/Hong_Kong

timedatectl list-timezones

date

hwclock

time

网络端口和进程等基本信息

#网络连接信息
netstat -npta | grep 22 //显示网络端口占用情况 

#-----进程信息查询查看------
#以前常用的 ps -aux  pa -axu 好像被废弃了
#查看当前当前用户相关的进程,-H 树状显示, | grep 过滤信息
ps u -H | grep name
#查看全部进程, -f 详细列表, -e 全部进程
ps -efH

top(htop) //当前进程,任务管理器 

stat              显示指定文件的详细信息,比ls更详细
who               显示在线登陆用户
whoami            显示当前操作用户
hostname          显示主机名
uname             显示系统信息
top               动态显示当前耗费资源最多进程信息
du                查看目录大小 du -h /home带有单位显示目录信息
df                查看磁盘大小 df -h 带有单位显示磁盘信息
ifconfig          查看网络情况
ping              测试网络连通
netstat           显示网络状态信息
man               命令不会用了,找男人  如:man ls
clear             清屏
alias             对命令重命名 如:alias showmeit="ps -aux" ,另外解除使用unaliax showmeit
kill              杀死进程,可以先用ps 或 top命令查看进程的id,然后再用kill命令杀死进程。
source            运行脚本: 也可以加个 './' 就可以了
<strong>journalctl        Query the systemd journal</strong>

lsb_release -a           //系统版本号
uname -a                //系统版本,名称
hostname -f              //显示完整的主机名称
service apache2 restart 
CTRL+D                   //行编辑状态下结束编辑,发送内容 
man section page

<strong># 修改主机名</strong>
# vim /etc/hostname
hname
# vim /etc/hosts
127.0.0.1 hname.full.name hname

日志管理

#journalctl - Query the systemd journal
journalctl -xn
journalctl -f
tail -f app.log   #查看日志最后几行,并跟踪显示变化 
less              #查看日文件,翻页
dmesg | more      #查看缓冲区日志

常见的几个登录文件有:
/var/log/secure:记录登入系统存取数据的文件,例如 pop3, ssh, telnet, ftp 等都会被记录;
/var/log/wtmp:记录登入者的讯息数据,由于本文件已经被编码过,所以必须使用 last指令来取出文件的内容;
/var/log/messages:尤为重要,几乎发生的错误讯息(或是重要信息)都会被记录在此;
/var/log/boot.log:记录开机或者是一些服务启动的时候,所显示的启动或关闭讯息;
/var/log/maillog 或 /var/log/mail/*:纪录邮件存取或往来( sendmail 与 pop3 )的使用者记录;
/var/log/cron:记录 crontab 这个例行性服务的内容的。
/var/log/httpd, /var/log/news, /var/log/mysqld.log, /var/log/samba, /var/log/procmail.log:分别是几个不同的网络服务的记录文件!

登录文件的纪录程序之一: syslogd

用户管理

添加用户 
adduser phpq 
重设密码
passwd <username>来重设<username>的密码
查看所有组 
cat /etc/group
查看当前登录用户
[root@krlcgcms01 ~]# w
[root@krlcgcms01 ~]# who
b),查看自己的用户名
[root@krlcgcms01 ~]# whoami
c),查看单个用户信息
[root@krlcgcms01 ~]# finger apacheuser
[root@krlcgcms01 ~]# id apacheuser
d),查看用户登录记录
[root@krlcgcms01 ~]# last 查看登录成功的用户记录
[root@krlcgcms01 ~]# lastb 查看登录不成功的用户记录
e),查看所有用户
[root@krlcgcms01 ~]# cut -d : -f 1 /etc/passwd
[root@krlcgcms01 ~]# cat /etc/passwd |awk -F \: '{print $1}'
f) 用户修改
$ usermod
$ useradd
$ passwd

Man命令

man section page  // etc: man 5 crontab
section list:
1 - user commands
2 - system calls
3 - library functions
4 - special files
5 - file formats
6 - games
7 - conventions and miscellany
8 - administration and privileged commands 
L - math library functions
N - tcl functions
1----用户在shell环境可以使用的指令或可执行文件 
2----系统核心提供的可调用的函数与工具 
3----常用函数、函数库,大部分是C的函数库 
4----设备驱动程序,通常在/dev下 
5----配置文件或某些档案的格式 
6----游戏(Games) 
7----惯例与协议等,如Linux的文件系统、网络协议、ASCII Code 
8----系统管理员可用的管理指令 
9----与Kernel有关的文件

环境配置

# 配置命令提示符,使用色彩
# vim /etc/bash.bashrc
# Custom lines
export PS1='\[\e[1;35m\][\[\e[1;33m\]\u@\h \[\e[1;31m\]\w\[\e[1;35m\]]\[\e[1;36m\]\$ \[\e[0m\]'
alias ls='ls --color=auto'
alias grep='grep --colour=auto'
alias egrep='egrep --colour=auto'
alias fgrep='fgrep --colour=auto'

# 使其立即生效 >> 
  $ . /etc/bash.bashrc | source /etc/bash.bashrc

GRUB

#修复(重装)GRUB
grub-install /dev/sda      #etc sda not sda1  

grub-mkdevicemap

update-grub

证书管理

letsencrypt - 证书服务商
certbot 证书更新器
/etc/cron.d/certbot 证书自动更新脚本

Apache Conf:
SSLCertificateFile /etc/letsencrypt/live/www.com/fullchain.pem 
SSLCertificateKeyFile /etc/letsencrypt/live/www.com/privkey.pem

定时任务管理

cron  //定时守护进程,由init.d加载
crontab //定时任务管理工具(用户级)
/etc/crontab (cron.dayly ...) //系统级定时任务
/etc/cron.d   //系统级定时任务

终端和屏幕管理(Screen)

#列出当前所有在在线的终端
w
#跟上面一样,好像简单一些
who
#当前使用的终端
who am in
#查看更多的进程信息可以时 PS Top

#------------- Screen --------------------
#恢复最近一个Session,如果没有就创建一个,并且Detach现有的实例
screen -D -R

#screen 显示Screen实例列表
screen -ls

#Screen 常用快捷键
C-a ?    #显示所有快捷键    
C-a a    #创建一个新的运行shell的窗口并切换到该窗口
C-a "    #显示当前窗口列表
C-a Esc  #进入复制模式,可以翻页
C-a n -> Next,切换到下一个 window
C-a p -> Previous,切换到前一个 window
C-a 0..9 -> 切换到第 0..9 个 window
Ctrl+a [Space] -> 由视窗0循序切换到视窗9
C-a C-a -> 在两个最近使用的 window 间切换 
C-a x -> 锁住当前的 window,需用用户密码解锁
C-a d -> detach,暂时离开当前session,将目前的 screen session (可能含有多个 windows) 丢到后台执行,并会回到还没进 screen 时的状态,此时在 screen session 里,每个 window 内运行的 process (无论是前台/后台)都在继续执行,即使 logout 也不影响。 
C-a z -> 把当前session放到后台执行,用 shell 的 fg 命令则可回去。
C-a w -> 显示所有窗口列表

邮件管理

debian下已经使用exim4替代了sendmail的功能。exim4 组合了 MTA/MDA 功能并相对小巧和灵活。 它默认配置为只处理系统本地的 e-mail,系统管理员(root 帐号)的 e-mail 地址会发送到安装时创建的普通用户帐号[16]

# 安装
apt-get install exim4 
# 配置
dpkg-reconfigure exim4-config 
# 一般选择服务器是网络中, 然后ip使用127.0.0.1就可以了 
dc_eximconfig_configtype='internet'
dc_other_hostnames='cloud.kometo.com'
dc_local_interfaces='127.0.0.1 ; ::1'
dc_relay_domains='*'
dc_use_split_config='false'

#测试
mail jiaqiubo@msn.com 
Subject: hello 
wo hen hao. 
. 
#这里的 (回车 + . + 回车) 是结束的意思。 

#测试(调试)邮件地址路由情况
exim4 -bt -d user@some.domain.com

注意的问题 

  • Exim使用自己的log记录,/var/log/exim4/mainlog ,本地邮件分发通过 Procmail, log文件在 /var/log/procmail.log
  • Exim log不出现在journalry中
  • Exim 默认使用 procmail 进行本地分发,如果Exim4看不到问题,可以看看procmail的日志,问题也许出在本地分发上了
  • 邮箱文件位置有两种情况 mailbox  ( /var/spool/mail/username) 和  Maildir (~/Maildir),Maildir是比较新的方式,如果使用它必须确保配置了  export MAIL=~/Maildir,否则mail 和 mutt 无法正确的找到邮件信息。

No deliveries to root!
Debian 默认的是不予许向root帐号发送邮件的,所以最佳实践是保持把root的邮件转发(aliases)到一个普通账户上。

No Exim 4 version released with any Debian OS can run deliveries as root.
If you don’t redirect mail for root via  /etc/aliases to a nonprivileged account, the mail will be delivered to /var/mail/mail with permissions 0600 and owner mail:mail.
This redirection is done by the mail4root router which is last in the list and will thus catch mail for root that has not been taken care of earlier.

 

# 配置转发root的邮件
vim aliases   ## root: someone, someone@mail.com
newaliases
service exim4 restart

SMTP的网络连通

一般情况下,推荐使用连接外部SMTP发送邮件的方式,但是连接外部SMTP服务器经常遇到连通性问题,有时很难排查,可以使用 telnet gmail-smtp-in.l.google.com 25  类似的命令进行连通性排查,通常情况下,Gmail不会直接屏蔽访问,对于拒绝的连接反馈拒绝原因;如果直接出现 “time out” 之类的情况,一般要从ISP、VPS主机商等方面找原因。比如Vultr默认的是屏蔽所有对外的SMTP访问,必须提交服务请求单,才会手动给予开通。

软件包管理

apt-get update   //更新软件库信息
apt-get upgrade  //升级,保守的
apt-get dist-upgrade  //升级,自动处理依赖关系

Removing mpt-statusd

As soon as Debian is installed on a VMware ESXi host it assumes that disks are in RAID and the RAID verification service is being installed.

It is sufficient to stop the mpt-status service and (optional) remove the package:

/etc/init.d/mpt-statusd stop
update-rc.d -f mpt-statusd remove

发表评论