To print epoch - The seconds that has passed since 1970-01-01 00:00:00 UTC, run the following command,
date +"%s" 1480572641Here %s tells date to print the epoch.
Tested on: CentOS 7, Debian 8, Ubuntu 16.04
To print epoch - The seconds that has passed since 1970-01-01 00:00:00 UTC, run the following command,
date +"%s" 1480572641Here %s tells date to print the epoch.
Tested on: CentOS 7, Debian 8, Ubuntu 16.04
In your system administration life time, suppose you're looking forward to edit a configuration file and it turns out that the file is not available. You forgot what package to install to have that file or what package contains that file. What would you do then? To get the answer, read this post.
To find out which package in the repository contains a specific file, you can use the repoquery command. The syntax to do that is,
repoquery -qf file_pathHere file_path is the file that you're looking for.
To check what package provides the file /etc/my.cnf, run the following command.
repoquery -qf /etc/my.cnfmariadb-libs-1:5.5.44-2.el7.centos.x86_64 mariadb-libs-1:5.5.44-2.el7.centos.i686
From the output, we can see that the packages that provides the specific file /etc/my.cnf is mariadb-libs.
Tested on: CentOS 7
To check what configuration files are associated with a specific package that is already installed on your system using rpm command, use the syntax as written below.
rpm -qc package_nameHere package_name is any package that is already installed on your system.
To check the configuration files that are associated with the package httpd, the Apache HTTP server, run the following command.
rpm -qc httpd/etc/httpd/conf.d/autoindex.conf /etc/httpd/conf.d/userdir.conf /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.modules.d/00-base.conf /etc/httpd/conf.modules.d/00-dav.conf /etc/httpd/conf.modules.d/00-lua.conf /etc/httpd/conf.modules.d/00-mpm.conf /etc/httpd/conf.modules.d/00-proxy.conf /etc/httpd/conf.modules.d/00-systemd.conf /etc/httpd/conf.modules.d/01-cgi.conf /etc/httpd/conf/httpd.conf /etc/httpd/conf/magic /etc/logrotate.d/httpd /etc/sysconfig/htcacheclean /etc/sysconfig/httpd
Tested on: CentOS 7
At times you need to know which package contains a specific file on rpm based Linux distros such as Fedora, RedHat(RHEL), CentOS, openSUSE, SLE etc. Read below to learn how.
Let's say you want to know what package provides the ip command. It's a two step process.
To find the ip binary location, run the following command.
which ip /sbin/ipThe location is /sbin/ip as it's seen from the output of 'which'.
To find the package that contains the binary /sbin/ip, run the following command.
rpm -qf /sbin/ip iproute-3.10.0-54.el7.x86_64From the output of 'rpm' command, you can see that the package is "iproute-3.10.0-54.el7.x86_64". So this is the package that we were looking for.
You can use any file path with 'rpm -qf' command. As long as the rpm database knows about the file. You will get a result.
Tested on: CentOS 7
Today I was looking at the /etc/sysconfig/network-scripts/ifcfg-enp0s3 file and I came across the line that says UUID. I was thinking what is UUID of a network interface? Then I started thinking about how to find UUIDs of network interfcaes. I looked on the internet but I got no solution. Then I thought of trying to find it myself using network management commands. I did find a solution.
To find the UUIDs of your network connection, run the following command.
nmcli connection showNAME UUID TYPE DEVICE virbr0 4e02750f-13d9-4662-bfc2-10f9ae1a71bd bridge virbr0 Wired connection 1 09066de3-7eb7-4ee1-9059-c651b6dff7a6 802-3-ethernet -- enp0s3 5144084b-0537-4e4b-9f15-065431bc6d38 802-3-ethernet --
From the output of this command, you can find the UUID of a connection on CentOS, Fedora, RedHat(RHEL), Ubuntu, Debian operating systems.
Tested on: CentOS 7, Ubuntu 16.04 LTS
If you're trying to mount network devices automatically at boot on Linux and your system is not booting correctly. This post is for you.
Today I was working with iscsi. I created portals, acls, luns, targets with targetcli. But when I tried to mount it automatically through fstab file on boot, my system wasn't booting. I looked online and I found that I had to change the mount option. I did and it worked. I am going to write what mount option I used in this post.
When my system wasn't booting, my /etc/fstab configuration file looked like this,
# # /etc/fstab # Created by anaconda on Thu Nov 24 03:07:55 2016 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/centos-root / xfs defaults 0 0 UUID=0d83fc00-ee94-4741-8163-717418187f59 /boot xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0 UUID=f246ba49-5772-4efc-bec2-28ff76f46f79 /root/mounts/sdb xfs defaults 0 0
Here UUID=f246ba49-5772-4efc-bec2-28ff76f46f79, this is the iscsi drive I was trying to mount. The mount option was "defaults". But to get it to work, I had to change the mount option to "_netdev". What this option does it, it attempts to mount this device after all the network connection is initialized.
So my working configuration looked like this,
# # /etc/fstab # Created by anaconda on Thu Nov 24 03:07:55 2016 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/centos-root / xfs defaults 0 0 UUID=0d83fc00-ee94-4741-8163-717418187f59 /boot xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0 UUID=f246ba49-5772-4efc-bec2-28ff76f46f79 /root/mounts/sdb xfs _netdev 0 0
Tested on: CentOS 7
This post is about how to open specific tcp and udp ports using the command firewall-cmd. So without further due, let's get started.
Let's say, I want to open the tcp ports 80 and 443 which are used by http and https protocol respectively.
To do that, run the following commands,
firewall-cmd --permanent --add-port=80/tcp firewall-cmd --permanent --add-port=443/tcp
Let's say, I want to open the udp port 53, which is used by bind dns service.
To do that, run the following command,
firewall-cmd --permanent --add-port=53/udpNow we have to reload firewall-cmd configurations. To do that, run the following command,
firewall-cmd --reloadTo verify that the commands worked, let's view the current opened port list,
firewall-cmd --list-ports 443/tcp 80/tcp 53/udpTested on: CentOS 7
In one of my earlier post, I wrote about finding available software groups on CentOS/RedHat/Fedora. But you might not know what each of these group is for, what packages it will install and stuff like that. This is the topic of this post.
Let's say I want to know more about the group "Basic Web Server". To view the information, run the following command,
yum groups info "Basic Web Server"
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Environment Group: Basic Web Server
Environment-Id: web-server-environment
Description: Server for serving static and dynamic internet content.
Mandatory Groups:
+base
+core
+web-server
Optional Groups:
+backup-client
+debugging
+directory-client
+guest-agents
+hardware-monitoring
+java-platform
+large-systems
+load-balancer
+mariadb-client
+network-file-system-client
+performance
+perl-web
+php
+postgresql-client
+python-web
+remote-system-management
+web-servlet
That's it.
Tested on: CentOS 7
In the previous post, I wrote about how to find the available software/package groups on CentOS/RedHat/Fedora OS using yum. But how do we install a group? I will show you how to install a group of packages with yum.
Let's say we want to install a minimal graphical user interface (GUI) on our server. So the group to install for that is, "Server with GUI".
To install this group, run the following command,
yum groups install "Server with GUI"It should work. Thank you.
Tested on: CentOS 7
To view the available groups of packages on CentOS/RedHat/Fedora, run the following command,
yum groups list
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Available Environment Groups:
Minimal Install
Compute Node
Infrastructure Server
File and Print Server
Basic Web Server
Virtualization Host
Server with GUI
GNOME Desktop
KDE Plasma Workspaces
Development and Creative Workstation
Available Groups:
Compatibility Libraries
Console Internet Tools
Development Tools
Graphical Administration Tools
Legacy UNIX Compatibility
Scientific Support
Security Tools
Smart Card Support
System Administration Tools
System Management
Done
You can see from the output that, theres a lot of available groups on my system such as "Server with GUI", "Basic Web Server" etc.
Tested on: CentOS 7
I've been looking for a way to remove a group of packages I installed earlier. Then I came across this solution that I am going to share with you guys.
To remove a group using yum, run the following command,
yum groups remove "NameOfTheGroup"I installed the group "Development Tools", then I found that I don't really need it. So to remove it, I used the following command,
yum groups remove "Development Tools"Tested on: CentOS 7
There's no move() method on Java's File class. But there's a renameTo() method. We can use this method to move files on Java.
Let's see how that works.
import java.io.File; public class MoveFileTest { public static void main(String[] args) { File srcFile = new File("test/file.txt"); File destFile = new File("test2/file.txt"); srcFile.renameTo(destFile); } }
The file file.txt should move from test directory to test2 directory.
The code I've written before was for Linux only. But if you want it to work on both Linux and Windows you can rewrite the program as follows.
import java.io.File; public class MoveFileTest { public static void main(String[] args) { File srcFile = new File("test" + File.separator + "file.txt"); File destFile = new File("test2" + File.separator + "file.txt"); srcFile.renameTo(destFile); } }
If you want a more modular and reusable version of the code, you can rewrite the program as follows.
import java.io.File; public class MoveFileTest { public static void main(String[] args) { moveFile("file.txt", "test", "test2"); } private static void moveFile(String name, String srcDir, String dstDir) { File srcFile = new File( srcDir + File.separator + name); File destFile = new File(dstDir + File.separator + name); srcFile.renameTo(destFile); } }
Here I've created a method moveFile() to move files. Now I can just call the method whenever I want to move a file.
You may have a lots of command in your shells history. You can display your shell commands history by running the following command,
history1 history 2 lscpu 3 history
This is the output of the history command of my Ubuntu computer. In real scenario you may have hundreds of commands on the history.
To clear the command history of your terminal on Linux, run the following command,
history -cThat's how you remove the history of your shell on Linux.
To display information about the CPU architecture of a Linux system lscpu command is used.
The man page of lscpu says,
lscpu gathers CPU architecture information from sysfs and /proc/cpuinfo. The command output can be
optimized for parsing or for easy readability by humans. The information includes, for example,
the number of CPUs, threads, cores, sockets, and Non-Uniform Memory Access (NUMA) nodes. There is
also information about the CPU caches and cache sharing, family, model, bogoMIPS, byte order, and
stepping.
Options that result in an output table have a list argument. Use this argument to customize the
command output. Specify a comma-separated list of column labels to limit the output table to only
the specified columns, arranged in the specified order. See COLUMNS for a list of valid column
labels. The column labels are not case sensitive.
Not all columns are supported on all architectures. If an unsupported column is specified, lscpu
prints the column but does not provide any data for it.
lscpu command is very easy to use. To use lscpu, just run "lscpu" on the terminal.
lscpu
Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 1 On-line CPU(s) list: 0 Thread(s) per core: 1 Core(s) per socket: 1 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 78 Model name: Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz Stepping: 3 CPU MHz: 2399.996 BogoMIPS: 4799.99 Hypervisor vendor: KVM Virtualization type: full L1d cache: 32K L1i cache: 32K L2 cache: 256K L3 cache: 3072K NUMA node0 CPU(s): 0 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc pni pclmulqdq monitor ssse3 cx16 sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm 3dnowprefetch rdseed clflushopt
At times we need to login to our Ubuntu system manually. With manually I mean, by typing in the user name and password instead of clicking on an username and typing in the password. For example, you might want to login to root with Graphical User Interface (GUI) enabled. But if you can't login to root from lightdm, you might need to run several commands. Instead of going the hard way, we will just configure lightdm to let us do that easily.
Let's see how it's done.
To enable manual login on lightdm, start your terminal application and run the following command,
sudo nano /etc/lightdm/lightdm.conf.d/50-manual.confNow add the following lines to this file.
[SeatDefaults] greeter-show-manual-login=true
Now save the file and restart your computer. Once your computer starts, you will see a new line "Login" on lightdm user list.
Click on it, type in the username and press enter. Then type in the password for that user and press enter. Great! You're logged!
I've been looking all over the internet to find a way to login to a website with Selenium and keep the cookies saved. So I won't have to relogin every time. But all I got was disappointment. Then I came across the Google Chrome Driver. It has command line options to just what I was trying to do. I just want to share it with you guys. I know it's a pain in the ass to fix things like this.
System.setProperty("webdriver.chrome.driver", "driver\\chromedriver.exe"); ChromeOptions options = new ChromeOptions(); options.addArguments(String.format("user-data-dir=%s", "profile\\dir")); options.addArguments(String.format("profile-directory=%s", "User")); WebDriver driver = new ChromeDriver(options);
That's how I initialized the ChromeDriver and it worked like a charm. Do remember that, profile\\dir is the directory where you want to save the google chrome profile data. User is the username for which you would like to save the preference of. Usually a directory will be created inside the user-data-dir with "User", where all the user specific configurations will be saved.
I got a work from a client on web automation. But everything was working on mobile browser. No matter what way I try, I couldn't get it to work on my desktop browser. Then I found that I can simulate mobile web browsers using chrome webdriver for Selenium. I would like to share this with you, in case any of you are having the same problem.
ChromeDriver Official Website This is the website where I got my solution.
I am using Java language for my work. So I am gonna write the example on Java.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.remote.DesiredCapabilities; import java.util.HashMap; import java.util.Map; public class ChromeMobileDemo { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "tools\\chromedriver.exe"); MapmobileEmulation = new HashMap (); mobileEmulation.put("deviceName", "Google Nexus 5"); Map chromeOptions = new HashMap (); chromeOptions.put("mobileEmulation", mobileEmulation); DesiredCapabilities capabilities = DesiredCapabilities.chrome(); capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions); WebDriver driver = new ChromeDriver(capabilities); driver.get("https://www.google.com"); // wait 60 seconds before closing the window try { driver.wait(60000); } catch (InterruptedException e) { // do nothing } driver.close(); } }
Just change mobileEmulation.put("deviceName", "Google Nexus 5") line to mobileEmulation.put("deviceName", "iPhone 6");
You can select any device that you have on your Chrome's Developer Settings. Just put the same of the device.
At times we need multiple IP addresses. But what if we have only a single network interface card (NIC)? Well, we can set up multiple IP addresses on the same NIC. Follow along to see how.
Let's say I have an Ethernet interface enp0s3 with the IP address 10.0.2.15. Now I want to add another IP address to the interface. Let's say the new IP address is 10.0.2.18.
2: enp0s3:mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:45:4e:87 brd ff:ff:ff:ff:ff:ff inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3 valid_lft 86402sec preferred_lft 86402sec inet6 fe80::a00:27ff:fe45:4e87/64 scope link valid_lft forever preferred_lft forever
To add another IP address, use the following command,
sudo ip addr add dev enp0s3 10.0.2.18/24Here 10.0.2.18 is the new IP address and /24 is the subnet mask. enp0s3 is the interface that we want the IP address to be added to.
2: enp0s3:mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:45:4e:87 brd ff:ff:ff:ff:ff:ff inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3 valid_lft 86219sec preferred_lft 86219sec inet 10.0.2.18/24 scope global secondary enp0s3 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fe45:4e87/64 scope link valid_lft forever preferred_lft forever
From the output, you can see that the new IP address has been added. Now let's try to ping to the IP addresses.
PING 10.0.2.15 (10.0.2.15) 56(84) bytes of data. 64 bytes from 10.0.2.15: icmp_seq=1 ttl=64 time=0.098 ms 64 bytes from 10.0.2.15: icmp_seq=2 ttl=64 time=0.132 ms 64 bytes from 10.0.2.15: icmp_seq=3 ttl=64 time=0.065 ms 64 bytes from 10.0.2.15: icmp_seq=4 ttl=64 time=0.239 ms 64 bytes from 10.0.2.15: icmp_seq=5 ttl=64 time=0.054 ms 64 bytes from 10.0.2.15: icmp_seq=6 ttl=64 time=0.053 ms 64 bytes from 10.0.2.15: icmp_seq=7 ttl=64 time=0.121 ms 64 bytes from 10.0.2.15: icmp_seq=8 ttl=64 time=0.124 ms ^C --- 10.0.2.15 ping statistics --- 8 packets transmitted, 8 received, 0% packet loss, time 7007ms rtt min/avg/max/mdev = 0.053/0.110/0.239/0.058 msping 10.0.2.18
PING 10.0.2.18 (10.0.2.18) 56(84) bytes of data. 64 bytes from 10.0.2.18: icmp_seq=1 ttl=64 time=0.117 ms 64 bytes from 10.0.2.18: icmp_seq=2 ttl=64 time=0.113 ms 64 bytes from 10.0.2.18: icmp_seq=3 ttl=64 time=0.111 ms 64 bytes from 10.0.2.18: icmp_seq=4 ttl=64 time=0.134 ms 64 bytes from 10.0.2.18: icmp_seq=5 ttl=64 time=0.113 ms ^C --- 10.0.2.18 ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4000ms rtt min/avg/max/mdev = 0.111/0.117/0.134/0.014 ms
Both the IP addresses can be pinged. Everything is working as expected.
To Find the IP Address of a specific network interface on Linux, use the following command,
sudo ip addr show enp0s32: enp0s3:mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:45:4e:87 brd ff:ff:ff:ff:ff:ff inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3 valid_lft 85650sec preferred_lft 85650sec inet6 fe80::a00:27ff:fe45:4e87/64 scope link valid_lft forever preferred_lft forever
Here enp0s3 is the network interface that I want to see the address of.
The output shows that, enp0s3 has an ipv4 address of 10.0.2.15 and an ipv6 address of fe80::a00:27ff:fe45:4e87
To find the ip addresses of the available network interfaces on Linux, use the following command,
sudo ip addr show1: lo:mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp0s3: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:45:4e:87 brd ff:ff:ff:ff:ff:ff inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3 valid_lft 86403sec preferred_lft 86403sec inet6 fe80::a00:27ff:fe45:4e87/64 scope link valid_lft forever preferred_lft forever 3: virbr0: mtu 1500 qdisc noqueue state DOWN link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 valid_lft forever preferred_lft forever 4: virbr0-nic: mtu 1500 qdisc pfifo_fast state DOWN qlen 500 link/ether 52:54:00:bb:2e:64 brd ff:ff:ff:ff:ff:ff
You can see that, the result shows the network interfaces with numbers 1,2,3,4 and so on. I have 4 network interfaces on my Linux system.
You can see lines that start with inet or inet6. These lines represent ipv4 and ipv6 addresses respectively.
The first network interface which is a loopback interface has an ipv4 address of 127.0.0.1 and an ipv6 address of ::1
The second network interface enp0s3 has an ipv4 address of 10.0.2.15 and an ipv6 address of fe80::a00:27ff:fe45:4e87
The third network interface virbr0 has an ipv4 address of 192.168.122.1. It doesn't have an ipv6 address set.
The fourth network interface virbr0-nic doesn't have any ip address set at the moment.
To find the hardware address or MAC address of the available network interfaces on Linux, use the following command,
sudo ip link show1: lo:mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: enp0s3: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 08:00:27:45:4e:87 brd ff:ff:ff:ff:ff:ff 3: virbr0: mtu 1500 qdisc noqueue state DOWN mode DEFAULT link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff 4: virbr0-nic: mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT qlen 500 link/ether 52:54:00:bb:2e:64 brd ff:ff:ff:ff:ff:ff
You can see that, the result shows the network interfaces with numbers 1,2,3,4 and so on. I have 4 network interfaces on my Linux system. The first one is lo, which is a loopback interface with the hardware address 00:00:00:00:00:00. The second one is enp0s3, which is the ethernet interface with the hardware address 08:00:27:45:4e:87. The third one, virbr0, is a bridge interface with the MAC address 00:00:00:00:00:00. The fourth one is virbr0-nic, which is another bridge interface with the MAC address 52:54:00:bb:2e:64