{"id":984,"date":"2014-07-05T11:20:15","date_gmt":"2014-07-05T09:20:15","guid":{"rendered":"http:\/\/blog.rabahi.net\/?page_id=984"},"modified":"2017-04-29T17:26:30","modified_gmt":"2017-04-29T15:26:30","slug":"useful-commands","status":"publish","type":"page","link":"https:\/\/blog.rabahi.net\/?page_id=984","title":{"rendered":"Useful Commands"},"content":{"rendered":"<div id=\"toc_container\" class=\"no_bullets\"><p class=\"toc_title\">Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Services_access\"><span class=\"toc_number toc_depth_1\">1<\/span> Services access<\/a><ul><li><a href=\"#Execution_level\"><span class=\"toc_number toc_depth_2\">1.1<\/span> Execution level<\/a><\/li><li><a href=\"#Services_management\"><span class=\"toc_number toc_depth_2\">1.2<\/span> Services management<\/a><\/li><\/ul><\/li><li><a href=\"#Virtual_network_interfaces\"><span class=\"toc_number toc_depth_1\">2<\/span> Virtual network interfaces<\/a><ul><li><a href=\"#Creating_Virtual_Interface\"><span class=\"toc_number toc_depth_2\">2.1<\/span> Creating Virtual Interface<\/a><\/li><li><a href=\"#Assign_Multiple_IP_Address_Range\"><span class=\"toc_number toc_depth_2\">2.2<\/span> Assign Multiple IP Address Range<\/a><\/li><\/ul><\/li><li><a href=\"#Log_rotate\"><span class=\"toc_number toc_depth_1\">3<\/span> Log rotate<\/a><\/li><li><a href=\"#Other_useful_commands\"><span class=\"toc_number toc_depth_1\">4<\/span> Other useful commands<\/a><ul><li><a href=\"#Get_mac_address\"><span class=\"toc_number toc_depth_2\">4.1<\/span> Get mac address<\/a><\/li><li><a href=\"#Get_my_ip_address\"><span class=\"toc_number toc_depth_2\">4.2<\/span> Get my ip address<\/a><\/li><li><a href=\"#Get_current_network_device\"><span class=\"toc_number toc_depth_2\">4.3<\/span> Get current network device<\/a><\/li><li><a href=\"#Export_X_Display\"><span class=\"toc_number toc_depth_2\">4.4<\/span> Export X Display<\/a><\/li><li><a href=\"#Execute_command_as_another_user\"><span class=\"toc_number toc_depth_2\">4.5<\/span> Execute command as another user<\/a><\/li><li><a href=\"#Create_pdf_from_png\"><span class=\"toc_number toc_depth_2\">4.6<\/span> Create pdf from png<\/a><\/li><li><a href=\"#Running_instances_informations\"><span class=\"toc_number toc_depth_2\">4.7<\/span> Running instances informations<\/a><\/li><li><a href=\"#Quick_install_fonts\"><span class=\"toc_number toc_depth_2\">4.8<\/span> Quick install fonts<\/a><\/li><li><a href=\"#Kill_pid_from_file\"><span class=\"toc_number toc_depth_2\">4.9<\/span> Kill pid from file<\/a><\/li><li><a href=\"#Make_change_in_service_file\"><span class=\"toc_number toc_depth_2\">4.10<\/span> Make change in service file<\/a><\/li><li><a href=\"#Shows_since_how_long_your_system_is_running\"><span class=\"toc_number toc_depth_2\">4.11<\/span> Shows since how long your system is running<\/a><\/li><li><a href=\"#Get_os_informations\"><span class=\"toc_number toc_depth_2\">4.12<\/span> Get os informations<\/a><\/li><\/ul><\/li><\/ul><\/div>\n<h1><span id=\"Services_access\">Services access<\/span><\/h1>\n<h2><span id=\"Execution_level\">Execution level<\/span><\/h2>\n<p>There are 7 execution levels:<\/p>\n<ul>\n<li>0 \u2014 Stop<\/li>\n<li>1 \u2014 One user mode<\/li>\n<li>2 \u2014 Not used (can be defined by user)<\/li>\n<li>3 \u2014 Multi user mode<\/li>\n<li>4 \u2014 Not used (can be defined by user)<\/li>\n<li>5 \u2014 Multi user mode (with graphical session)<\/li>\n<li>6 \u2014 Reboot<\/li>\n<\/ul>\n<p>Each one of these levels can be found in the directory: \/etc\/rc.d\/rc.d, where is the execution level.<br \/>\nTo modify the default execution level, modify the file \/etc\/inittab.<\/p>\n<h2><span id=\"Services_management\">Services management<\/span><\/h2>\n<p>We will use the command: <em>chkconfig<\/em><\/p>\n<pre lang=\"bash\"># To check a service, use this command:\r\nchkconfig --list myservice\r\n\r\n# For example, to disable a service from levels 3,4,5: \r\nchkconfig --level 345 myservice off\r\n\r\n# To enable a service at startup:\r\nchkconfig myservice on\r\n\r\n# To disable a service at statup\r\nchkconfig myservice off<\/pre>\n<h1><span id=\"Virtual_network_interfaces\">Virtual network interfaces<\/span><\/h1>\n<h2><span id=\"Creating_Virtual_Interface\">Creating Virtual Interface<\/span><\/h2>\n<pre lang=\"bash\">cp ifcfg-ens33 ifcfg-ens33:0\r\nservice network restart<\/pre>\n<p>If you ask ifconfig, you will have see the ens33:0 entry.<\/p>\n<h2><span id=\"Assign_Multiple_IP_Address_Range\">Assign Multiple IP Address Range<\/span><\/h2>\n<p>create the file \/etc\/sysconfig\/network-scripts\/ifcfg-ens33-range0 with the following entries:<\/p>\n<pre lang=\"bash\">IPADDR_START=192.168.0.1\r\nIPADDR_END=192.168.0.15\r\nNETMASK=255.255.255.0\r\nCLONENUM_START=0<\/pre>\n<p>This will map interface ens33:0 to 192.168.0.1; ens33:1 to 192.168.0.2; etc&#8230;<\/p>\n<h1><span id=\"Log_rotate\">Log rotate<\/span><\/h1>\n<p>create file here \/etc\/logrotate.d\/myconfiguration:<\/p>\n<pre lang=\"bash\"># size   \u2013 logrotate runs only if the filesize is equal to (or greater than) this size.\r\n# create \u2013 rotate the original file and create the new file with specified permission, user and group.\r\n# rotate \u2013 limits the number of log file rotation. So, this would keep only the recent 4 rotated log files.\r\n\r\n# compress \u2013 Indicates that compression should be done.\r\n# compresscmd \u2013 Specify what type of compression command should be used. For example: \/bin\/bzip2\r\n# compressext \u2013 Specify the extension on the rotated log file. Without this option, the rotated file wouldd have the default extension as .gz. So, if you use bzip2 compressioncmd, specify the extension as .bz2 as shown in the above example.\r\n\r\n# maxage : Remove older rotated log files\r\n# missingok : Dont return error if the log file is missing\r\n\r\n\/var\/log\/myconfiguration\/*.log {\r\n        missingok\r\n        notifempty\r\n\r\n        size 30k\r\n        copytruncate\r\n        create 0600 root root\r\n\r\n        compress        \r\n        compresscmd \/bin\/bzip2\r\n        compressext .bz2\r\n\r\n        rotate 4\r\n\r\n        maxage 100\r\n}<\/pre>\n<h1><span id=\"Other_useful_commands\">Other useful commands<\/span><\/h1>\n<h2><span id=\"Get_mac_address\">Get mac address<\/span><\/h2>\n<pre lang=\"bash\">cat \/sys\/class\/net\/ens33\/address<\/pre>\n<h2><span id=\"Get_my_ip_address\">Get my ip address<\/span><\/h2>\n<pre lang=\"bash\">hostname -I<\/pre>\n<h2><span id=\"Get_current_network_device\">Get current network device<\/span><\/h2>\n<pre lang=\"bash\">\r\nnmcli d | grep connected | awk '{split($1,a,\"\\t\"); print a[1]}'\r\n<\/pre>\n<h2><span id=\"Export_X_Display\">Export X Display<\/span><\/h2>\n<pre lang=\"bash\">\r\nyum -y install xhost\r\necho \"export display\"\r\nexport DISPLAY=myremotehost:0.0\r\necho \"enable connections from all hosts\"\r\nxhost +<\/pre>\n<p>On <em>myremotehost<\/em> must have a X Server.<br \/>\nFor windows user, you can use Xming <a href=\"http:\/\/sourceforge.net\/projects\/xming\/\">here<\/a><br \/>\nNote: do not forget to add the server ip in X0.hosts !<\/p>\n<h2><span id=\"Execute_command_as_another_user\">Execute command as another user<\/span><\/h2>\n<pre lang=\"bash\">runuser -l  myuser -c 'command'<\/pre>\n<h2><span id=\"Create_pdf_from_png\">Create pdf from png<\/span><\/h2>\n<pre lang=\"bash\">\r\n yum -y install ImageMagick\r\n # ask to compress pictures to minimize pdf size\r\n convert -compress jpeg `ls *.png` mygenerated.pdf\r\n<\/pre>\n<h2><span id=\"Running_instances_informations\">Running instances informations<\/span><\/h2>\n<pre lang=\"bash\">\r\necho \"You'll see the pid\/binary-name on far right column, match this to your running instance\"\r\nnetstat -tulpn\r\n\r\necho \"get running port for MYSERVICE (example sshd)\"\r\nnetstat -tulnp | grep sshd | awk '{split($4,a,\":\"); print a[4]}'\r\n\r\necho \"get running pid for MYSERVICE  (example sshd)\"\r\nnetstat -tulnp | grep sshd | awk '{split($7,a,\"\/\"); print a[1]}'\r\n<\/pre>\n<h2><span id=\"Quick_install_fonts\">Quick install fonts<\/span><\/h2>\n<pre lang=\"bash\">\r\nmkdir -p \/usr\/share\/fonts\/myFontName\r\ncp myfont.ttf \/usr\/share\/fonts\/myFontName\r\n\r\necho \"refresh the cache\"\r\nfc-cache -f -v\r\n<\/pre>\n<h2><span id=\"Kill_pid_from_file\">Kill pid from file<\/span><\/h2>\n<pre lang=\"bash\">\r\nkill -9 $(cat tmp\/pid)\r\n<\/pre>\n<h2><span id=\"Make_change_in_service_file\">Make change in service file<\/span><\/h2>\n<p>If you want to make any change to a .service file (example httpd.service), you have to reload daemon<\/p>\n<pre lang=\"bash\">\r\nsystemctl daemon-reload\r\n<\/pre>\n<p>before (re) starting your service :<\/p>\n<pre lang=\"bash\">\r\nsystemctl restart myservice.service\r\n<\/pre>\n<h2><span id=\"Shows_since_how_long_your_system_is_running\">Shows since how long your system is running<\/span><\/h2>\n<pre lang=\"bash\">\r\nuptime\r\n<\/pre>\n<h2><span id=\"Get_os_informations\">Get os informations<\/span><\/h2>\n<pre lang=\"bash\">\r\n[root@localhost ~]# cat \/etc\/os-release\r\nNAME=\"CentOS Linux\"\r\nVERSION=\"7 (Core)\"\r\nID=\"centos\"\r\nID_LIKE=\"rhel fedora\"\r\nVERSION_ID=\"7\"\r\nPRETTY_NAME=\"CentOS Linux 7 (Core)\"\r\nANSI_COLOR=\"0;31\"\r\nCPE_NAME=\"cpe:\/o:centos:centos:7\"\r\nHOME_URL=\"https:\/\/www.centos.org\/\"\r\nBUG_REPORT_URL=\"https:\/\/bugs.centos.org\/\"\r\n\r\nCENTOS_MANTISBT_PROJECT=\"CentOS-7\"\r\nCENTOS_MANTISBT_PROJECT_VERSION=\"7\"\r\nREDHAT_SUPPORT_PRODUCT=\"centos\"\r\nREDHAT_SUPPORT_PRODUCT_VERSION=\"7\"\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 Services access1.1 Execution level1.2 Services management2 Virtual network interfaces2.1 Creating Virtual Interface2.2 Assign Multiple IP Address Range3 Log rotate4 Other useful commands4.1 Get mac address4.2 Get my ip address4.3 Get current network device4.4 Export X Display4.5 Execute command as another user4.6 Create pdf from png4.7 Running instances informations4.8 Quick install fonts4.9 Kill pid from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":443,"menu_order":0,"comment_status":"closed","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-984","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/984","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=984"}],"version-history":[{"count":21,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/984\/revisions"}],"predecessor-version":[{"id":1891,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/984\/revisions\/1891"}],"up":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/443"}],"wp:attachment":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}