CentOS7常用命令详解
查看ip :
ip addr
设置ip信息 :
nmtui
安装ifconfig工具:
yum install net-tools
配置防火墙,阻止单个ip地址访问:
iptables -I INPUT -s 192.168.1.123 -j DROP
配置防火墙,允许单个ip地址访问:
firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -s 192.168.1.123 -j ACCEPT
firewall-cmd --reload
删除命令:
firewall-cmd --permanent --direct --remove-rule ipv4 filter INPUT 0 -s 192.168.1.123 -j ACCEPT
firewall-cmd --reload
或者直接编辑 /etc/firewalld/direct.xml 文件:
[root@local t1]# cat /etc/firewalld/direct.xml
<?xml version="1.0" encoding="utf-8"?>
<direct>
<rule priority="0" table="filter" ipv="ipv4" chain="INPUT">-s 192.168.1.123 -j ACCEPT</rule>
</direct>
[root@local t1]# systemctl restart firewalld.service
关闭防火墙:
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
设置防火墙(开启端口):
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=6000-6100/tcp --permanent
firewall-cmd --reload
查看防火墙端口:
firewall-cmd --list-ports
删除防火墙端口:
firewall-cmd --zone=public --remove-port=80/tcp --permanent
firewall-cmd --zone=public --remove-port=6000-6100/tcp --permanent
firewall-cmd --reload
安装pgsql(9.6) :
yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-centos96-9.6-3.noarch.rpm
yum install postgresql96
yum install postgresql96-server
/usr/pgsql-9.6/bin/postgresql96-setup initdb
systemctl enable postgresql-9.6
systemctl start postgresql-9.6
关闭selinux :
vi /etc/sysconfig/selinux
SELINUX=disabled
接着再执行如下命令,注意 setenforce 后面有空格:
setenforce 0
#设置 SELinux 状态
getenforce
#获取 SELinux 状态
--------------------
[root@localhost etc]# setenforce 0
[root@localhost etc]# getenforce
Permissive
-------------------
设置运行级别:
# multi-user.target类似于runlevel 3;
# graphical.target类似于runlevel5
#查看默认运行级别的方式为
systemctl get-default
#设置默认运行级别的方式
systemctl set-default multi-user.target # 多用户级别
systemctl set-default graphical.target # 图形用户级别
安装tmux :
yum install tmux -y
tmux 防止session名字过长 :
vi ~/.tmux.conf
set-option -g allow-rename off
重启tmux
安装epel :
rpm -ivh https://mirrors.ustc.edu.cn/epel/7/x86_64/Packages/e/epel-release-7-14.noarch.rpm
通过光盘安装软件
1、挂载iso文件
mount CentOS-7-x86_64-Everything-1611.iso /mnt -o loop
如果是使用光驱则执行如下命令: mount /dev/cdrom /mnt -o loop
2、修改yum配置
vi /etc/yum.repos.d/CentOS-Media.repo
[c7-media]
name=CentOS-$releasever - Media
baseurl=file:///media/CentOS/
file:///media/cdrom/
file:///media/cdrecorder/
file:///mnt/
3、搜索并安装
yum --disablerepo=* --enablerepo=c7-media search cmake
yum --disablerepo=* --enablerepo=c7-media install cmake
设置时区 :
timedatectl set-timezone Asia/Shanghai
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
安装gcc等开发工具 :
yum groupinstall "Development Tools"
putty终端文件夹蓝色显示问题:
/etc/DIR_COLORS
DIR 01;34 修改为 : DIR 01;94
dircolors /etc/DIR_COLORS
需要putty重新连接
tmux文件夹蓝色显示问题需要在上面命令的基础上如下操作(.bashrc中添加):
alias tmux="tmux -2"
if [ "$TERM" != "xterm-256color" ]; then
export TERM=xterm-256color
fi
下载软件离线安装包:
yum install --downloadonly --downloaddir=/tmp/ libuuid-devel
(前提是该软件之前未安装过,否则上述命令不成功)
ramdisk 相关
1、临时创建
mkdir /tmp/ramdisk
sudo mount -t tmpfs -o size=1024m tmpfs /tmp/ramdisk
2、开机启动创建
vi /etc/fstab
tmpfs /tmp/ramdisk tmpfs nodev,nosuid,noexec,nodiratime,size=1024M 0 0
可以通过 df -h 看下映射结果
也可以直接使用 /dev/shm ,通过软连接指向该目录即可使用,比如:
ln /dev/shm/ -s log
nc 相关
安装: yum install nc
1、udp测试
服务端:
nc -ulvp 9093
客户端连接服务器:
nc -u <serverIP> 9093
2、tcp测试
服务端:
nc -lv 9093
客户端连接服务器:
nc <serverIP> 9093