Linux 系统下操作集锦

前言

本篇文章专门用来记录本人在使用 Linux 系统过程一些操作技巧和遇到的一些问题及解决方案,长期更新。


SOLUTION

Linux 系统下配置网关

详见《Linux 系统下配置网关》。

Linux 系统下关闭 / 开启防火墙

Centos 7 firewall 命令

查看已经开放的端口

1
[root@sunys ~]# firewall-cmd --list-ports

开启端口

1
[root@sunys ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent

删除端口

1
[root@sunys ~]# firewall-cmd --zone=public --remove-port=80/tcp --permanent

– zone #作用域
– add-port=80/tcp #添加端口,格式为:端口 / 通讯协议
– permanent #永久生效,没有此参数重启后失效

重启 / 停止 / 停止 / 查看防火墙

1
2
3
4
[root@sunys ~]# firewall-cmd --reload  #重启firewall
[root@sunys ~]# systemctl stop firewalld.service #停止firewall
[root@sunys ~]# systemctl disable firewalld.service #禁止firewall开机启动
[root@sunys ~]# firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)

CentOS 7 以下版本 iptables 命令

查看防火墙状态

1
2
[root@sunys ~]# service iptables status
iptables: Firewall is not running.

查看打开的端口

1
[root@sunys ~]# /etc/init.d/iptables status

开启端口

①使用命令行:

1
2
[root@sunys ~]# /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[root@sunys ~]# /etc/rc.d/init.d/iptables save #保存

②或者修改 /etc/sysconfig/iptables 文件,添加以下内容:

1
2
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

重启 / 停止 / 停止 / 查看防火墙

①永久关闭,重启后生效
开启: chkconfig iptables on
关闭: chkconfig iptables off
②即时生效,重启后失效
开启: service iptables start
关闭: service iptables stop

查看 SELinux 状态 & 关闭 SELinux

查看 SELinux 状态

① getenforce 命令是单词 get(获取)和 enforce (执行) 连写,可查看 selinux 状态,与 setenforce 命令相反。

1
2
[root@sunys ~]# getenforce
Enforcing

② /usr/sbin/sestatus 查看详细参数

1
2
3
4
5
6
7
8
9
10
[root@sunys ~]# /usr/sbin/sestatus
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: enforcing
Policy MLS status: enabled
Policy deny_unknown status: allowed
Max kernel policy version: 28

SELinux status:selinux 防火墙的状态,enabled 表示启用 selinux 防火墙
Current mode: selinux 防火墙当前的安全策略,enforcing 表示强


关闭 SELinux

①临时关闭
setenforce 命令则是单词 set(设置)和 enforce (执行) 连写,用于设置 selinux 防火墙状态,如: setenforce 0 用于关闭 selinux 防火墙,但重启后失效

1
2
3
4
5
6
7
8
9
10
11
[root@sunys ~]# setenforce 0
[root@sunys ~]# /usr/sbin/sestatus
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: permissive
Mode from config file: enforcing
Policy MLS status: enabled
Policy deny_unknown status: allowed
Max kernel policy version: 28

②永久关闭
修改 selinux 的配置文件,重启后生效。
打开 selinux 配置文件,将 SELINUX=enforcing 改为 SELINUX=disabled。

1
2
3
4
5
6
7
8
9
10
11
12
[root@sunys ~]# vim /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted

重启

1
[root@sunys ~]# reboot

验证

1
2
3
4
[root@sunys ~]# /usr/sbin/sestatus
SELinux status: disabled
[root@sunys ~]# getenforce
Disabled

CentOS 下开启 SSH Server 服务

查看 SSH 是否安装

1
[root@sunys ~]# rpm -qa | grep ssh

若没安装 SSH 则可输入一下命令安装

1
[root@sunys ~]# yum install openssh-server

启动 SSH 服务

1
[root@sunys ~]# systemctl start sshd

重启 SSH 服务

1
[root@sunys ~]# systemctl restart  sshd

停止 SSH 服务

1
[root@sunys ~]# systemctl stop sshd

启动后可输入一下命令查看是否启动 22 端口(可略)

1
[root@sunys ~]# netstat -antp | grep sshd

设置 SSH 服务为开机启动

1
[root@sunys ~]# systemctl enable sshd

设置禁止 SSH 开机启动

1
[root@sunys ~]# systemctl disable sshd

linux 下 java 读取串口相关问题

权限相关问题 No permission to create lock file

issue:

CentOS 下部署短信猫时报错:check_group_uucp (): error testing lock file creation Error details: 权限不够 check_lock_status: No permission to create lock file.please see: How can I use Lock Files with rxtx? in INSTALL

cause:

这个问题源于试图在 /var/lock 中创建锁文件的 rxtx 包。 可以通过更改访问权限(以 root 身份)来解决此问题

解决办法一

查找 /etc 下的 groups 文件,但是没有找到。只有 group、group- 这 2 个文件,其中 group 文件就是我们要找的。
打开此文件,将如下 2 段修改:
uucp:x:14:uucp 修改为 uucp:x:14:uucp,ebm
lock:x:54: 修改为 lock:x:54:ebm
修改这个文件后,必须重启系统,才能使之起效

解决办法二

执行如下命令

1
2
[root@sunys ~]# chgrp uucp /var/lock/
[root@sunys ~]# chmod g+w /var/lock/

文件已存在 File exists

issue:

RXTX fhs_lock () Error: creating lock file: /var/lock/LCK..ttyS0: 文件已存在

cause:

有多个进程同时使用串口,端口被占用

解决办法

执行如下命令

1
[root@sunys ~]# rm /var/lock/LCK..ttyS0