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❌14:uucp 修改为 uucp❌14:uucp,ebm
lock❌54: 修改为 lock❌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
------------- The End -------------
对您有用的话就赞赏一个吧!
syshlang 微信 微信
syshlang 支付宝 支付宝
  • 本文作者: syshlang
  • 本文链接: https://syshlang.com/b529e05a/
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!